Autosemi after comments #134
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi! Currently, there is a subtle difference in the way
language-javascriptdeals with auto semicolons when comments are present.Let's have a look at this code sample:
And how esprima parses it:
{ "type": "Program", "body": [ { "type": "FunctionDeclaration", "id": { "type": "Identifier", "name": "f1" }, "params": [], "body": { "type": "BlockStatement", "body": [ { "type": "ReturnStatement", "argument": null }, { "type": "ExpressionStatement", "expression": { "type": "Literal", "value": 4, "raw": "4" } } ] }, "generator": false, "expression": false, "async": false }, { "type": "FunctionDeclaration", "id": { "type": "Identifier", "name": "f2" }, "params": [], "body": { "type": "BlockStatement", "body": [ { "type": "ReturnStatement", "argument": null }, { "type": "ExpressionStatement", "expression": { "type": "Literal", "value": 4, "raw": "4" } } ] }, "generator": false, "expression": false, "async": false }, { "type": "FunctionDeclaration", "id": { "type": "Identifier", "name": "f3" }, "params": [], "body": { "type": "BlockStatement", "body": [ { "type": "ReturnStatement", "argument": null }, { "type": "ExpressionStatement", "expression": { "type": "Literal", "value": 4, "raw": "4" } } ] }, "generator": false, "expression": false, "async": false }, { "type": "FunctionDeclaration", "id": { "type": "Identifier", "name": "f4" }, "params": [], "body": { "type": "BlockStatement", "body": [ { "type": "ReturnStatement", "argument": null }, { "type": "ExpressionStatement", "expression": { "type": "Literal", "value": 4, "raw": "4" } } ] }, "generator": false, "expression": false, "async": false } ], "sourceType": "script" }As you can see, the parser is introducing an automatic semicolon in all the four scenarios. This behavior is consistent with modern V8 and SpiderMonkey engines.
However, let's have a look at how
language-javascripthandles these scenarios. If you add the following tests toProgramParser.hs......only the fourth and last test will actually produce the expected results. The other three will fail with the same error:
In other words, this parser is only inserting an automatic semicolon after a newline, but not after comments. This PR fixes this issue.
I have also refactored the
Lexertests, so that they don't just inspect the tokens produced byalex, but also those sent tohappy.