private static int stringLiteralsNumber(List<ExpressionTree> operandList) { int literalsNum = 0; for (ExpressionTree operand : operandList) { if (operand.is(Kind.TEMPLATE_LITERAL, Kind.STRING_LITERAL)) { literalsNum++; } } return literalsNum; }
private static List<ExpressionTree> updatedExpressions(@Nullable ExpressionTree expression) { if (expression == null) { return ImmutableList.<ExpressionTree>of(); } UpdateVisitor visitor = new UpdateVisitor(); expression.accept(visitor); return visitor.updatedExpressions; }
private static boolean isRemovableReturn(StatementTree tree) { if (tree.is(Kind.RETURN_STATEMENT)) { ExpressionTree expression = ((ReturnStatementTree) tree).expression(); if (expression != null && !expression.is(Kind.OBJECT_LITERAL)) { int firstLine = expression.firstToken().line(); int lastLine = expression.lastToken().line(); return firstLine == lastLine; } } return false; }
@Nullable private static FunctionTree getFunction(CallExpressionTree tree) { Set<Type> types = tree.callee().types(); if (types.size() == 1 && types.iterator().next().kind().equals(Type.Kind.FUNCTION)) { return ((FunctionType) types.iterator().next()).functionTree(); } return null; }
@Override public void visitCallExpression(CallExpressionTree tree) { if (!isChainedCall(tree) && callExpressionWasLikelyIntended(tree.argumentClause())) { int calleeLastLine = tree.callee().lastToken().endLine(); int argumentsFirstLine = tree.argumentClause().firstToken().line(); if (calleeLastLine != argumentsFirstLine) { addIssue(tree.argumentClause(), String.format(MESSAGE, calleeLastLine)); } } super.visitCallExpression(tree); }
@CheckForNull private static FunctionTree functionDeclaration(CallExpressionTree tree) { TypeSet types = tree.callee().types(); FunctionType functionType = (FunctionType) types.getUniqueType(Type.Kind.FUNCTION); return functionType == null ? null : functionType.functionTree(); }
private static int checkForUpdateByOne(ExpressionTree update) { if (update.is(POSTFIX_INCREMENT, PREFIX_INCREMENT) || (update.is(PLUS_ASSIGNMENT) && isUpdateOnOneWithAssign(update))) { return +1; } if (update.is(POSTFIX_DECREMENT, PREFIX_DECREMENT) || (update.is(MINUS_ASSIGNMENT) && isUpdateOnOneWithAssign(update))) { return -1; } return 0; }
@Override public void visitParenthesisedExpression(ParenthesisedExpressionTree tree) { super.visitParenthesisedExpression(tree); ((ParenthesisedExpressionTreeImpl) tree).addTypes(tree.expression().types()); }
private List<ExpressionTree> updatedExpressions(ExpressionTree expression) { if (expression == null) { return ImmutableList.<ExpressionTree>of(); } UpdateVisitor visitor = new UpdateVisitor(); expression.accept(visitor); return visitor.updatedExpressions; }
private int checkForUpdateByOne(ExpressionTree update) { if (update.is(Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_INCREMENT) || (update.is(Tree.Kind.PLUS_ASSIGNMENT) && isUpdateOnOneWithAssign(update))) { return +1; } if (update.is(Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT) || (update.is(Tree.Kind.MINUS_ASSIGNMENT) && isUpdateOnOneWithAssign(update))) { return -1; } return 0; }
@Override public void visitNewExpression(NewExpressionTree tree) { ExpressionTree expression = tree.expression(); if (!expression.types().isEmpty() && isNotConstructor(expression.types())) { getContext().addIssue(this, expression, String.format("Replace %s with a constructor function.", CheckUtils.asString(expression))); } super.visitNewExpression(tree); }
@Override public void visitForStatement(ForStatementTree forStatement) { List<ExpressionTree> updatedExpressions = updatedExpressions(forStatement.update()); ExpressionTree condition = forStatement.condition(); if (!updatedExpressions.isEmpty() && condition != null) { ConditionVisitor conditionVisitor = new ConditionVisitor(updatedExpressions); condition.accept(conditionVisitor); if (!conditionVisitor.foundUpdatedExpression) { String updated = expressionList(updatedExpressions); String tested = expressionList(conditionVisitor.testedExpressions); String message = String.format(MESSAGE, tested, updated); addIssue(forStatement.forKeyword(), message); } } super.visitForStatement(forStatement); }
@CheckForNull private static ForLoopIncrement increment(ExpressionTree incrementTree, ExpressionTree expression, Double value) { if (expression.is(Tree.Kind.IDENTIFIER_REFERENCE)) { return new ForLoopIncrement(incrementTree, (IdentifierTree) expression, value); } return null; }
public static boolean isElementList(ExpressionTree tree) { if (tree.is(Tree.Kind.CALL_EXPRESSION) && ((CallExpressionTree) tree).callee().is(Tree.Kind.DOT_MEMBER_EXPRESSION)) { DotMemberExpressionTree callee = (DotMemberExpressionTree) ((CallExpressionTree) tree).callee(); if (callee.object().types().contains(Type.Kind.DOCUMENT) && DOCUMENT_METHODS_TO_GET_ELEMENTS.contains(callee.property().name())) { return true; } } return false; }
private void inferType(Tree identifier, ExpressionTree assignedTree) { super.scan(assignedTree); if (identifier instanceof IdentifierTree) { Symbol symbol = ((IdentifierTree) identifier).symbol(); if (symbol != null) { addTypes(symbol, assignedTree.types()); } } }
@Override public void visitForStatement(ForStatementTree forStatement) { List<ExpressionTree> updatedExpressions = updatedExpressions(forStatement.update()); ExpressionTree condition = forStatement.condition(); if (!updatedExpressions.isEmpty() && condition != null) { ConditionVisitor conditionVisitor = new ConditionVisitor(updatedExpressions); condition.accept(conditionVisitor); if (!conditionVisitor.foundUpdatedExpression) { String updated = expressionList(updatedExpressions); String tested = expressionList(conditionVisitor.testedExpressions); String message = String.format("This loop's stop condition tests \"%s\" but the incrementer updates \"%s\".", tested, updated); getContext().addIssue(this, forStatement, message); } } super.visitForStatement(forStatement); }
private static boolean propertyIsDeprecated(ExpressionTree property, List<String> deprecated) { if (property.is(Tree.Kind.PROPERTY_IDENTIFIER)) { IdentifierTree identifier = (IdentifierTree) property; return deprecated.contains(identifier.name()); } return false; } }