public void testAssignment() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" var i, j;\n"); buf.append(" i= 0;\n"); buf.append(" i-= j= 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); FunctionDeclaration methodDecl = findMethodDeclaration(astRoot, "foo"); Block block = methodDecl.getBody(); List statements = block.statements(); assertTrue("Number of statements not 3", statements.size() == 3); { // change left side & right side ExpressionStatement stmt = (ExpressionStatement) statements.get(1); Assignment assignment = (Assignment) stmt.getExpression(); SimpleName name = ast.newSimpleName("j"); rewrite.replace(assignment.getLeftHandSide(), name, null); FunctionInvocation invocation = ast.newFunctionInvocation(); invocation.setName(ast.newSimpleName("goo")); invocation.setExpression(ast.newSimpleName("other")); rewrite.replace(assignment.getRightHandSide(), invocation, null); } { // change operator and operator of inner ExpressionStatement stmt = (ExpressionStatement) statements.get(2); Assignment assignment = (Assignment) stmt.getExpression(); rewrite.set( assignment, Assignment.OPERATOR_PROPERTY, Assignment.Operator.DIVIDE_ASSIGN, null); Assignment inner = (Assignment) assignment.getRightHandSide(); rewrite.set( inner, Assignment.OPERATOR_PROPERTY, Assignment.Operator.RIGHT_SHIFT_UNSIGNED_ASSIGN, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append("function foo() {\n"); buf.append(" var i, j;\n"); buf.append(" j= other.goo();\n"); buf.append(" i/= j>>>= 3;\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 testFieldAccess() throws Exception { IPackageFragment pack1 = this.sourceFolder.createPackageFragment("test1", false, null); StringBuffer buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" foo().i= goo().i;\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 field expression, replace field name ExpressionStatement stmt = (ExpressionStatement) statements.get(0); Assignment assignment = (Assignment) stmt.getExpression(); FieldAccess leftFieldAccess = (FieldAccess) assignment.getLeftHandSide(); FieldAccess rightFieldAccess = (FieldAccess) assignment.getRightHandSide(); FunctionInvocation invocation = ast.newFunctionInvocation(); invocation.setName(ast.newSimpleName("xoo")); rewrite.replace(leftFieldAccess.getExpression(), invocation, null); SimpleName newName = ast.newSimpleName("x"); rewrite.replace(leftFieldAccess.getName(), newName, null); SimpleName rightHand = ast.newSimpleName("b"); rewrite.replace(rightFieldAccess.getExpression(), rightHand, null); } String preview = evaluateRewrite(cu, rewrite); buf = new StringBuffer(); buf.append(" function foo() {\n"); buf.append(" xoo().x= b.i;\n"); buf.append(" }\n"); assertEqualString(preview, buf.toString()); }
/* * @see ASTVisitor#visit(Assignment) */ public boolean visit(Assignment node) { node.getLeftHandSide().accept(this); this.fBuffer.append(node.getOperator().toString()); node.getRightHandSide().accept(this); return false; }