Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 318472

Summary: Using DOM API a function can not be inserted in the last position of object literal
Product: [WebTools] JSDT Reporter: Chris Jaun <cmjaun>
Component: GeneralAssignee: Chris Jaun <cmjaun>
Status: RESOLVED FIXED QA Contact: Nitin Dahyabhai <thatnitind>
Severity: normal    
Priority: P3 Flags: thatnitind: review+
Version: 3.2   
Target Milestone: 3.2.1   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
patch none

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.