/** * Creates a new `const` declaration statement for a single variable name. * * <p>Takes the type for the variable name from the value node. * * <p>e.g. `const variableName = value;` */ Node createSingleConstNameDeclaration(String variableName, Node value) { return IR.constNode(createName(variableName, value.getJSType()), value); }
private void visitExportDefault(NodeTraversal t, Node export, Node parent) { Node child = export.getFirstChild(); String name = null; if (child.isFunction() || child.isClass()) { name = NodeUtil.getName(child); } if (name != null) { Node decl = child.detach(); parent.replaceChild(export, decl); } else { name = JSCOMP_DEFAULT_EXPORT; // Default exports are constant in more ways than one. Not only can they not be // overwritten but they also act like a const for temporal dead-zone purposes. Node var = IR.constNode(IR.name(name), export.removeFirstChild()); parent.replaceChild(export, var.useSourceInfoIfMissingFromForTree(export)); NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.CONST_DECLARATIONS); } exportedNameToLocalQName.put("default", new LocalQName(name, export)); t.reportCodeChange(); }
/** * Splits the given name of an export spec into its own constant export statement. * * @return the right hand side of the new const node */ private Node splitExportSpec(NodeTraversal t, Node nameNode, Node exportSpec) { Node oldExport = exportSpec.getGrandparent(); // export spec -> specs -> export Node newRHSNameNode = IR.name(nameNode.getString()); Node newExport = IR.export(IR.constNode(IR.name(exportSpec.getSecondChild().getString()), newRHSNameNode)) .srcrefTree(exportSpec); oldExport.getParent().addChildAfter(newExport, oldExport); exportSpec.detach(); t.reportCodeChange(); return newRHSNameNode; }
} else { exportSpecNode = IR.constNode(IR.name(exportedSymbol), rhs); exportSpecNode.useSourceInfoFrom(rhs);
IR.constNode(IR.name(thisVarName), IR.thisNode()).useSourceInfoFromForTree(block)); IR.constNode(IR.name(argumentsVarName), IR.name("arguments")) .useSourceInfoFromForTree(block)); NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.ARROW_FUNCTIONS); String superReplacementName = superPropGetterPrefix + replacedMethodName; prefixBlock.addChildToBack(IR.constNode(IR.name(superReplacementName), arrowFunction));
private void extractExtends(NodeTraversal t, Node classNode) { String name = ModuleNames.fileToJsIdentifier(classNode.getStaticSourceFile().getName()) + CLASS_EXTENDS_VAR + classExtendsVarCounter++; Node statement = NodeUtil.getEnclosingStatement(classNode); Node originalExtends = classNode.getSecondChild(); originalExtends.replaceWith(IR.name(name).useSourceInfoFrom(originalExtends)); Node extendsAlias = IR.constNode(IR.name(name), originalExtends) .useSourceInfoIfMissingFromForTree(originalExtends); statement.getParent().addChildBefore(extendsAlias, statement); NodeUtil.addFeatureToScript(NodeUtil.getEnclosingScript(classNode), Feature.CONST_DECLARATIONS); t.reportCodeChange(classNode); }
IR.constNode( IR.name(iteratorTempName), NodeUtil.newCallNode(NodeUtil.newQName(compiler, "$jscomp.makeAsyncIterator"), rhs)) IR.constNode(IR.name(resultTempName), constructAwaitNextResult(ctx, iteratorTempName)); Node breakIfDone = IR.ifNode(getprop(IR.name(resultTempName), "done"), IR.block(IR.breakNode()));
Node thisVar = IR.constNode(name, IR.thisNode().setJSType(context.getThisType())); NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.CONST_DECLARATIONS); thisVar.useSourceInfoIfMissingFromForTree(scopeBody); Node name = IR.name(ARGUMENTS_VAR).setJSType(context.getArgumentsType()); Node argumentsVar = IR.constNode(name, IR.name("arguments").setJSType(context.getArgumentsType())); NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.CONST_DECLARATIONS); scopeBody.addChildToFront(argumentsVar);
if (addArguments) { Node name = IR.name(ARGUMENTS_VAR); Node argumentsVar = IR.constNode(name, IR.name("arguments")); JSDocInfoBuilder jsdoc = new JSDocInfoBuilder(false); jsdoc.recordType( Node thisVar = IR.constNode(name, IR.thisNode()); thisVar.useSourceInfoIfMissingFromForTree(scopeRoot); scopeRoot.addChildToFront(thisVar);
checkState(child.getFirstChild().isName()); Node constNode = IR.constNode( IR.name(child.getFirstChild().getString()), IR.getprop(
parent.replaceChild(classNode, classNameRhs); Node classDeclaration = IR.constNode(classNameLhs, classNode).useSourceInfoIfMissingFromForTree(classNode); NodeUtil.addFeatureToScript(t.getCurrentScript(), Feature.CONST_DECLARATIONS); classDeclaration.setJSDocInfo(JSDocInfoBuilder.maybeCopyFrom(info).build());