public boolean isConstDeclaration() { return getParent().isConst(); }
boolean isConstDeclaration() { return getParent().isConst(); }
/** * Is this node a name declaration? * * @param n The node * @return True if {@code n} is VAR, LET or CONST */ public static boolean isNameDeclaration(Node n) { return n.isVar() || n.isLet() || n.isConst(); }
private boolean isDeclaredConst(Node lhs, JSDocInfo info) { if (info != null && info.hasConstAnnotation()) { return true; } return lhs.getParent().isConst(); }
/** * Is this node a name declaration? * * @param n The node * @return True if {@code n} is VAR, LET or CONST */ public static boolean isNameDeclaration(Node n) { return n != null && (n.isVar() || n.isLet() || n.isConst()); }
private void varify() { if (!letConsts.isEmpty()) { for (Node n : letConsts) { if (n.isConst()) { handleDeclarationList(n, n.getParent()); } n.setType(Token.VAR); } compiler.reportCodeChange(); } }
public static Node forNode(Node init, Node cond, Node incr, Node body) { checkState(init.isVar() || init.isLet() || init.isConst() || mayBeExpressionOrEmpty(init)); checkState(mayBeExpressionOrEmpty(cond)); checkState(mayBeExpressionOrEmpty(incr)); checkState(body.isBlock()); return new Node(Token.FOR, init, cond, incr, body); }
private void rewriteDeclsToVars() { if (!letConsts.isEmpty()) { for (Node n : letConsts) { if (n.isConst()) { handleDeclarationList(n, n.getParent()); } n.setToken(Token.VAR); compiler.reportChangeToEnclosingScope(n); } } }
/** * Detect any block-scoped declarations that are younger siblings of the given starting point. * * @param start The start point */ private static boolean hasBlockScopedVarsFollowing(Node start) { for (Node n = start.getNext(); n != null; n = n.getNext()) { if (n.isLet() || n.isConst()) { return true; } } return false; } }
/** * This is similar to NodeUtil.isLValue except that object properties and * array member modification aren't important ("o" in "o.a = 2" is still "o" * after assignment, where in as "o = x", "o" is now "x"). * * This also looks for the redefinition of a name. * function (x) {var x;} * * @param n The NAME node in question. * @param parent The parent of the node. */ private static boolean canNameValueChange(Node n) { return NodeUtil.isLValue(n) && !NodeUtil.getEnclosingStatement(n).isConst() && !NodeUtil.getEnclosingStatement(n).isLet(); }
/** * Gets the parent node of the value for any assignment to a Name. * For example, in the assignment * {@code var x = 3;} * the parent would be the NAME node. */ private static Node getValueParent(Ref ref) { // there are two types of declarations: VARs, ASSIGNs, and CONSTs return ref.node.getParent() != null && (ref.node.getParent().isVar() || ref.node.getParent().isConst()) ? ref.node : ref.node.getParent(); }
static boolean isConstToBeInferred(Node nameNode) { JSDocInfo jsdoc = NodeUtil.getBestJSDocInfo(nameNode); boolean isConst = nameNode.getParent().isConst() || isExportLhs(nameNode) || (jsdoc != null && jsdoc.isConstant()); return isConst && !JsdocUtil.hasAnnotatedType(jsdoc) && !NodeUtil.isNamespaceDecl(nameNode); }
/** Matches destructing from a variable ie `const {foo, bar: baz} = quux;` */ protected static boolean isVariableDestructuringAssignment(Node statement) { if (!(statement.isConst() || statement.isVar() || statement.isLet())) { return false; } if (!statement.getFirstChild().isDestructuringLhs()) { return false; } Node destructuringAssignment = statement.getFirstChild(); Node rightHandSide = destructuringAssignment.getChildAtIndex(1); return rightHandSide.isName(); }
private static void maybeAddConstJSDoc(Node srcDeclaration, Node srcParent, Node srcName, Node destDeclaration) { if (srcDeclaration.isConst() // Don't add @const for the left side of a for/in. If we do we get warnings from the NTI. // TODO(lharker): Check if this condition is still necessary, since NTI is deleted && !(srcParent.isForIn() && srcDeclaration == srcParent.getFirstChild())) { extractInlineJSDoc(srcDeclaration, srcName, destDeclaration); JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(destDeclaration.getJSDocInfo()); builder.recordConstancy(); destDeclaration.setJSDocInfo(builder.build()); } }
private static void maybeAddConstJSDoc(Node srcDeclaration, Node srcParent, Node srcName, Node destDeclaration) { if (srcDeclaration.isConst() // Don't add @const for the left side of a for/in. If we do we get warnings from the NTI. && !(NodeUtil.isForIn(srcParent) && srcDeclaration == srcParent.getFirstChild())) { extractInlineJSDoc(srcDeclaration, srcName, destDeclaration); JSDocInfoBuilder builder = JSDocInfoBuilder.maybeCopyFrom(destDeclaration.getJSDocInfo()); builder.recordConstancy(); destDeclaration.setJSDocInfo(builder.build()); } }
Node processLabeledStatement(LabelledStatementTree labelTree) { Node statement = transform(labelTree.statement); if (statement.isFunction() || statement.isClass() || statement.isLet() || statement.isConst()) { errorReporter.error( "Lexical declarations are only allowed at top level or inside a block.", sourceName, lineno(labelTree), charno(labelTree)); return statement; // drop the LABEL node so that the resulting AST is valid } return newNode(Token.LABEL, transformLabelName(labelTree.name), statement); }
/** * Matches either `const foo = goog.require()` or `const foo = goog.module.get()` or `const foo = * goog.forwardDeclare()` depending on if statement is in a goog.module or a goog.scope. */ protected static boolean isImportAssignment(Node statement) { if (!(statement.isConst() || statement.isVar() || statement.isLet())) { return false; } Node rightHandSide = statement.getFirstFirstChild(); return rightHandSide != null && rightHandSide.isCall() && (rightHandSide.getFirstChild().matchesQualifiedName("goog.require") || rightHandSide.getFirstChild().matchesQualifiedName("goog.module.get") || rightHandSide.getFirstChild().matchesQualifiedName("goog.forwardDeclare")); }
/** * Matches either `const {foo} = goog.require()` or `const {foo} = goog.module.get()` depending on * if statement is in a goog.module or a goog.scope. */ protected static boolean isImportDestructuringAssignment(Node statement) { if (!(statement.isConst() || statement.isVar() || statement.isLet())) { return false; } if (!statement.getFirstChild().isDestructuringLhs()) { return false; } Node destructuringAssignment = statement.getFirstChild(); Node rightHandSide = destructuringAssignment.getChildAtIndex(1); return rightHandSide.isCall() && (rightHandSide.getFirstChild().matchesQualifiedName("goog.require") || rightHandSide.getFirstChild().matchesQualifiedName("goog.module.get")); }
private void rewriteClassDefinition(Node node, Node parent, NodeTraversal traversal) { Node grandparent = parent.getParent(); if (grandparent.isConst()) { compiler.report(JSError.make(node, PolymerPassErrors.POLYMER_INVALID_DECLARATION)); return; } PolymerClassDefinition def = PolymerClassDefinition.extractFromCallNode( node, compiler, globalNames); if (def != null) { if (def.nativeBaseElement != null) { appendPolymerElementExterns(def); } PolymerClassRewriter rewriter = new PolymerClassRewriter(compiler, polymerElementExterns); if (NodeUtil.isNameDeclaration(grandparent) || parent.isAssign()) { rewriter.rewritePolymerClass(grandparent, def, traversal.getScope().isGlobal()); } else { rewriter.rewritePolymerClass(parent, def, traversal.getScope().isGlobal()); } } }
private void considerVar(Var v, ReferenceCollection refCollection) { Node nameNode = v.getNameNode(); JSDocInfo docInfo = v.getJSDocInfo(); if (docInfo != null && docInfo.isConstant()) { nameNode.putBooleanProp(Node.IS_CONSTANT_VAR, true); } else if (nameNode != null && nameNode.getParent().isConst()) { nameNode.putBooleanProp(Node.IS_CONSTANT_VAR, true); } else if (nameNode != null && compiler.getCodingConvention().isConstant(nameNode.getString())) { nameNode.putBooleanProp(Node.IS_CONSTANT_VAR, true); } else if (nameNode != null && refCollection != null && refCollection.isWellDefined() && refCollection.isAssignedOnceInLifetime() && refCollection.firstReferenceIsAssigningDeclaration()) { nameNode.putBooleanProp(Node.IS_CONSTANT_VAR, true); } } }