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()); }
/** @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()); }