private int countTerms(JSExpression expression) { if (expression == null) { return 0; } if (!isBoolean(expression)) { return 1; } if (expression instanceof JSBinaryExpression) { final JSBinaryExpression binaryExpression = (JSBinaryExpression) expression; final JSExpression lhs = binaryExpression.getLOperand(); final JSExpression rhs = binaryExpression.getROperand(); return countTerms(lhs) + countTerms(rhs); } else if (expression instanceof JSPrefixExpression) { final JSPrefixExpression prefixExpression = (JSPrefixExpression) expression; final JSExpression operand = prefixExpression.getExpression(); return countTerms(operand); } else if (expression instanceof JSParenthesizedExpression) { final JSParenthesizedExpression parenthesizedExpression = (JSParenthesizedExpression) expression; final JSExpression contents = parenthesizedExpression.getInnerExpression(); return countTerms(contents); } return 1; }
public static JSExpression getNegated(JSExpression condition) { if (condition instanceof JSPrefixExpression) { final JSPrefixExpression prefixExp = (JSPrefixExpression) condition; final JSExpression operand = prefixExp.getExpression(); return ParenthesesUtils.stripParentheses(operand); } else if (condition instanceof JSBinaryExpression) { final JSBinaryExpression binaryExpression = (JSBinaryExpression) condition; final IElementType sign = binaryExpression.getOperationSign(); final JSExpression lhs = binaryExpression.getLOperand(); final JSExpression rhs = binaryExpression.getROperand(); final String negatedSign = ComparisonUtils.getNegatedOperatorText(sign); final String negatedText = lhs.getText() + negatedSign + rhs.getText(); return (JSExpression) JSChangeUtil.createExpressionFromText( condition.getProject(), negatedText); } else if (condition instanceof JSParenthesizedExpression) { return getNegated(((JSParenthesizedExpression) condition).getInnerExpression()); } return condition; }
private void replaceMultiplyOrDivideWithShift(JSBinaryExpression exp) throws IncorrectOperationException { JSExpression lhs = exp.getLOperand(); JSExpression rhs = exp.getROperand(); final IElementType tokenType = exp.getOperationSign(); final String operatorString = (tokenType.equals(JSTokenTypes.MULT) ? "<<" : ">>"); if (ShiftUtils.isPowerOfTwo(lhs) && tokenType.equals(JSTokenTypes.MULT)) { JSExpression swap = lhs; lhs = rhs; rhs = swap; } final String lhsText = ParenthesesUtils.getParenthesized(lhs, ParenthesesUtils.SHIFT_PRECENDENCE); String expString = lhsText + operatorString + ShiftUtils.getLogBase2(rhs); final JSElement parent = (JSElement) exp.getParent(); if (parent != null && parent instanceof JSExpression) { if (!(parent instanceof JSParenthesizedExpression) && ParenthesesUtils.getPrecendence((JSExpression) parent) < ParenthesesUtils.SHIFT_PRECENDENCE) { expString = '(' + expString + ')'; } } JSElementFactory.replaceExpression(exp, expString); }
private void replaceShiftWithMultiplyOrDivide(JSBinaryExpression exp) throws IncorrectOperationException { final JSExpression lhs = exp.getLOperand(); final JSExpression rhs = exp.getROperand(); final IElementType tokenType = exp.getOperationSign(); final String operatorString = ((tokenType.equals(JSTokenTypes.LTLT)) ? "*" : "/"); final String lhsText = ParenthesesUtils.getParenthesized(lhs, ParenthesesUtils.MULTIPLICATIVE_PRECENDENCE); String expString = lhsText + operatorString + ShiftUtils.getExpBase2(rhs); final JSElement parent = (JSElement) exp.getParent(); if (parent != null && parent instanceof JSExpression) { if (!(parent instanceof JSParenthesizedExpression) && ParenthesesUtils.getPrecendence((JSExpression) parent) < ParenthesesUtils.MULTIPLICATIVE_PRECENDENCE) { expString = '(' + expString + ')'; } } JSElementFactory.replaceExpression(exp, expString); }
public Collection<JSDefinitionExpression> getDefinitions() { final PsiElement firstChild = getFirstChild(); if (firstChild instanceof JSDefinitionExpression) { return Collections.singletonList((JSDefinitionExpression) firstChild); } if (firstChild instanceof JSParenthesizedExpression) { final PsiElement commaExpression = PsiTreeUtil.findChildOfType(firstChild, JSCommaExpression.class); if (commaExpression != null) { return PsiTreeUtil.findChildrenOfType(commaExpression, JSDefinitionExpression.class); } } return Collections.emptyList(); }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile psiFile, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement element, @NotNull PsiElement end) throws IncorrectOperationException { // PsiElement element = descriptor.getPsiElement(); JSBinaryExpression binary = PsiTreeUtil.getParentOfType(element, JSBinaryExpression.class); JSBinaryExpression binaryClone = (JSBinaryExpression) binary.copy(); binaryClone.getLOperand().replace(binary.getLOperand().getLastChild()); ASTNode negate = JSChangeUtil.createStatementFromText(project, "!(true)"); JSParenthesizedExpression paren = PsiTreeUtil.getChildOfType(negate.getPsi().getFirstChild(), JSParenthesizedExpression.class); paren.getInnerExpression().replace(binaryClone); binary.replace(negate.getPsi()); }
public Collection<JSDefinitionExpression> getDefinitions() { final PsiElement firstChild = getFirstChild(); if (firstChild instanceof JSDefinitionExpression) { return Collections.singletonList((JSDefinitionExpression)firstChild); } else if (firstChild instanceof JSParenthesizedExpression) { final PsiElement commaExpression = PsiTreeUtil.findChildOfType(firstChild, JSCommaExpression.class); if (commaExpression != null) { return PsiTreeUtil.findChildrenOfType(commaExpression, JSDefinitionExpression.class); } } return Collections.emptyList(); }
public static boolean isLHSExpression(JSExpression expr) { if(expr instanceof JSDefinitionExpression) { expr = ((JSDefinitionExpression) expr).getExpression(); } if(expr instanceof JSReferenceExpression) { return true; } if(expr instanceof JSParenthesizedExpression) { return isLHSExpression(((JSParenthesizedExpression) expr).getInnerExpression()); } if(expr instanceof JSIndexedPropertyAccessExpression) { return true; } if(expr instanceof JSCallExpression) { return true; } if(expr instanceof JSNewExpression) { return true; } return false; }
private static boolean isCallExpression(JSExpression expression) { if (expression instanceof JSCallExpression ) { return true; } if (expression instanceof JSParenthesizedExpression) { final JSParenthesizedExpression parenthesizedExpression = (JSParenthesizedExpression) expression; final JSExpression containedExpression = parenthesizedExpression.getInnerExpression(); return isCallExpression(containedExpression); } return false; }
@Override public void visitJSParenthesizedExpression(JSParenthesizedExpression expression) { super.visitJSParenthesizedExpression(expression); checkExpression(expression); }
private static boolean expressionDefinitelyRecurses(JSExpression exp, JSFunction method) { if (exp == null) { return false; } if (exp instanceof JSNewExpression) { return RecursionUtil.newExpressionDefinitelyRecurses( (JSNewExpression) exp, method); } if (exp instanceof JSCallExpression) { return RecursionUtil.callExpressionDefinitelyRecurses( (JSCallExpression) exp, method); } if (exp instanceof JSAssignmentExpression) { return RecursionUtil.assignmentExpressionDefinitelyRecurses( (JSAssignmentExpression) exp, method); } if (exp instanceof JSArrayLiteralExpression) { return RecursionUtil.arrayLiteralExpressionDefinitelyRecurses( (JSArrayLiteralExpression) exp, method); } if (exp instanceof JSIndexedPropertyAccessExpression) { return RecursionUtil.indexedPropertyAccessExpressionDefinitelyRecurses( (JSIndexedPropertyAccessExpression) exp, method); } if (exp instanceof JSPrefixExpression) { return RecursionUtil.prefixExpressionDefinitelyRecurses( (JSPrefixExpression) exp, method); } if (exp instanceof JSPostfixExpression) { return RecursionUtil.postfixExpressionDefinitelyRecurses( (JSPostfixExpression) exp, method); } if (exp instanceof JSBinaryExpression) { return RecursionUtil.binaryExpressionDefinitelyRecurses( (JSBinaryExpression) exp, method); } if (exp instanceof JSConditionalExpression) { return RecursionUtil.conditionalExpressionDefinitelyRecurses( (JSConditionalExpression) exp, method); } if (exp instanceof JSParenthesizedExpression) { return RecursionUtil.parenthesizedExpressionDefinitelyRecurses( (JSParenthesizedExpression) exp, method); } if (exp instanceof JSReferenceExpression) { return RecursionUtil.referenceExpressionDefinitelyRecurses( (JSReferenceExpression) exp, method); } if (exp instanceof JSLiteralExpression || exp instanceof JSThisExpression) { return false; } return false; }
private static boolean parenthesizedExpressionDefinitelyRecurses( JSParenthesizedExpression expression, JSFunction method) { final JSExpression innerExpression = expression.getInnerExpression(); return RecursionUtil.expressionDefinitelyRecurses(innerExpression, method); }