Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rhino/src/main/java/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,16 @@ && matchToken(Token.NAME, true)
markDestructuring(init);
cond = expr(false); // object over which we're iterating
} else { // ordinary for-loop
// For ordinary for loops, destructuring declarations must have initializers
if (init instanceof VariableDeclaration) {
VariableDeclaration varDecl = (VariableDeclaration) init;
for (VariableInitializer vi : varDecl.getVariables()) {
if (vi.isDestructuring() && vi.getInitializer() == null) {
reportError("msg.destruct.assign.no.init");
}
}
}

mustMatchToken(Token.SEMI, "msg.no.semi.for", true);
if (peekToken() == Token.SEMI) {
// no loop condition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,13 @@ public void oomOnInvalidInput() {
expectParseErrors("`\\u{8", new String[] {"syntax error"});
}

@Test
public void errorOnInvalidDestructuringDeclaration() {
expectParseErrors(
"for(var {};;) {}",
new String[] {"Missing = in destructuring declaration", "syntax error"});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a reader of error messages i like this form more - not sure why the french version has the ' and the english not.

Missing '=' in destructuring declaration

}

private void expectParseErrors(String string, String[] errors) {
parse(string, errors, null, false);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/testsrc/jstests/harmony/destructuring.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ assertEquals(obj.b, 345);

assertThrows("(1 ? {} : 490) = 1", SyntaxError);
assertThrows("(1 ? [] : 490) = 1", SyntaxError);
assertThrows("for (var {};;) {}", SyntaxError);
assertThrows("for (let {};;) {}", SyntaxError);
assertThrows("for (var {a};;) {}", SyntaxError);
assertThrows("for (let {a};;) {}", SyntaxError);

"success";