Bug 232645

Summary: For in loops are written incorrectly by the NaiveASTFlattener
Product: [WebTools] JSDT Reporter: Etienne Pfister <epfister>
Component: GeneralAssignee: Phil Berkland <berkland>
Status: RESOLVED FIXED QA Contact: Phil Berkland <berkland>
Severity: major    
Priority: P3 CC: cbachman, hf
Version: unspecifiedKeywords: contributed
Target Milestone: 3.0.1   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Patch for correct for in loop respresenation in NaiveASTFlattener bjorn.freeman-benson: iplog+

Description Etienne Pfister CLA 2008-05-17 04:18:57 EDT
Created attachment 100764 [details]
Patch for correct for in loop respresenation in NaiveASTFlattener

If an AST contains for in loops the NaiveASTFlattener (called by the toString() method of an ASTNode) will write them incorrectly.

an example:
for (x in mycars)
{
 ...
}

output after rewrite:
for (x; in mycars)
{
 ...
}

This occurs because x is part of an ExpressionStatement and an Expression Statement is terminated by default with a ";". If the parent of an ExpressionStatement is a ForInStatement ";" shouldn't be written.
Comment 1 Phil Berkland CLA 2008-07-18 17:27:03 EDT
fixed
Comment 2 H. Finster CLA 2011-01-11 05:12:51 EST
(In reply to comment #1)
> fixed

Please forgive me, if the following is unrelated to this bug/fix, but I think that this fix causes a null pointer exception, if I try to insert a new ExpressionStatement into e.g. a CompilationUnit.

For example, if I create an Assignment
final Assignment assignment = ast.newAssignment();
// ... initialize lhs and rhs ...
and wrap the Assignment into an ExpressionStatement

final ExpressionStatement statement = ast.newExpressionStatement(assignment);
and attempt to insert this into a JavaScriptUnit
final ListRewrite listRewrite = rewrite.getListRewrite(unit,
					JavaScriptUnit.STATEMENTS_PROPERTY);
listRewrite.insertLast(statement, null);

TextEdit edit = rewrite.rewriteAST( document, null);

The "rewriteAST" fails with a null pointer exception, because in 
ASTRewriteFlattener visit(ExpressionStatement) node.getParent()
returns null; (As the new statement is still unparented.)

This is the stack-trace:
java.lang.NullPointerException
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteFlattener.visit(ASTRewriteFlattener.java:535)
	at org.eclipse.wst.jsdt.core.dom.ExpressionStatement.accept0(ExpressionStatement.java:144)
	at org.eclipse.wst.jsdt.core.dom.ASTNode.accept(ASTNode.java:2404)
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteFormatter.getFormattedResult(ASTRewriteFormatter.java:176)
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.doTextInsert(ASTRewriteAnalyzer.java:1116)
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteAnalyzer$ListRewriter.rewriteList(ASTRewriteAnalyzer.java:596)
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteParagraphList(ASTRewriteAnalyzer.java:961)
	at org.eclipse.wst.jsdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.visit(ASTRewriteAnalyzer.java:1411)
	at org.eclipse.wst.jsdt.core.dom.JavaScriptUnit.accept0(JavaScriptUnit.java:219)
	at org.eclipse.wst.jsdt.core.dom.ASTNode.accept(ASTNode.java:2404)
	at org.eclipse.wst.jsdt.core.dom.rewrite.ASTRewrite.internalRewriteAST(ASTRewrite.java:257)
	at org.eclipse.wst.jsdt.core.dom.rewrite.ASTRewrite.rewriteAST(ASTRewrite.java:189)
	
...