/** * Overwrite all the source information in this node and its subtree with * that of {@code other} iff the source info is missing. */ public final Node useSourceInfoIfMissingFromForTree(Node other) { useSourceInfoIfMissingFrom(other); for (Node child = first; child != null; child = child.next) { child.useSourceInfoIfMissingFromForTree(other); } return this; }
private Node createConditionalObjectDecl(String name, Node srcref) { String jscovDecl = " var " + name + " = window.top.__jscov || " + "(window.top.__jscov = {fileNames:[], instrumentedLines: [], executedLines: []});"; Node script = compiler.parseSyntheticCode(jscovDecl); Node var = script.removeFirstChild(); return var.useSourceInfoIfMissingFromForTree(srcref); } }
/** Add a default block for conditional statements, e.g., If, Switch. */ private Node addDefaultBlock(Node node) { Node defaultBlock = IR.block(); node.addChildToBack(defaultBlock); return defaultBlock.useSourceInfoIfMissingFromForTree(node); }
/** * Overwrite all the source information in this node and its subtree with * that of {@code other} iff the source info is missing. */ public Node useSourceInfoIfMissingFromForTree(Node other) { useSourceInfoIfMissingFrom(other); for (Node child = getFirstChild(); child != null; child = child.getNext()) { child.useSourceInfoIfMissingFromForTree(other); } return this; }
/** * Example: * a = (void) 0; */ private static void addDummyAssignment(Node node, String resultName) { checkArgument(node.isBlock()); // A result is needed create a dummy value. Node srcLocation = node; Node retVal = NodeUtil.newUndefinedNode(srcLocation); Node resultNode = createAssignStatementNode(resultName, retVal); resultNode.useSourceInfoIfMissingFromForTree(node); node.addChildToBack(resultNode); }
/** * Try to fold arithmetic binary operators */ private Node tryFoldArithmeticOp(Node n, Node left, Node right) { Node result = performArithmeticOp(n.getToken(), left, right); if (result != null) { result.useSourceInfoIfMissingFromForTree(n); reportChangeToEnclosingScope(n); n.replaceWith(result); return result; } return n; }
private Node maybeProcessOptionalProperty(Node n, Node type) { if (n.isOptionalEs6Typed()) { n.putBooleanProp(Node.OPT_ES6_TYPED, false); TypeDeclarationNode baseType = (TypeDeclarationNode) maybeCreateAnyType(n, type); type = TypeDeclarationsIR.unionType( ImmutableList.of(baseType, TypeDeclarationsIR.undefinedType())); type.useSourceInfoIfMissingFromForTree(baseType); } else { type = maybeCreateAnyType(n, type); } return convertWithLocation(type); }
private Node maybeProcessOptionalProperty(Node n, Node type) { if (n.isOptionalEs6Typed()) { n.putBooleanProp(Node.OPT_ES6_TYPED, false); TypeDeclarationNode baseType = (TypeDeclarationNode) maybeCreateAnyType(n, type); type = TypeDeclarationsIR.unionType( ImmutableList.of(baseType, TypeDeclarationsIR.undefinedType())); type.useSourceInfoIfMissingFromForTree(baseType); } else { type = maybeCreateAnyType(n, type); } return convertWithLocation(type); }
/** * Creates the js code to be added to source. This code declares and * initializes the variables required for collection of coverage data. */ private void addHeaderCode(Node script) { script.addChildToFront(createConditionalObjectDecl(JS_INSTRUMENTATION_OBJECT_NAME, script)); // Make subsequent usages of "window" and "window.top" work in a Web Worker context. script.addChildToFront( compiler.parseSyntheticCode( "if (!self.window) { self.window = self; self.window.top = self; }") .removeFirstChild() .useSourceInfoIfMissingFromForTree(script)); }
/** Returns an assign replacing the equivalent var or let declaration. */ private static Node varToAssign(Node var) { Node assign = IR.assign(var.getFirstChild().cloneNode(), var.getFirstChild().removeFirstChild()); return IR.exprResult(assign).useSourceInfoIfMissingFromForTree(var); } }
private void visitExponentiationOperator(Node operator) { Node callClone = mathPowCall.cloneTree(); callClone.addChildToBack(operator.removeFirstChild()); // Base argument. callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument. callClone.useSourceInfoIfMissingFromForTree(operator); operator.replaceWith(callClone); compiler.reportChangeToEnclosingScope(callClone); }
/** * If the function is an arrow function, wrap the body in a block if it is not already a block. */ // TODO(bradfordcsmith): This should be separated from this pass. private void ensureArrowFunctionsHaveBlockBodies(NodeTraversal t, Node function) { Node body = function.getLastChild(); if (!body.isBlock()) { body.detach(); Node replacement = IR.block(IR.returnNode(body)).useSourceInfoIfMissingFromForTree(body); function.addChildToBack(replacement); t.reportCodeChange(); } }
Node processComputedPropertyGetter(ComputedPropertyGetterTree tree) { maybeWarnForFeature(tree, Feature.COMPUTED_PROPERTIES); Node key = transform(tree.property); Node body = transform(tree.body); Node function = IR.function(IR.name(""), IR.paramList(), body); function.useSourceInfoIfMissingFromForTree(body); Node n = newNode(Token.COMPUTED_PROP, key, function); n.putBooleanProp(Node.COMPUTED_PROP_GETTER, true); n.putBooleanProp(Node.STATIC_MEMBER, tree.isStatic); return n; }
Node processComputedPropertyGetter(ComputedPropertyGetterTree tree) { maybeWarnEs6Feature(tree, Feature.COMPUTED_PROPERTIES); Node key = transform(tree.property); Node body = transform(tree.body); Node function = IR.function(IR.name(""), IR.paramList(), body); function.useSourceInfoIfMissingFromForTree(body); Node n = newNode(Token.COMPUTED_PROP, key, function); n.putBooleanProp(Node.COMPUTED_PROP_GETTER, true); n.putBooleanProp(Node.STATIC_MEMBER, tree.isStatic); return n; }
Node processComputedPropertySetter(ComputedPropertySetterTree tree) { maybeWarnEs6Feature(tree, Feature.COMPUTED_PROPERTIES); Node key = transform(tree.property); Node body = transform(tree.body); Node paramList = IR.paramList(safeProcessName(tree.parameter)); Node function = IR.function(IR.name(""), paramList, body); function.useSourceInfoIfMissingFromForTree(body); Node n = newNode(Token.COMPUTED_PROP, key, function); n.putBooleanProp(Node.COMPUTED_PROP_SETTER, true); n.putBooleanProp(Node.STATIC_MEMBER, tree.isStatic); return n; }
private void visitExponentiationAssignmentOperator(Node operator) { Node lValue = operator.removeFirstChild(); Node callClone = mathPowCall.cloneTree(); callClone.addChildToBack(lValue.cloneTree()); // Base argument. callClone.addChildToBack(operator.removeFirstChild()); // Exponent argument. Node assignment = IR.assign(lValue, callClone).setJSType(numberType); assignment.useSourceInfoIfMissingFromForTree(operator); operator.replaceWith(assignment); compiler.reportChangeToEnclosingScope(assignment); }
private void visitArrowFunction(NodeTraversal t, Node n, ThisAndArgumentsContext context) { n.setIsArrowFunction(false); Node body = n.getLastChild(); if (!body.isBlock()) { body.detach(); body = IR.block(IR.returnNode(body)).useSourceInfoIfMissingFromForTree(body); n.addChildToBack(body); } ThisAndArgumentsReferenceUpdater updater = new ThisAndArgumentsReferenceUpdater(compiler, context); NodeTraversal.traverse(compiler, body, updater); t.reportCodeChange(); }
Node processComputedPropertySetter(ComputedPropertySetterTree tree) { maybeWarnForFeature(tree, Feature.COMPUTED_PROPERTIES); Node key = transform(tree.property); Node paramList = processFormalParameterList(tree.parameter); setSourceInfo(paramList, tree.parameter); Node body = transform(tree.body); Node function = IR.function(IR.name(""), paramList, body); function.useSourceInfoIfMissingFromForTree(body); Node n = newNode(Token.COMPUTED_PROP, key, function); n.putBooleanProp(Node.COMPUTED_PROP_SETTER, true); n.putBooleanProp(Node.STATIC_MEMBER, tree.isStatic); return n; }
private JSTypeExpression createIObject(NodeTraversal t, Node indexSignature) { Node indexType = convertWithLocation(indexSignature.getFirstChild() .getDeclaredTypeExpression()); Node declaredType = convertWithLocation(indexSignature.getDeclaredTypeExpression()); Node block = new Node(Token.BLOCK, indexType, declaredType); Node iObject = IR.string("IObject"); iObject.addChildToFront(block); JSTypeExpression bang = new JSTypeExpression(new Node(Token.BANG, iObject) .useSourceInfoIfMissingFromForTree(indexSignature), indexSignature.getSourceFileName()); indexSignature.detach(); t.reportCodeChange(); return bang; }
private JSTypeExpression createIObject(Node indexSignature) { Node indexType = convertWithLocation(indexSignature.getFirstChild() .getDeclaredTypeExpression()); Node declaredType = convertWithLocation(indexSignature.getDeclaredTypeExpression()); Node block = new Node(Token.BLOCK, indexType, declaredType); Node iObject = IR.string("IObject"); iObject.addChildrenToFront(block); JSTypeExpression bang = new JSTypeExpression(new Node(Token.BANG, iObject) .useSourceInfoIfMissingFromForTree(indexSignature), indexSignature.getSourceFileName()); indexSignature.detachFromParent(); compiler.reportCodeChange(); return bang; }