/** @deprecated using deprecated code */ public void testConditionalExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= (k == 0) ? 1 : 2;\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); { // change compare expression, then expression & else expression ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); ConditionalExpression condExpression = (ConditionalExpression) assignment.getRightHandSide(); BooleanLiteral literal = ast.newBooleanLiteral(true); rewrite.replace(condExpression.getExpression(), literal, null); SimpleName newThenExpre = ast.newSimpleName("x"); rewrite.replace(condExpression.getThenExpression(), newThenExpre, null); InfixExpression infixExpression = ast.newInfixExpression(); infixExpression.setLeftOperand(ast.newNumberLiteral("1")); infixExpression.setRightOperand(ast.newNumberLiteral("2")); infixExpression.setOperator(InfixExpression.Operator.PLUS); rewrite.replace(condExpression.getElseExpression(), infixExpression, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= true ? x : 1 + 2;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
public void testArrayAccess() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo(o) {\n"); buf.append(" o[3 /* comment*/ - 1]= this.o[3 - 1];\n"); buf.append("}\n"); buf.append(""); 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); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 1", statements.size() == 1); { // replace left hand side index, replace right hand side index by left side index ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); ArrayAccess left = (ArrayAccess) assignment.getLeftHandSide(); ArrayAccess right = (ArrayAccess) assignment.getRightHandSide(); NumberLiteral name = ast.newNumberLiteral("1"); rewrite.replace(left.getIndex(), name, null); ASTNode placeHolder = rewrite.createCopyTarget(left.getIndex()); rewrite.replace(right.getIndex(), placeHolder, null); SimpleName newName = ast.newSimpleName("o"); rewrite.replace(right.getArray(), newName, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo(o) {\n"); buf.append(" o[1]= o[3 /* comment*/ - 1];\n"); buf.append("}\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testPostfixExpression() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= x--;\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); { // modify operand and operation ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); PostfixExpression postExpression = (PostfixExpression) assignment.getRightHandSide(); NumberLiteral newOperation = ast.newNumberLiteral("10"); rewrite.replace(postExpression.getOperand(), newOperation, null); rewrite.set( postExpression, PostfixExpression.OPERATOR_PROPERTY, PostfixExpression.Operator.INCREMENT, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" i= 10++;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/** @deprecated using deprecated code */ public void testMethodInvocation() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" foo(1, 2).goo();\n"); buf.append(" foo(1, 2).goo();\n"); buf.append(" foo(1, 2).goo();\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 3", statements.size() == 3); { // remove expression, add param, change name ExpressionStatement stmt = (ExpressionStatement) statements.get(0); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); rewrite.remove(invocation.getExpression(), null); SimpleName name = ast.newSimpleName("x"); rewrite.replace(invocation.getName(), name, null); ASTNode arg = ast.newNumberLiteral("1"); rewrite .getListRewrite(invocation, FunctionInvocation.ARGUMENTS_PROPERTY) .insertLast(arg, null); } { // insert expression, delete params ExpressionStatement stmt = (ExpressionStatement) statements.get(1); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); FunctionInvocation leftInvocation = (FunctionInvocation) invocation.getExpression(); SimpleName newExpression = ast.newSimpleName("x"); rewrite.set(leftInvocation, FunctionInvocation.EXPRESSION_PROPERTY, newExpression, null); List args = leftInvocation.arguments(); rewrite.remove((ASTNode) args.get(0), null); rewrite.remove((ASTNode) args.get(1), null); } { // remove expression, add it as parameter ExpressionStatement stmt = (ExpressionStatement) statements.get(2); FunctionInvocation invocation = (FunctionInvocation) stmt.getExpression(); ASTNode placeHolder = rewrite.createCopyTarget(invocation.getExpression()); rewrite.set(invocation, FunctionInvocation.EXPRESSION_PROPERTY, null, null); rewrite .getListRewrite(invocation, FunctionInvocation.ARGUMENTS_PROPERTY) .insertLast(placeHolder, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" x(1);\n"); buf.append(" x.foo().goo();\n"); buf.append(" goo(foo(1, 2));\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }