Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 318472 - Using DOM API a function can not be inserted in the last position of object literal
Summary: Using DOM API a function can not be inserted in the last position of object l...
Status: RESOLVED FIXED
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: Chris Jaun CLA
QA Contact: Nitin Dahyabhai CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-06-30 09:52 EDT by Chris Jaun CLA
Modified: 2010-07-06 07:03 EDT (History)
0 users

See Also:
thatnitind: review+


Attachments
patch (1012 bytes, patch)
2010-06-30 11:37 EDT, Chris Jaun CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Jaun CLA 2010-06-30 09:52:16 EDT
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.
Comment 1 Chris Jaun CLA 2010-06-30 11:37:34 EDT
Created attachment 173111 [details]
patch

Patch uses the declaration start and end to calculate length of function.
Comment 2 Chris Jaun CLA 2010-06-30 11:42:21 EDT
Patch allows below junit to pass and correctly inserts fields into the final position of object literals.
Comment 3 Chris Jaun CLA 2010-06-30 16:33:53 EDT
Patch checked in.