From 7eced6fde8997af5a53dcdba2eeb589b697b5701 Mon Sep 17 00:00:00 2001 From: Shrey Banga Date: Mon, 26 Aug 2019 13:15:52 -0700 Subject: [PATCH 1/5] Retain trailing comments in array expressions This is a proposed fix for https://github.com/babel/babel/issues/10368 with a simple test. --- packages/babel-parser/src/parser/comments.js | 20 +- .../array-expression-trailing-comma/input.js | 13 + .../output.json | 515 ++++++++++++++++++ 3 files changed, 547 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 5ef57efe2fd9..b0cecc9f7a5b 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -86,7 +86,7 @@ export default class CommentsParser extends BaseParser { // Attach comments that follow a trailing comma on the last // property in an object literal or a trailing comma in function arguments - // as trailing comments + // or a trailing comma in array expressions as trailing comments if (firstChild && this.state.leadingComments.length > 0) { const lastComment = last(this.state.leadingComments); @@ -137,6 +137,24 @@ export default class CommentsParser extends BaseParser { } } } + } else if (node.type === "ArrayExpression" && node.elements.length > 0) { + const lastElement = last(node.elements); + if (this.state.commentPreviousNode) { + for (j = 0; j < this.state.leadingComments.length; j++) { + if ( + this.state.leadingComments[j].end < + this.state.commentPreviousNode.end + ) { + this.state.leadingComments.splice(j, 1); + j--; + } + } + + if (this.state.leadingComments.length > 0) { + lastElement.trailingComments = this.state.leadingComments; + this.state.leadingComments = []; + } + } } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js new file mode 100644 index 000000000000..c297bf09d9ff --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js @@ -0,0 +1,13 @@ +const trailing = [ + "One", // One + // Two + "Two", // Three + // Four +] + +const nonTrailing = [ + "One", // One + // Two + "Two" // Three + // Four +] \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json new file mode 100644 index 000000000000..9dc01545348c --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json @@ -0,0 +1,515 @@ +{ + "type": "File", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 13, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 152, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 13, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 14, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 14 + }, + "identifierName": "trailing" + }, + "name": "trailing" + }, + "init": { + "type": "ArrayExpression", + "start": 17, + "end": 74, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "elements": [ + { + "type": "StringLiteral", + "start": 21, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "extra": { + "rawValue": "One", + "raw": "\"One\"" + }, + "value": "One" + }, + { + "type": "StringLiteral", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "extra": { + "rawValue": "Two", + "raw": "\"Two\"" + }, + "value": "Two", + "leadingComments": [ + { + "type": "CommentLine", + "value": " One", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 9 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentLine", + "value": " Three", + "start": 54, + "end": 62, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 17 + } + } + }, + { + "type": "CommentLine", + "value": " Four", + "start": 65, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 9 + } + } + } + ] + } + ] + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 76, + "end": 152, + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 13, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 82, + "end": 152, + "loc": { + "start": { + "line": 8, + "column": 6 + }, + "end": { + "line": 13, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 82, + "end": 93, + "loc": { + "start": { + "line": 8, + "column": 6 + }, + "end": { + "line": 8, + "column": 17 + }, + "identifierName": "nonTrailing" + }, + "name": "nonTrailing" + }, + "init": { + "type": "ArrayExpression", + "start": 96, + "end": 152, + "loc": { + "start": { + "line": 8, + "column": 20 + }, + "end": { + "line": 13, + "column": 1 + } + }, + "elements": [ + { + "type": "StringLiteral", + "start": 100, + "end": 105, + "loc": { + "start": { + "line": 9, + "column": 2 + }, + "end": { + "line": 9, + "column": 7 + } + }, + "extra": { + "rawValue": "One", + "raw": "\"One\"" + }, + "value": "One" + }, + { + "type": "StringLiteral", + "start": 126, + "end": 131, + "loc": { + "start": { + "line": 11, + "column": 2 + }, + "end": { + "line": 11, + "column": 7 + } + }, + "extra": { + "rawValue": "Two", + "raw": "\"Two\"" + }, + "value": "Two", + "leadingComments": [ + { + "type": "CommentLine", + "value": " One", + "start": 107, + "end": 113, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 117, + "end": 123, + "loc": { + "start": { + "line": 10, + "column": 3 + }, + "end": { + "line": 10, + "column": 9 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentLine", + "value": " Three", + "start": 132, + "end": 140, + "loc": { + "start": { + "line": 11, + "column": 8 + }, + "end": { + "line": 11, + "column": 16 + } + } + }, + { + "type": "CommentLine", + "value": " Four", + "start": 143, + "end": 150, + "loc": { + "start": { + "line": 12, + "column": 2 + }, + "end": { + "line": 12, + "column": 9 + } + } + } + ] + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " One", + "start": 28, + "end": 34, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 38, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 9 + } + } + }, + { + "type": "CommentLine", + "value": " Three", + "start": 54, + "end": 62, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 17 + } + } + }, + { + "type": "CommentLine", + "value": " Four", + "start": 65, + "end": 72, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 9 + } + } + }, + { + "type": "CommentLine", + "value": " One", + "start": 107, + "end": 113, + "loc": { + "start": { + "line": 9, + "column": 9 + }, + "end": { + "line": 9, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 117, + "end": 123, + "loc": { + "start": { + "line": 10, + "column": 3 + }, + "end": { + "line": 10, + "column": 9 + } + } + }, + { + "type": "CommentLine", + "value": " Three", + "start": 132, + "end": 140, + "loc": { + "start": { + "line": 11, + "column": 8 + }, + "end": { + "line": 11, + "column": 16 + } + } + }, + { + "type": "CommentLine", + "value": " Four", + "start": 143, + "end": 150, + "loc": { + "start": { + "line": 12, + "column": 2 + }, + "end": { + "line": 12, + "column": 9 + } + } + } + ] +} \ No newline at end of file From 674bf6abdde80a24540a4535e7eeffef28908701 Mon Sep 17 00:00:00 2001 From: Shrey Banga Date: Tue, 27 Aug 2019 16:25:18 -0700 Subject: [PATCH 2/5] Move lastElement in the block where it's used --- packages/babel-parser/src/parser/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index b0cecc9f7a5b..743c97b7ef2d 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -138,7 +138,6 @@ export default class CommentsParser extends BaseParser { } } } else if (node.type === "ArrayExpression" && node.elements.length > 0) { - const lastElement = last(node.elements); if (this.state.commentPreviousNode) { for (j = 0; j < this.state.leadingComments.length; j++) { if ( @@ -151,6 +150,7 @@ export default class CommentsParser extends BaseParser { } if (this.state.leadingComments.length > 0) { + const lastElement = last(node.elements); lastElement.trailingComments = this.state.leadingComments; this.state.leadingComments = []; } From 1ea1ada3644217b57ec145160e81209365a15b63 Mon Sep 17 00:00:00 2001 From: Shrey Banga Date: Tue, 27 Aug 2019 16:25:26 -0700 Subject: [PATCH 3/5] Test trailing comment after array expression --- .../array-expression-trailing-comma/input.js | 15 +- .../output.json | 335 ++++++++++++++---- 2 files changed, 270 insertions(+), 80 deletions(-) diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js index c297bf09d9ff..bc0802b8addd 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/input.js @@ -1,13 +1,18 @@ -const trailing = [ +const nonTrailing = [ "One", // One // Two - "Two", // Three + "Two" // Three // Four ] -const nonTrailing = [ +const trailingAfterComma = [ "One", // One // Two - "Two" // Three + "Two", // Three // Four -] \ No newline at end of file +] + +const trailingAfterArray = [ + "One", // One + // Two +] // Three diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json index 9dc01545348c..cf9c617bcd68 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json @@ -1,29 +1,29 @@ { "type": "File", "start": 0, - "end": 152, + "end": 229, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 13, - "column": 1 + "line": 18, + "column": 10 } }, "program": { "type": "Program", "start": 0, - "end": 152, + "end": 229, "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 13, - "column": 1 + "line": 18, + "column": 10 } }, "sourceType": "script", @@ -32,7 +32,7 @@ { "type": "VariableDeclaration", "start": 0, - "end": 74, + "end": 76, "loc": { "start": { "line": 1, @@ -47,7 +47,7 @@ { "type": "VariableDeclarator", "start": 6, - "end": 74, + "end": 76, "loc": { "start": { "line": 1, @@ -61,7 +61,7 @@ "id": { "type": "Identifier", "start": 6, - "end": 14, + "end": 17, "loc": { "start": { "line": 1, @@ -69,20 +69,20 @@ }, "end": { "line": 1, - "column": 14 + "column": 17 }, - "identifierName": "trailing" + "identifierName": "nonTrailing" }, - "name": "trailing" + "name": "nonTrailing" }, "init": { "type": "ArrayExpression", - "start": 17, - "end": 74, + "start": 20, + "end": 76, "loc": { "start": { "line": 1, - "column": 17 + "column": 20 }, "end": { "line": 6, @@ -92,8 +92,8 @@ "elements": [ { "type": "StringLiteral", - "start": 21, - "end": 26, + "start": 24, + "end": 29, "loc": { "start": { "line": 2, @@ -112,8 +112,8 @@ }, { "type": "StringLiteral", - "start": 47, - "end": 52, + "start": 50, + "end": 55, "loc": { "start": { "line": 4, @@ -133,8 +133,8 @@ { "type": "CommentLine", "value": " One", - "start": 28, - "end": 34, + "start": 31, + "end": 37, "loc": { "start": { "line": 2, @@ -149,8 +149,8 @@ { "type": "CommentLine", "value": " Two", - "start": 38, - "end": 44, + "start": 41, + "end": 47, "loc": { "start": { "line": 3, @@ -167,24 +167,24 @@ { "type": "CommentLine", "value": " Three", - "start": 54, - "end": 62, + "start": 56, + "end": 64, "loc": { "start": { "line": 4, - "column": 9 + "column": 8 }, "end": { "line": 4, - "column": 17 + "column": 16 } } }, { "type": "CommentLine", "value": " Four", - "start": 65, - "end": 72, + "start": 67, + "end": 74, "loc": { "start": { "line": 5, @@ -206,8 +206,8 @@ }, { "type": "VariableDeclaration", - "start": 76, - "end": 152, + "start": 78, + "end": 162, "loc": { "start": { "line": 8, @@ -221,8 +221,8 @@ "declarations": [ { "type": "VariableDeclarator", - "start": 82, - "end": 152, + "start": 84, + "end": 162, "loc": { "start": { "line": 8, @@ -235,8 +235,8 @@ }, "id": { "type": "Identifier", - "start": 82, - "end": 93, + "start": 84, + "end": 102, "loc": { "start": { "line": 8, @@ -244,20 +244,20 @@ }, "end": { "line": 8, - "column": 17 + "column": 24 }, - "identifierName": "nonTrailing" + "identifierName": "trailingAfterComma" }, - "name": "nonTrailing" + "name": "trailingAfterComma" }, "init": { "type": "ArrayExpression", - "start": 96, - "end": 152, + "start": 105, + "end": 162, "loc": { "start": { "line": 8, - "column": 20 + "column": 27 }, "end": { "line": 13, @@ -267,8 +267,8 @@ "elements": [ { "type": "StringLiteral", - "start": 100, - "end": 105, + "start": 109, + "end": 114, "loc": { "start": { "line": 9, @@ -287,8 +287,8 @@ }, { "type": "StringLiteral", - "start": 126, - "end": 131, + "start": 135, + "end": 140, "loc": { "start": { "line": 11, @@ -308,8 +308,8 @@ { "type": "CommentLine", "value": " One", - "start": 107, - "end": 113, + "start": 116, + "end": 122, "loc": { "start": { "line": 9, @@ -324,8 +324,8 @@ { "type": "CommentLine", "value": " Two", - "start": 117, - "end": 123, + "start": 126, + "end": 132, "loc": { "start": { "line": 10, @@ -342,24 +342,24 @@ { "type": "CommentLine", "value": " Three", - "start": 132, - "end": 140, + "start": 142, + "end": 150, "loc": { "start": { "line": 11, - "column": 8 + "column": 9 }, "end": { "line": 11, - "column": 16 + "column": 17 } } }, { "type": "CommentLine", "value": " Four", - "start": 143, - "end": 150, + "start": 153, + "end": 160, "loc": { "start": { "line": 12, @@ -378,6 +378,143 @@ } ], "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 164, + "end": 220, + "loc": { + "start": { + "line": 15, + "column": 0 + }, + "end": { + "line": 18, + "column": 1 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 170, + "end": 220, + "loc": { + "start": { + "line": 15, + "column": 6 + }, + "end": { + "line": 18, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 170, + "end": 188, + "loc": { + "start": { + "line": 15, + "column": 6 + }, + "end": { + "line": 15, + "column": 24 + }, + "identifierName": "trailingAfterArray" + }, + "name": "trailingAfterArray" + }, + "init": { + "type": "ArrayExpression", + "start": 191, + "end": 220, + "loc": { + "start": { + "line": 15, + "column": 27 + }, + "end": { + "line": 18, + "column": 1 + } + }, + "elements": [ + { + "type": "StringLiteral", + "start": 195, + "end": 200, + "loc": { + "start": { + "line": 16, + "column": 2 + }, + "end": { + "line": 16, + "column": 7 + } + }, + "extra": { + "rawValue": "One", + "raw": "\"One\"" + }, + "value": "One", + "trailingComments": [ + { + "type": "CommentLine", + "value": " One", + "start": 202, + "end": 208, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 212, + "end": 218, + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + { + "type": "CommentLine", + "value": " Three", + "start": 221, + "end": 229, + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ] + } + ] + } + } + ], + "kind": "const" } ], "directives": [] @@ -386,8 +523,8 @@ { "type": "CommentLine", "value": " One", - "start": 28, - "end": 34, + "start": 31, + "end": 37, "loc": { "start": { "line": 2, @@ -402,8 +539,8 @@ { "type": "CommentLine", "value": " Two", - "start": 38, - "end": 44, + "start": 41, + "end": 47, "loc": { "start": { "line": 3, @@ -418,24 +555,24 @@ { "type": "CommentLine", "value": " Three", - "start": 54, - "end": 62, + "start": 56, + "end": 64, "loc": { "start": { "line": 4, - "column": 9 + "column": 8 }, "end": { "line": 4, - "column": 17 + "column": 16 } } }, { "type": "CommentLine", "value": " Four", - "start": 65, - "end": 72, + "start": 67, + "end": 74, "loc": { "start": { "line": 5, @@ -450,8 +587,8 @@ { "type": "CommentLine", "value": " One", - "start": 107, - "end": 113, + "start": 116, + "end": 122, "loc": { "start": { "line": 9, @@ -466,8 +603,8 @@ { "type": "CommentLine", "value": " Two", - "start": 117, - "end": 123, + "start": 126, + "end": 132, "loc": { "start": { "line": 10, @@ -482,24 +619,24 @@ { "type": "CommentLine", "value": " Three", - "start": 132, - "end": 140, + "start": 142, + "end": 150, "loc": { "start": { "line": 11, - "column": 8 + "column": 9 }, "end": { "line": 11, - "column": 16 + "column": 17 } } }, { "type": "CommentLine", "value": " Four", - "start": 143, - "end": 150, + "start": 153, + "end": 160, "loc": { "start": { "line": 12, @@ -510,6 +647,54 @@ "column": 9 } } + }, + { + "type": "CommentLine", + "value": " One", + "start": 202, + "end": 208, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 15 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 212, + "end": 218, + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 9 + } + } + }, + { + "type": "CommentLine", + "value": " Three", + "start": 221, + "end": 229, + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 10 + } + } } ] } \ No newline at end of file From c9f6603fb230c9d2f00d62463446f00e9a5ba52f Mon Sep 17 00:00:00 2001 From: Shrey Banga Date: Tue, 27 Aug 2019 19:41:13 -0700 Subject: [PATCH 4/5] Don't move comments after the array expression --- packages/babel-parser/src/parser/comments.js | 13 +++++++++---- .../array-expression-trailing-comma/output.json | 16 ---------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 743c97b7ef2d..5986d1ad8f71 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -149,10 +149,15 @@ export default class CommentsParser extends BaseParser { } } - if (this.state.leadingComments.length > 0) { - const lastElement = last(node.elements); - lastElement.trailingComments = this.state.leadingComments; - this.state.leadingComments = []; + const lastElement = last(node.elements); + lastElement.trailingComments = []; + for (j = 0; j < this.state.leadingComments.length; j++) { + if (this.state.leadingComments[j].end < node.end) { + lastElement.trailingComments.push( + this.state.leadingComments.splice(j, 1)[0], + ); + j--; + } } } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json index cf9c617bcd68..795add707634 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json @@ -491,22 +491,6 @@ "column": 9 } } - }, - { - "type": "CommentLine", - "value": " Three", - "start": 221, - "end": 229, - "loc": { - "start": { - "line": 18, - "column": 2 - }, - "end": { - "line": 18, - "column": 10 - } - } } ] } From 0638e59dd90b942c5aee87a3773d92eeefa87fd1 Mon Sep 17 00:00:00 2001 From: Shrey Banga Date: Tue, 27 Aug 2019 19:59:05 -0700 Subject: [PATCH 5/5] Retain trailing comment after the array expression --- packages/babel-parser/src/parser/comments.js | 15 ++++++++------ .../output.json | 20 ++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 5986d1ad8f71..60f3c1a23f9b 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -151,12 +151,15 @@ export default class CommentsParser extends BaseParser { const lastElement = last(node.elements); lastElement.trailingComments = []; - for (j = 0; j < this.state.leadingComments.length; j++) { - if (this.state.leadingComments[j].end < node.end) { - lastElement.trailingComments.push( - this.state.leadingComments.splice(j, 1)[0], - ); - j--; + while (this.state.leadingComments.length) { + const leadingComment = this.state.leadingComments.shift(); + if (leadingComment.end < node.end) { + lastElement.trailingComments.push(leadingComment); + } else { + if (node.trailingComments === undefined) { + node.trailingComments = []; + } + node.trailingComments.push(leadingComment); } } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json index 795add707634..90428301c81b 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/array-expression-trailing-comma/output.json @@ -498,7 +498,25 @@ } } ], - "kind": "const" + "kind": "const", + "trailingComments": [ + { + "type": "CommentLine", + "value": " Three", + "start": 221, + "end": 229, + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ] } ], "directives": []