public void postVisit(ASTNode node) {
   if (!(node instanceof ParenthesizedExpression)) {
     return;
   }
   ParenthesizedExpression parenthesizedExpression = (ParenthesizedExpression) node;
   Expression expression = parenthesizedExpression.getExpression();
   while (expression instanceof ParenthesizedExpression) {
     expression = ((ParenthesizedExpression) expression).getExpression();
   }
   // if this is part of another expression, check for this and parent precedences
   if (parenthesizedExpression.getParent() instanceof Expression) {
     Expression parentExpression = (Expression) parenthesizedExpression.getParent();
     int expressionPrecedence = getExpressionPrecedence(expression);
     int parentPrecedence = getExpressionPrecedence(parentExpression);
     if ((expressionPrecedence > parentPrecedence)
         && !(parenthesizedExpression.getParent() instanceof ParenthesizedExpression)) {
       return;
     }
     // check for case when precedences for expression and parent are same
     if ((expressionPrecedence == parentPrecedence)
         && (parentExpression instanceof InfixExpression)) {
       // we have expr infix (expr infix expr) removing the parenthesis is equal to (expr infix
       // expr) infix expr
       InfixExpression parentInfix = (InfixExpression) parentExpression;
       Operator parentOperator = parentInfix.getOperator();
       if (parentInfix.getLeftOperand() == parenthesizedExpression) {
         fNodes.add(node);
       } else if (isAssoziative(parentOperator)) {
         if (parentOperator == InfixExpression.Operator.PLUS) {
           if (isStringExpression(parentInfix.getLeftOperand())
               || isStringExpression(parentInfix.getRightOperand())) {
             return;
           }
           for (Iterator J = parentInfix.extendedOperands().iterator(); J.hasNext(); ) {
             Expression operand = (Expression) J.next();
             if (isStringExpression(operand)) {
               return;
             }
           }
         }
         fNodes.add(node);
       }
       return;
     } else if (expressionPrecedence == parentPrecedence
         && parentExpression instanceof ConditionalExpression) {
       if (((ConditionalExpression) parentExpression).getElseExpression()
           != parenthesizedExpression) return;
     }
   }
   fNodes.add(node);
 }
    /** {@inheritDoc} */
    public void rewriteAST(CompilationUnitRewrite cuRewrite, List textEditGroups)
        throws CoreException {
      TextEditGroup group =
          createTextEditGroup(FixMessages.ExpressionsFix_removeUnnecessaryParenthesis_description);
      textEditGroups.add(group);

      ASTRewrite rewrite = cuRewrite.getASTRewrite();

      while (fExpressions.size() > 0) {
        ParenthesizedExpression parenthesizedExpression =
            (ParenthesizedExpression) fExpressions.iterator().next();
        fExpressions.remove(parenthesizedExpression);
        ParenthesizedExpression down = parenthesizedExpression;
        while (fExpressions.contains(down.getExpression())) {
          down = (ParenthesizedExpression) down.getExpression();
          fExpressions.remove(down);
        }

        ASTNode move = rewrite.createMoveTarget(down.getExpression());

        ParenthesizedExpression top = parenthesizedExpression;
        while (fExpressions.contains(top.getParent())) {
          top = (ParenthesizedExpression) top.getParent();
          fExpressions.remove(top);
        }

        rewrite.replace(top, move, group);
      }
    }
    /** {@inheritDoc} */
    public void rewriteAST(CompilationUnitRewrite cuRewrite, List textEditGroups)
        throws CoreException {
      TextEditGroup group =
          createTextEditGroup(FixMessages.ExpressionsFix_addParanoiacParenthesis_description);
      textEditGroups.add(group);

      ASTRewrite rewrite = cuRewrite.getASTRewrite();
      AST ast = cuRewrite.getRoot().getAST();

      for (int i = 0; i < fExpressions.length; i++) {
        // add parenthesis around expression
        Expression expression = fExpressions[i];

        ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
        parenthesizedExpression.setExpression((Expression) rewrite.createCopyTarget(expression));
        rewrite.replace(expression, parenthesizedExpression, group);
      }
    }
  /** @deprecated using deprecated code */
  public void testParenthesizedExpression() throws Exception {
    // System.out.println(getClass().getName()+"::" + getName() +" disabled (bug 23362)");

    IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null);
    StringBuffer buf = new StringBuffer();
    buf.append("    function foo() {\n");
    buf.append("        i= (1 + 2) * 3;\n");
    buf.append("    }\n");
    IJavaScriptUnit cu = pack1.createCompilationUnit("E.js", buf.toString(), false, null);

    JavaScriptUnit astRoot = createAST(cu);
    ASTRewrite rewrite = ASTRewrite.create(astRoot.getAST());

    AST ast = astRoot.getAST();

    assertTrue("Parse errors", (astRoot.getFlags() & ASTNode.MALFORMED) == 0);
    //		TypeDeclaration type= findTypeDeclaration(astRoot, "E");
    FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo");
    Block block = methodDecl.getBody();
    List statements = block.statements();
    assertTrue("Number of statements not 1", statements.size() == 1);
    { // replace expression
      ExpressionStatement stmt = (ExpressionStatement) statements.get(0);
      Assignment assignment = (Assignment) stmt.getExpression();

      InfixExpression multiplication = (InfixExpression) assignment.getRightHandSide();

      ParenthesizedExpression parenthesizedExpression =
          (ParenthesizedExpression) multiplication.getLeftOperand();

      SimpleName name = ast.newSimpleName("x");
      rewrite.replace(parenthesizedExpression.getExpression(), name, null);
    }

    String preview = evaluateRewrite(cu, rewrite);

    buf = new StringBuffer();
    buf.append("    function foo() {\n");
    buf.append("        i= (x) * 3;\n");
    buf.append("    }\n");
    assertEqualString(preview, buf.toString());
  }
Ejemplo n.º 5
0
 /*
  * @see ASTVisitor#visit(ParenthesizedExpression)
  */
 public boolean visit(ParenthesizedExpression node) {
   this.fBuffer.append("("); // $NON-NLS-1$
   node.getExpression().accept(this);
   this.fBuffer.append(")"); // $NON-NLS-1$
   return false;
 }