Community
Participate
Working Groups
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.
fixed
(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) ...