/** * Creates a reference to "arguments" with the type specified in externs, or unknown if the * externs for it weren't included. */ Node createArgumentsReference() { Node result = IR.name("arguments"); if (isAddingTypes()) { result.setJSType(argumentsTypeSupplier.get()); } return result; }
Node createName(String name, JSType type) { Node result = IR.name(name); if (isAddingTypes()) { result.setJSType(checkNotNull(type)); } return result; }
public Node buildBranchRecordingFunction(String uri, int id, int lineNo, int conditionNo) { Node params = IR.paramList(IR.name("result")); Node name = IR.name(format("visit%d_%d_%d", id, lineNo, conditionNo)); Node lineAndConditionCall = buildLineAndConditionCall(uri, lineNo, conditionNo); Node body = IR.block(lineAndConditionCall); body.addChildToBack(IR.returnNode(IR.name("result"))); return IR.function(name, params, body); } }
private Node createUndefinedNode() { Node undefined = IR.name("undefined"); if (shouldAddTypesOnNewAstNodes) { undefined.setJSType(getNativeType(VOID_TYPE)); } return undefined; }
/** * Replaces a string literal with a reference to the string's alias variable. */ private void replaceStringWithAliasName(StringOccurrence occurrence, String name, StringInfo info) { Node nameNode = IR.name(name); occurrence.parent.replaceChild(occurrence.node, nameNode); info.isAliased = true; compiler.reportChangeToEnclosingScope(nameNode); }
/** * Create a valid statement Node containing an assignment to name of the * given expression. */ private static Node createAssignStatementNode(String name, Node expression) { // Create 'name = result-expression;' statement. // EXPR (ASSIGN (NAME, EXPRESSION)) Node nameNode = IR.name(name); Node assign = IR.assign(nameNode, expression); return NodeUtil.newExpr(assign); }
Node createName(Scope scope, String name) { Node result = IR.name(name); if (isAddingTypes()) { result.setJSType(getVarNameType(scope, name)); } return result; }
/** * Create and return a new array declaration node. The array name is * generated based on the source filename, and declaration is of the form: * "var arrayNameUsedInFile = [];" */ private Node newArrayDeclarationNode(NodeTraversal traversal) { return IR.var( IR.name(createArrayName(traversal)), IR.arraylit()); }
/** Creates a NAME node having the type of "this" appropriate for the given function node. */ Node createThisAliasReferenceForFunction(String aliasName, Node functionNode) { final Node result = IR.name(aliasName); if (isAddingTypes()) { result.setJSType(getTypeOfThisForFunctionNode(functionNode)); } return result; }
Node createName(String name, JSTypeNative nativeType) { Node result = IR.name(name); if (isAddingTypes()) { result.setJSType(getNativeType(nativeType)); } return result; }
private static Node newName(AbstractCompiler compiler, String name) { Node nameNode = IR.name(name); nameNode.setLength(name.length()); if (compiler.getCodingConvention().isConstant(name)) { nameNode.putBooleanProp(Node.IS_CONSTANT_NAME, true); } return nameNode; }
/** * Infer the parameter types from the doc info alone. */ FunctionTypeBuilder inferParameterTypes(JSDocInfo info) { // Create a fake args parent. Node lp = IR.paramList(); for (String name : info.getParameterNames()) { lp.addChildToBack(IR.name(name)); } return inferParameterTypes(lp, info); }
@Override public void performRemove(AbstractCompiler compiler) { // replace internal name with "" function.replaceChild(function.getFirstChild(), IR.name("")); compiler.reportChangeToEnclosingScope(function.getFirstChild()); } }
private void addExternForGlobalSymbolNamespace() { Node varNode = IR.var(IR.name(globalSymbolNamespace)); CompilerInput input = compiler.getSynthesizedExternsInput(); input.getAstRoot(compiler).addChildToBack(varNode); compiler.reportChangeToEnclosingScope(varNode); }
/** Set the given function/class node to an empty name */ public static void removeName(Node n) { checkState(n.isFunction() || n.isClass()); Node originalName = n.getFirstChild(); Node emptyName = n.isFunction() ? IR.name("") : IR.empty(); n.replaceChild(originalName, emptyName.useSourceInfoFrom(originalName)); }
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); }
private static Node getChromeI18nGetMessageNode(String messageId) { Node chromeI18n = IR.getprop(IR.name("chrome"), IR.string("i18n")); Node getMessage = IR.getprop(chromeI18n, IR.string("getMessage")); return IR.call(getMessage, IR.string(messageId)); }
private Node createMathPowCall() { return IR.call( IR.getprop( IR.name("Math").setJSType(mathType), // Force wrapping. IR.string("pow").setJSType(stringType)) .setJSType(mathPowType)) .setJSType(numberType); } }
private Node tryRewriteStringValueOfCall(Node n) { Node param = n.getSecondChild(); if (NodeUtil.isUndefined(param)) { return IR.string("null"); } else if (NodeUtil.isDefinedValue(param) && !param.isArrayLit()) { // Generate String(param), let other peephole optimizations handle the rest when safe return NodeUtil.newCallNode(IR.name("String").useSourceInfoFrom(n), param.detach()); } return n; }
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; }