Community
Participate
Working Groups
Below is the test case I am using to test... /** * insert a new function in an object literal */ public void xtest0009() throws Exception { IPackageFragment pack1= fSourceFolder.createPackageFragment("test0009", false, null); StringBuffer buf= new StringBuffer(); buf.append("var o = {\n"); buf.append("con : function(args){},\n"); buf.append("fun1 : function(args){}\n"); buf.append("};\n"); IJavaScriptUnit cu= pack1.createCompilationUnit("X.js", buf.toString(), false, null); JavaScriptUnit astRoot= createCU(cu, false); astRoot.recordModifications(); AST a = astRoot.getAST(); List statements = astRoot.statements(); VariableDeclarationStatement varDeclaration = (VariableDeclarationStatement)statements.get(0); VariableDeclarationFragment frag = (VariableDeclarationFragment) varDeclaration.fragments().get(0); ObjectLiteral obLit = (ObjectLiteral) frag.getInitializer(); List fields = obLit.fields(); ObjectLiteralField newObjectLiteralField = a.newObjectLiteralField(); newObjectLiteralField.setFieldName(a.newSimpleName("newMethod")); newObjectLiteralField.setInitializer(a.newFunctionExpression()); fields.add(newObjectLiteralField); String preview = evaluateRewrite(cu, astRoot); buf= new StringBuffer(); buf.append("var o = {\n"); buf.append("con : function(args){},\n"); buf.append("fun1 : function(args){},\n"); buf.append("newMethod : function(){}\n"); buf.append("};\n"); assertEqualString(preview, buf.toString()); } This currently fails.
Created attachment 173111 [details] patch Patch uses the declaration start and end to calculate length of function.
Patch allows below junit to pass and correctly inserts fields into the final position of object literals.
Patch checked in.