private static String getLastPartOfQualifiedName(Node n) { if (n.isName()) { return n.getString(); } else if (n.isGetProp()) { return n.getLastChild().getString(); } return null; }
static boolean isEnumDecl(Node n) { if (n.isVar() || n.isName() && n.getParent().isVar() || (n.isGetProp() && n.getParent().isAssign() && n.getGrandparent().isExprResult()) || (n.isAssign() && n.getParent().isExprResult())) { JSDocInfo jsdoc = getBestJSDocInfo(n); return jsdoc != null && jsdoc.hasEnumParameterType(); } return false; }
/** 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 boolean isExportLhs(Node lhs) { return (lhs.isName() && lhs.matchesQualifiedName("exports")) || (lhs.isGetProp() && lhs.getFirstChild().matchesQualifiedName("exports")) || lhs.matchesQualifiedName("module.exports"); }
@Override public boolean preventsRemovalOfVariableWithNonLocalValueOrPrototype() { if (targetNode.isGetProp()) { Node getPropLhs = targetNode.getFirstChild(); // assignment to varName.property or varName.prototype.property // cannot be removed unless varName and varName.prototype have literal, local values. return getPropLhs.isName() || isNameDotPrototype(getPropLhs); } else { return false; } }
static boolean isPrototypeAssignment(Node getProp) { if (!getProp.isGetProp()) { return false; } Node parent = getProp.getParent(); return parent.isAssign() && parent.getFirstChild() == getProp && parent.getFirstChild().getLastChild().getString().equals("prototype"); }
private boolean isFunctionDecl(Node n) { return n.isFunction() || (n.isVar() && n.getFirstFirstChild() != null && n.getFirstFirstChild().isFunction()) || n.isAssign() && n.getFirstChild().isQualifiedName() && n.getLastChild().isFunction(); }
static boolean isNaN(Node n) { return (n.isName() && n.getString().equals("NaN")) || (n.getToken() == Token.DIV && n.getFirstChild().isNumber() && n.getFirstChild().getDouble() == 0 && n.getLastChild().isNumber() && n.getLastChild().getDouble() == 0) || n.matchesQualifiedName(NUMBER_NAN); }
@Override public void visit(Node node) { if ((node.isName() && (node.getString().equals(MODULE) || node.getString().equals("define"))) || (node.isGetProp() && node.matchesQualifiedName("window.define")) || (node.isString() && node.getString().equals("amd") && node.getParent().isIn())) { umdTests.add(node); } } });
private void visitQualifiedName(NodeTraversal t, Node n, Node parent) { checkState(n.isName() || n.isGetProp() || n.isStringKey(), n); String qualifiedName = n.isStringKey() ? n.getString() : n.getQualifiedName(); addWeakUsagesOfAllPrefixes(qualifiedName); if (mode != Mode.SINGLE_FILE) { // TODO(b/71638622): Fix violations and remove this check. return; } if (!n.isStringKey() && !NodeUtil.isLhsOfAssign(n) && !parent.isExprResult()) { addUsageOfOutermostClassName(n, t); } }
static String getIdForGeneratorNode(boolean consistent, Node n) { checkState(n.isString() || n.isStringKey(), n); if (consistent) { return n.getString(); } else { return n.getSourceFileName() + ':' + n.getLineno() + ":" + n.getCharno(); } } }
/** * Whether the provided node acts as the target function in a call expression including * .call expressions. For example, returns true for 'x' in 'x.call()'. */ static boolean isCallTarget(Node n) { Node parent = n.getParent(); return ((parent.getFirstChild() == n) && parent.isCall()) || (parent.isGetProp() && parent.getParent().isCall() && parent.getLastChild().getString().equals("call")); }
boolean isInstrumentation(Node n) { //if (n == null) // return false; if (n.getSourceFileName() == null) return true; Node child = n.getFirstChild(); return child != null && child.isGetProp() && child.getFirstChild().isName() && child.getFirstChild().getString().equals("_$jscoverage"); }
private static boolean isClassPropertyDeclaration(Node n, NTIScope s) { Node parent = n.getParent(); return n.isGetProp() && n.getFirstChild().isThis() && (parent.isAssign() && parent.getFirstChild().equals(n) || parent.isExprResult()) && (s.isConstructor() || s.isPrototypeMethod()); }
private static boolean mightContainForwardReference(Node node, String varName) { if (node.isName()) { return varName.equals(node.getString()); } for (Node child = node.getFirstChild(); child != null; child = child.getNext()) { if (mightContainForwardReference(child, varName)) { return true; } } return false; }
private boolean isVarOrAssignExprLhs(Node n) { Node parent = n.getParent(); return parent.isVar() || (parent.isAssign() && parent.getFirstChild() == n && parent.getParent().isExprResult()); }
/** * Matches literal value declarations {@code var foo = 3;} or stub declarations like * {@code var bar;}. */ private static boolean isClassFieldDeclaration(Node node) { return node.getParent().isScript() && node.isVar() && (!node.getFirstChild().hasChildren() || (node.getFirstFirstChild() != null && NodeUtil.isLiteralValue( node.getFirstFirstChild(), false /* includeFunctions */))); }
private void checkShortGoogRequireCall(NodeTraversal t, Node callNode, Node declaration) { if (declaration.isLet()) { t.report(declaration, LET_GOOG_REQUIRE); } if (declaration.getChildCount() != 1) { t.report(declaration, ONE_REQUIRE_PER_DECLARATION); } Node lhs = declaration.getFirstChild(); String shortName = lhs.isName() ? lhs.getString() : null; shortRequiredNamespaces.put(extractFirstArgumentName(callNode), shortName); } }
/** * Heuristic method to check whether symbol was created by DeclaredGlobalExternsOnWindow.java * pass. */ private boolean isSymbolDuplicatedExternOnWindow(Symbol symbol) { Node node = symbol.getDeclarationNode(); // Check that node is of type "window.foo"; return !node.isIndexable() && node.isGetProp() && node.getFirstChild().isName() && node.getFirstChild().getString().equals("window"); }