Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[parser] Don't crash on comment after trailing comma after elision #10490

Merged
merged 1 commit into from Sep 24, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -40,7 +40,7 @@ export default class CommentsParser extends BaseParser {

adjustCommentsAfterTrailingComma(
node: Node,
elements: Node[],
elements: (Node | null)[],
// When the current node is followed by a token which hasn't a respective AST node, we
// need to take all the trailing comments to prevent them from being attached to an
// unrelated node. e.g. in
Expand All @@ -55,12 +55,15 @@ export default class CommentsParser extends BaseParser {
return;
}

if (elements.length === 0) {
let lastElement = null;
let i = elements.length;
while (lastElement === null && i > 0) {
lastElement = elements[--i];
}
if (lastElement === null) {
return;
}

const lastElement = last(elements);

for (let j = 0; j < this.state.leadingComments.length; j++) {
if (
this.state.leadingComments[j].end < this.state.commentPreviousNode.end
Expand Down
@@ -0,0 +1,2 @@
function foo([foo, /* not used */, /* not used */]) {
}
@@ -0,0 +1,191 @@
{
"type": "File",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 12,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"async": false,
"params": [
{
"type": "ArrayPattern",
"start": 13,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 50
}
},
"elements": [
{
"type": "Identifier",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "foo"
},
"name": "foo",
"trailingComments": [
{
"type": "CommentBlock",
"value": " not used ",
"start": 19,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 33
}
}
},
{
"type": "CommentBlock",
"value": " not used ",
"start": 35,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 1,
"column": 49
}
}
}
]
},
null
]
}
],
"body": {
"type": "BlockStatement",
"start": 52,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 52
},
"end": {
"line": 2,
"column": 1
}
},
"body": [],
"directives": []
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " not used ",
"start": 19,
"end": 33,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 1,
"column": 33
}
}
},
{
"type": "CommentBlock",
"value": " not used ",
"start": 35,
"end": 49,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 1,
"column": 49
}
}
}
]
}