Bug 232645 - For in loops are written incorrectly by the NaiveASTFlattener
Summary: For in loops are written incorrectly by the NaiveASTFlattener
Status: RESOLVED FIXED
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: 3.0.1   Edit
Assignee: Phil Berkland CLA
QA Contact: Phil Berkland CLA
URL:
Whiteboard:
Keywords: contributed
Depends on:
Blocks:
 
Reported: 2008-05-17 04:18 EDT by Etienne Pfister CLA
Modified: 2011-01-11 05:12 EST (History)
2 users (show)

See Also:


Attachments
Patch for correct for in loop respresenation in NaiveASTFlattener (783 bytes, patch)
2008-05-17 04:18 EDT, Etienne Pfister CLA
bjorn.freeman-benson: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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)
	
...