Skip to content

Commit

Permalink
Fix syntax error for comments in array (#607)
Browse files Browse the repository at this point in the history
Fix syntax error for comments embedded in an array
  • Loading branch information
PalAditya authored and gbrail committed Oct 10, 2019
1 parent c214075 commit e85ebaf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/org/mozilla/javascript/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3272,6 +3272,8 @@ private AstNode arrayLiteral()
elements.add(new EmptyExpression(ts.tokenBeg, 1));
skipCount++;
}
} else if(tt == Token.COMMENT) {
consumeToken();
} else if (tt == Token.RB) {
consumeToken();
// for ([a,] in obj) is legal, but for ([a] in obj) is
Expand Down
26 changes: 26 additions & 0 deletions testsrc/org/mozilla/javascript/tests/ParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,32 @@ public void testParseAutoSemiColonBeforeNewlineAndComments() throws IOException

assertEquals("var s = 3;\nvar t = 1;\n", root.toSource());
}

public void testCommentInArray() throws IOException{
//Test a single comment
AstRoot root = parseAsReader(
"var array = [/**/];");
assertNotNull(root.getComments());
assertEquals(1, root.getComments().size());
assertEquals(root.toSource(),"var array = [];\n");
//Test multiple comments
root = parseAsReader(
"var array = [/*Hello*/ /*World*/ 1,2];");
assertNotNull(root.getComments());
assertEquals(2, root.getComments().size());
assertEquals(root.toSource(),"var array = [1, 2];\n");
//Test no comments
root = parseAsReader(
"var array = [1,2];");
assertNull(root.getComments());
assertEquals(root.toSource(),"var array = [1, 2];\n");
root = parseAsReader(
"var array = [1,/*hello*/2,/*World*/3];");
//Test comments in middle
assertNotNull(root.getComments());
assertEquals(2, root.getComments().size());
assertEquals(root.toSource(),"var array = [1, 2, 3];\n");
}

public void testNewlineAndComments() throws IOException {
AstRoot root = parseAsReader(
Expand Down

0 comments on commit e85ebaf

Please sign in to comment.