Skip to content

Commit

Permalink
[parser] Don't crash on comment after trailing comma after eli… (#10490)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 24, 2019
1 parent 26be14b commit 0e95026
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 4 deletions.
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
}
}
}
]
}

0 comments on commit 0e95026

Please sign in to comment.