diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 8554ffaf52cc..5a50c15c6414 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -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 diff --git a/packages/babel-parser/test/fixtures/comments/regression/10448/input.js b/packages/babel-parser/test/fixtures/comments/regression/10448/input.js new file mode 100644 index 000000000000..c0bd1ccbc1cc --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/10448/input.js @@ -0,0 +1,2 @@ +function foo([foo, /* not used */, /* not used */]) { +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/regression/10448/output.json b/packages/babel-parser/test/fixtures/comments/regression/10448/output.json new file mode 100644 index 000000000000..ab6d764cc245 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/10448/output.json @@ -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 + } + } + } + ] +} \ No newline at end of file