private static List<ExpressionTree> getAllSubExpressions(BinaryExpressionTree tree) { List<ExpressionTree> result = new LinkedList<>(); result.add(tree.rightOperand()); ExpressionTree currentExpression = tree.leftOperand(); while (currentExpression.is(Kind.COMMA_OPERATOR)) { result.add(((BinaryExpressionTree) currentExpression).rightOperand()); currentExpression = ((BinaryExpressionTree) currentExpression).leftOperand(); } result.add(currentExpression); return result; }
private List<ExpressionTree> getAllSubExpressions(BinaryExpressionTree tree) { List<ExpressionTree> result = new LinkedList<>(); result.add(tree.rightOperand()); ExpressionTree currentExpression = tree.leftOperand(); while (currentExpression.is(Kind.COMMA_OPERATOR)) { result.add(((BinaryExpressionTree) currentExpression).rightOperand()); currentExpression = ((BinaryExpressionTree) currentExpression).leftOperand(); } result.add(currentExpression); return result; }
@CheckForNull private static ExpressionTree getNaN(BinaryExpressionTree expression) { if (isNaN(expression.leftOperand())) { return expression.leftOperand(); } else if (isNaN(expression.rightOperand())) { return expression.rightOperand(); } return null; }
private static boolean hasOneSymbolLiteralOperand(BinaryExpressionTree expression) { LiteralTree literal = null; if (expression.leftOperand().is(Kind.STRING_LITERAL)) { literal = (LiteralTree) expression.leftOperand(); } else if (expression.rightOperand().is(Kind.STRING_LITERAL)) { literal = (LiteralTree) expression.rightOperand(); } return literal != null && literal.value().length() == 3; } }
@Override public void visitNode(Tree tree) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree; if (!isNullLiteral(binaryExpressionTree.leftOperand()) && !isNullLiteral(binaryExpressionTree.rightOperand())) { equalityExpressions.add(binaryExpressionTree); } }
private static Tree biggestTreeWithSameTruthiness(Tree condition, boolean isTruthy, Set<Tree> neverExecutedCode) { Tree parent = CheckUtils.parentIgnoreParentheses(condition); if ((parent.is(Kind.CONDITIONAL_OR) && isTruthy) || (parent.is(Kind.CONDITIONAL_AND) && !isTruthy)) { BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) parent; if (binaryExpressionTree.leftOperand().equals(condition)) { neverExecutedCode.add(binaryExpressionTree.rightOperand()); } return biggestTreeWithSameTruthiness(parent, isTruthy, neverExecutedCode); } return condition; }
private static ExpressionTree getNonNullLiteralOperand(BinaryExpressionTree binaryExpressionTree) { if (isNullOrUndefined(binaryExpressionTree.leftOperand())) { return binaryExpressionTree.rightOperand(); } return binaryExpressionTree.leftOperand(); }
@Override public void visitNode(Tree tree) { if (lonelyBitwiseAndOr == null && tree.is(Kind.BITWISE_AND, Kind.BITWISE_OR) && !((BinaryExpressionTree) tree).rightOperand().is(Kind.NUMERIC_LITERAL)) { lonelyBitwiseAndOr = ((BinaryExpressionTree) tree).operatorToken(); } else { fileContainsBitwiseOperations = true; } }
@Override boolean isProblem(BinaryExpressionTree tree, ProgramState currentState) { ExpressionTree onlyStringOperand = getOnlyStringOperand(tree.leftOperand(), tree.rightOperand(), currentState); return onlyStringOperand != null && onlyStringOperand.is(Kind.IDENTIFIER_REFERENCE); }
@Override public void endOfFile(ScriptTree scriptTree) { EqualityVisitor equalityVisitor = new EqualityVisitor(); equalityVisitor.scanTree(scriptTree); equalityVisitor.equalityExpressions .stream() .filter(equalityExpression -> !ignoredList.contains(equalityExpression)) .forEach(equalityExpression -> addIssue(equalityExpression.operatorToken(), equalityExpression.is(Kind.EQUAL_TO) ? "Replace \"==\" with \"===\"." : "Replace \"!=\" with \"!==\".") .secondary(equalityExpression.leftOperand()) .secondary(equalityExpression.rightOperand())); }
@Override public void visitNode(Tree tree) { BinaryExpressionTree expression = (BinaryExpressionTree) tree; if (isZero(expression.rightOperand()) && isIndexOfCall(expression.leftOperand())) { addIssue(tree, MESSAGE); } }
private boolean isNullComparison(ExpressionTree expression, Tree.Kind kind1, Tree.Kind kind2) { ExpressionTree tree = removeParenthesis(expression); if (tree.is(kind1, kind2)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) tree; return isNullOrUndefined(binaryExp.leftOperand()) || isNullOrUndefined(binaryExp.rightOperand()); } return false; }
private static boolean isNullComparison(ExpressionTree expression, Tree.Kind kind1, Tree.Kind kind2) { ExpressionTree tree = CheckUtils.removeParenthesis(expression); if (tree.is(kind1, kind2)) { BinaryExpressionTree binaryExp = (BinaryExpressionTree) tree; return isNullOrUndefined(binaryExp.leftOperand()) || isNullOrUndefined(binaryExp.rightOperand()); } return false; }
@Override void raiseIssue(BinaryExpressionTree tree) { addIssue(tree.operatorToken(), MESSAGE) .secondary(tree.leftOperand()) .secondary(tree.rightOperand()); }
@Override void raiseIssue(BinaryExpressionTree tree) { if (!hasOneSymbolLiteralOperand(tree)) { String message = String.format(MESSAGE, tree.operatorToken().text()); addIssue(tree.operatorToken(), message) .secondary(tree.leftOperand()) .secondary(tree.rightOperand()); } }
private void raiseIssue(BinaryExpressionTree tree) { String message = tree.is(Kind.STRICT_EQUAL_TO) ? MESSAGE_EQUAL : MESSAGE_NOT_EQUAL; addIssue(tree.operatorToken(), message) .secondary(tree.leftOperand()) .secondary(tree.rightOperand()); } }
private static boolean isUpdateIncDec(ExpressionTree update) { boolean result = false; if (update.is(COMMA_OPERATOR)) { BinaryExpressionTree commaExpressions = (BinaryExpressionTree) update; result = isUpdateIncDec(commaExpressions.leftOperand()) && isUpdateIncDec(commaExpressions.rightOperand()); } else if (update.is(INC_DEC_KINDS) || update.is(PLUS_ASSIGNMENT, MINUS_ASSIGNMENT)) { result = true; } return result; }
@Override public void beforeBlockElement(ProgramState currentState, Tree element, ProgramPoint programPoint) { if (element.is(Kind.RELATIONAL_IN)) { SymbolicValue rightOperandValue = currentState.peekStack(); Constraint rightOperandConstraint = currentState.getConstraint(rightOperandValue); if (rightOperandConstraint.isIncompatibleWith(Constraint.OBJECT)) { BinaryExpressionTree inOperation = (BinaryExpressionTree) element; addUniqueIssue(inOperation.rightOperand(), MESSAGE, new IssueLocation(inOperation.operatorToken())); } } }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (isAndWithEqualToNull(tree) || isOrWithNonEqualToNull(tree)) { BinaryExpressionTree leftOperand = (BinaryExpressionTree) CheckUtils.removeParenthesis(tree.leftOperand()); ExpressionTree expression = CheckUtils.removeParenthesis(getNonNullLiteralOperand(leftOperand)); tree.rightOperand().accept(new NullExpressionUsageVisitor(expression, this)); } super.visitBinaryExpression(tree); }
@Override public void visitBinaryExpression(BinaryExpressionTree tree) { if (!isNullLiteral(tree.leftOperand()) && !isNullLiteral(tree.rightOperand())) { if (tree.is(Tree.Kind.EQUAL_TO)) { getContext().addIssue(this, tree.operator(), "Replace \"==\" with \"===\"."); } else if (tree.is(Tree.Kind.NOT_EQUAL_TO)) { getContext().addIssue(this, tree.operator(), "Replace \"!=\" with \"!==\"."); } } super.visitBinaryExpression(tree); }