private Node newBranchesInLineNode(String name, FileInstrumentationData data) { List<Node> assignments = new ArrayList<>(); // var JSCompiler_lcov_branchesInLine = []; assignments.add(IR.var(IR.name(name), IR.arraylit())); int lineWithBranch = 0; for (int lineIdx = 1; lineIdx <= data.maxBranchPresentLine(); ++lineIdx) { Integer numBranches = data.getNumBranches(lineIdx); if (numBranches != null && numBranches > 0) { // JSCompiler_lcov_branchesInLine[<branch-index>] = 2; Node assignment = IR.exprResult( IR.assign( IR.getelem(IR.name(name), IR.number(lineWithBranch++)), IR.number(numBranches))); assignments.add(assignment.useSourceInfoIfMissingFromForTree(assignment)); } } return IR.block(assignments); } }
public Node buildLineAndConditionInitialisation(String uri, int lineNo, int conditionNo, int position, int length) { Node indexLineNumber = buildLineDeclaration(uri, lineNo); Node conditionNumberLiteral = IR.number(conditionNo); Node indexConditionNumber = IR.getelem(indexLineNumber, conditionNumberLiteral); Node positionLiteral = IR.number(position); Node lengthLiteral = IR.number(length); Node initCall = IR.string("init"); Node propertyName = IR.getprop(indexConditionNumber, initCall); Node fnCall = IR.call(propertyName, positionLiteral, lengthLiteral); return IR.exprResult(fnCall); }
Node buildLineDeclaration(String uri, int lineNo) { Node coverVar = IR.name("_$jscoverage"); Node path = IR.string(uri); Node getURI = IR.getelem(coverVar, path); Node prop = IR.string("branchData"); Node propGet = IR.getprop(getURI, prop); Node number = IR.string("" + lineNo); return IR.getelem(propGet, number); }
IR.constNode( IR.name(iteratorTempName), NodeUtil.newCallNode(NodeUtil.newQName(compiler, "$jscomp.makeAsyncIterator"), rhs)) .useSourceInfoIfMissingFromForTree(rhs); IR.constNode(IR.name(resultTempName), constructAwaitNextResult(ctx, iteratorTempName)); Node breakIfDone = IR.ifNode(getprop(IR.name(resultTempName), "done"), IR.block(IR.breakNode())); if (lhs.isValidAssignmentTarget()) { lhsAssignment = IR.exprResult(IR.assign(lhs, getprop(IR.name(resultTempName), "value"))); } else if (NodeUtil.isNameDeclaration(lhs)) { lhs.getFirstChild().addChildToBack(getprop(IR.name(resultTempName), "value")); lhsAssignment = lhs; } else { IR.forNode( initializer, IR.empty(), IR.empty(), IR.block(resultDeclaration, breakIfDone, lhsAssignment, ensureBlock(originalBody))); forAwaitOf.replaceWith(newForLoop); newForLoop.useSourceInfoIfMissingFromForTree(forAwaitOf);
/** Wraps the entire current module definition in a $jscomp.registerAndLoadModule function. */ private void registerAndLoadModule(NodeTraversal t) { Node block = IR.block(); block.addChildrenToFront(script.removeChildren()); block.addChildToFront(IR.exprResult(IR.string(pragma))); Node moduleFunction = IR.function( IR.name(""), IR.paramList(IR.name(REQUIRE), IR.name(EXPORTS), IR.name(MODULE)), block); Node shallowDeps = new Node(Token.ARRAYLIT); for (ModuleRequest request : importRequests) { shallowDeps.addChildToBack(IR.string(request.specifier())); } Node exprResult = IR.exprResult( IR.call( IR.getprop(IR.name("$jscomp"), IR.string("registerAndLoadModule")), moduleFunction, // Resolving this path enables removing module roots from this path. IR.string( normalizePath( compiler.getModuleLoader().resolve(t.getInput().getName()).toString())), shallowDeps)); script.addChildToBack(exprResult.useSourceInfoIfMissingFromForTree(script)); compiler.reportChangeToChangeScope(script); compiler.reportChangeToChangeScope(moduleFunction); t.reportCodeChange(); }
/** Adds an ES5 getter to the given object literal to use an an export. */ private void addExport(Node definePropertiesLit, String exportedName, LocalQName localQName) { Node exportedValue = NodeUtil.newQName(compiler, localQName.qName); Node getterFunction = IR.function(IR.name(""), IR.paramList(), IR.block(IR.returnNode(exportedValue))); getterFunction.useSourceInfoFromForTree(localQName.nodeForSourceInfo); Node objLit = IR.objectlit( IR.stringKey("enumerable", IR.trueNode()), IR.stringKey("get", getterFunction)); definePropertiesLit.addChildToBack(IR.stringKey(exportedName, objLit)); compiler.reportChangeToChangeScope(getterFunction); }
/** * Adds the generated setter for a readonly property. * @see https://www.polymer-project.org/0.8/docs/devguide/properties.html#read-only */ private Node makeReadOnlySetter(String propName, String qualifiedPath) { String setterName = "_set" + propName.substring(0, 1).toUpperCase() + propName.substring(1); Node fnNode = IR.function(IR.name(""), IR.paramList(IR.name(propName)), IR.block()); Node exprResNode = IR.exprResult( IR.assign(NodeUtil.newQName(compiler, qualifiedPath + setterName), fnNode)); JSDocInfoBuilder info = new JSDocInfoBuilder(true); // This is overriding a generated function which was added to the interface in // {@code addInterfaceExterns}. info.recordOverride(); exprResNode.getFirstChild().setJSDocInfo(info.build()); return exprResNode; }
private void visitExportStar(NodeTraversal t, Node export, Node parent) { // export * from 'moduleIdentifier'; Node moduleIdentifier = export.getLastChild(); // Make an "import 'spec'" from this export node and then visit it to rewrite to a require(). Node importNode = IR.importNode(IR.empty(), IR.empty(), moduleIdentifier.cloneNode()); importNode.useSourceInfoFrom(export); parent.addChildBefore(importNode, export); visit(t, importNode, parent); String moduleName = getVarNameOfImport(moduleIdentifier.getString()); export.replaceWith( IR.exprResult( IR.call( IR.getprop(IR.name("$$module"), IR.string("exportAllFrom")), IR.name(moduleName))) .useSourceInfoFromForTree(export)); t.reportCodeChange(); }
/** * Adds a computed property method, with the name of the marker, to the class. * * <pre>{@code * class C { * ['instance_of__C']() {} * } * }</pre> */ private Node addMarkerToClass(String markerName, @Nullable String unused, Node classMembers) { Node function = IR.function(IR.name(""), IR.paramList(), IR.block()); Node member = IR.computedProp(IR.string(markerName), function); member.putBooleanProp(Node.COMPUTED_PROP_METHOD, true); classMembers.addChildToBack(member); compiler.reportChangeToEnclosingScope(member); compiler.reportChangeToChangeScope(function); NodeUtil.addFeatureToScript(traversal.getCurrentScript(), Feature.COMPUTED_PROPERTIES); return classMembers; }
@Override public void visit(NodeTraversal t, Node n, Node parent) { if (n.isReturn() && n.hasChildren()) { Node retVal = n.getFirstChild(); n.removeChild(retVal); parent.replaceChild(n, IR.exprResult( IR.assign( IR.getprop(IR.name("module"), IR.string("exports")), retVal)) .useSourceInfoFromForTree(n)); } } }
/** * Create an assignment to the branch coverage data for the given index into the array. * * @return the newly constructed assignment node. */ private Node newBranchInstrumentationNode(NodeTraversal traversal, Node node, int idx) { String arrayName = createArrayName(traversal); // Create instrumentation Node Node getElemNode = IR.getelem(IR.name(arrayName), IR.number(idx)); // Make line number 0-based Node exprNode = IR.exprResult(IR.assign(getElemNode, IR.trueNode())); // Note line as instrumented String fileName = traversal.getSourceName(); if (!instrumentationData.containsKey(fileName)) { instrumentationData.put(fileName, new FileInstrumentationData(fileName, arrayName)); } return exprNode.useSourceInfoIfMissingFromForTree(node); }
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; }
private static Node createStateUpdate() { return IR.exprResult(IR.assign(IR.name(GENERATOR_STATE), IR.number(generatorCaseCount))); }
private Node newReportFunctionExitNode(Node infoNode, Node returnRhs) { Node call = IR.call( IR.name(reportFunctionExitName), IR.number(functionId), (returnRhs != null) ? returnRhs : IR.name("undefined"), IR.string(functionName)); call.putBooleanProp(Node.FREE_CALL, true); call.useSourceInfoFromForTree(infoNode); return call; }
Node name = IR.name(ARGUMENTS_VAR); Node argumentsVar = IR.constNode(name, IR.name("arguments")); JSDocInfoBuilder jsdoc = new JSDocInfoBuilder(false); jsdoc.recordType( new JSTypeExpression( new Node(Token.BANG, IR.string("Arguments")), "<Es6RewriteArrowFunction>")); argumentsVar.setJSDocInfo(jsdoc.build()); argumentsVar.useSourceInfoIfMissingFromForTree(scopeRoot); Node name = IR.name(THIS_VAR); Node thisVar = IR.constNode(name, IR.thisNode()); thisVar.useSourceInfoIfMissingFromForTree(scopeRoot); scopeRoot.addChildToFront(thisVar);
private static Node createFinallyJumpBlock(Node finallyName, int finallyStartState) { int jumpPoint = generatorCaseCount++; Node setReturnState = IR.exprResult(IR.assign(finallyName.cloneTree(), IR.number(jumpPoint))); Node toFinally = createStateUpdate(finallyStartState); Node returnPoint = makeGeneratorMarker(jumpPoint); Node returnBlock = IR.block(setReturnState, toFinally, createSafeBreak()); returnBlock.addChildToBack(returnPoint); return returnBlock; }
public Node buildConditionalStatement(int startLine, int endLine, String fileName) { Node indexLineNumber = buildLineNumberExpression(startLine, fileName, "conditionals"); Node lineNumberLiteral = IR.number(endLine); Node assignment = IR.assign(indexLineNumber, lineNumberLiteral); return IR.exprResult(assignment); }