Closed Bug 565361 Opened 16 years ago Closed 15 years ago

VarDeclaration toSource missing semicolon

Categories

(Rhino Graveyard :: Core, defect)

x86
All
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 491621

People

(Reporter: mikesamuel, Unassigned)

Details

User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.9 Safari/533.2 Build Identifier: The VarDeclaration toSource method does not append a semicolon where necessary. Reproducible: Always Steps to Reproduce: Compile and run the program below import org.mozilla.javascript.Parser; import org.mozilla.javascript.ast.AstNode; public class RhinoBug { public static void main(String... argv) throws Exception { String js = "function f() { var i = 2; for (var j = 0; j < i; ++j) print(j); }"; AstNode root = new Parser().parse(js, "test-input", 1); System.out.println(root.toSource()); } } Actual Results: function f() { var i = 2 for (var j = 0; j < i; ++j) print(j); } Expected Results: function f() { var i = 2; for (var j = 0; j < i; ++j) print(j); } Note that in the actual results above, since the var declaration is on its own line, the lack of a semicolon results in a syntax error. If there was only a newline then the below would not have a syntax exception but would be incorrect. function f() { var i = function(x) { return x; }; ({ x: y }); } I believe changing VariableDeclaration.toSource to read @Override public String toSource(int depth) { StringBuilder sb = new StringBuilder(); sb.append(makeIndent(depth)); sb.append(declTypeName()); sb.append(" "); printList(variables, sb); if (!(getParent() instanceof ForLoop)) { sb.append(";\n"); } return sb.toString(); } fixes the problem.
The patch above should also exclude (getParent() instanceof ForInLoop) so that for (var k in o) {...} does not render as for (var k; in o) { ... }
Thanks for the report, fixed in CVS.
Status: UNCONFIRMED → RESOLVED
Closed: 15 years ago
Resolution: --- → FIXED
The committed patch does not properly fix all occurrences of this bug. For example, the following source code: "for(var i = 0; i < 10; i++) var x=i; x++;" is rendered as follows by the AST: for (var i = 0; i < 10; i++) var x = ix++; Reopening and marking as duplicate of bug #491621.
Resolution: FIXED → DUPLICATE
You need to log in before you can comment on or make changes to this bug.