private static boolean hasWindowLikeName(ExpressionTree tree) { String str = CheckUtils.asString(tree); return str.contains("window") || str.contains("Window"); }
public int getAverageLineLength() { long nbLines = 0; long nbCharacters = 0; List<String> lines = CheckUtils.readLines(new JavaScriptFileImpl(file)); for (String line : lines) { if (!isLineInHeaderComment(line)) { nbLines++; nbCharacters += line.length(); } } return nbLines > 0 ? (int) (nbCharacters / nbLines) : 0; }
public static String asString(Tree tree) { List<Token> tokens = ((JavaScriptTree) tree).getTokens(); StringBuilder sb = new StringBuilder(); Token prevToken = null; for (Token token : tokens) { if (prevToken != null && !areAdjacent(prevToken, token)) { sb.append(" "); } sb.append(token.getOriginalValue()); prevToken = token; } return sb.toString(); }
private static void appendChild(StringBuilder sb, @Nullable SyntaxToken prevToken, Tree child) { if (prevToken != null) { SyntaxToken firstToken = child.firstToken(); if (isSpaceRequired(prevToken, firstToken)) { sb.append(" "); } } sb.append(asString(child)); }
private static String getCalleeName(CallExpressionTree callExpression) { ExpressionTree callee = CheckUtils.removeParenthesis(callExpression.callee()); if (callee.is(Kind.DOT_MEMBER_EXPRESSION)) { return ((DotMemberExpressionTree) callee).property().name(); } else { return CheckUtils.asString(callee); } }
private static Optional<ClassTree> getEnclosingType(Tree tree) { final Tree classBoundary = CheckUtils.getFirstAncestor(tree, Kind.CLASS_DECLARATION, Kind.CLASS_EXPRESSION, Kind.OBJECT_LITERAL); if (classBoundary.is(Kind.OBJECT_LITERAL)) { return Optional.empty(); } else { return Optional.ofNullable((ClassTree) classBoundary); } }
private static boolean canExecuteMoreThanOnce(IterationStatementTree loopTree) { ControlFlowGraph cfg = CheckUtils.buildControlFlowGraph(loopTree); for (CfgBranchingBlock block : Iterables.filter(cfg.blocks(), CfgBranchingBlock.class)) { if (loopTree.equals(block.branchingTree()) && hasPredecessorInsideLoopBody(block, loopTree)) { return true; } } return false; }
@Override public void leaveNode(AstNode astNode) { if (CheckUtils.isFunction(astNode)) { stack.pop(); } }
@Override public void leaveNode(AstNode astNode) { if (CheckUtils.isIterationStatement(astNode)) { stack.push(stack.pop() - 1); } else { stack.pop(); } }
@Override public void init() { subscribeTo(CheckUtils.functionNodesArray()); }
private void checkSymbol(Symbol symbol, IdentifierTree declarationIdentifier, @Nullable ForObjectStatementTree loop, String title) { for (Usage usage : symbol.usages()) { if (usage.isWrite() && !usage.identifierTree().equals(declarationIdentifier) && (loop == null || CheckUtils.isDescendant(usage.identifierTree(), loop))) { addIssue(usage.identifierTree(), String.format(MESSAGE, title, symbol.name())); } } }
public static String asString(Tree tree) { if (tree.is(Kind.TOKEN)) { return ((SyntaxToken) tree).text(); } else { StringBuilder sb = new StringBuilder(); Iterator<Tree> treeIterator = ((JavaScriptTree) tree).childrenIterator(); SyntaxToken prevToken = null; while (treeIterator.hasNext()) { Tree child = treeIterator.next(); if (child != null) { appendChild(sb, prevToken, child); prevToken = child.lastToken(); } } return sb.toString(); } }
@Override public void visitNewExpression(NewExpressionTree tree) { ExpressionTree expression = tree.expression(); if (!expression.types().isEmpty() && !isConstructor(expression.types())) { Tree primaryLocationTree = expression; String expressionStr = CheckUtils.asString(expression); ExpressionTree unwrapped = CheckUtils.removeParenthesis(expression); if (unwrapped.is(Tree.Kind.FUNCTION_EXPRESSION)) { primaryLocationTree = ((FunctionExpressionTree) unwrapped).functionKeyword(); expressionStr = "this function"; } addIssue(primaryLocationTree, String.format(MESSAGE, expressionStr)) .secondary(tree.newKeyword()); } super.visitNewExpression(tree); }
@CheckForNull private static MethodDeclarationTree getEnclosingConstructor(Tree tree) { FunctionTree function = (FunctionTree) CheckUtils.getFirstAncestor(tree, KindSet.FUNCTION_KINDS); if (function != null && isConstructor(function)) { return (MethodDeclarationTree) function; } return null; }
private static Map<CaseClauseTree, CfgBranchingBlock> caseClauseBlocksByTree(SwitchStatementTree switchTree) { ControlFlowGraph cfg = CheckUtils.buildControlFlowGraph(switchTree); Map<CaseClauseTree, CfgBranchingBlock> map = new HashMap<>(); for (CfgBlock block : cfg.blocks()) { if (block instanceof CfgBranchingBlock) { CfgBranchingBlock branchingBlock = (CfgBranchingBlock) block; Tree branchingTree = branchingBlock.branchingTree(); if (branchingTree.is(Kind.CASE_CLAUSE)) { map.put((CaseClauseTree) branchingTree, branchingBlock); } } } return map; }
@Override public void leaveNode(AstNode astNode) { if (CheckUtils.isFunction(astNode)) { stack.pop(); } }
@Override public void visitNode(AstNode astNode) { if (CheckUtils.isIterationStatement(astNode)) { stack.push(stack.pop() + 1); } else { if (stack.peek() > 0) { getContext().createLineViolation(this, "Define this function outside of a loop.", astNode); } stack.add(0); } }
@Override public void init() { subscribeTo( Kind.VAR_DECLARATION, Kind.LET_DECLARATION, Kind.CONST_DECLARATION); subscribeTo(CheckUtils.functionNodesArray()); }
private static boolean hasPredecessorInsideLoopBody(CfgBranchingBlock conditionBlock, IterationStatementTree loopTree) { for (CfgBlock loopPredecessor : conditionBlock.predecessors()) { List<Tree> predecessorElements = loopPredecessor.elements(); Tree predecessorLastElement = predecessorElements.get(predecessorElements.size() - 1); if (loopTree.is(Kind.FOR_STATEMENT)) { ForStatementTree forTree = (ForStatementTree) loopTree; if (forTree.update() != null && forTree.update().equals(predecessorLastElement)) { return !loopPredecessor.predecessors().isEmpty(); } } StatementTree loopBody = loopTree.statement(); if (isDescendant(predecessorLastElement, loopBody)) { return true; } } return false; }
private static boolean hasWindowLikeName(ExpressionTree tree) { String str = CheckUtils.asString(tree); return str.contains("window") || str.contains("Window"); }