[parser] Don't crash on comment after trailing comma after eli… (#10490)
This commit is contained in:
parent
26be14b24d
commit
0e9502685a
@ -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
|
||||
@ -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
|
||||
|
||||
2
packages/babel-parser/test/fixtures/comments/regression/10448/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/comments/regression/10448/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
function foo([foo, /* not used */, /* not used */]) {
|
||||
}
|
||||
191
packages/babel-parser/test/fixtures/comments/regression/10448/output.json
vendored
Normal file
191
packages/babel-parser/test/fixtures/comments/regression/10448/output.json
vendored
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user