diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index bf936d2dce10..c990c9e0e8a9 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -264,7 +264,22 @@ export default class CommentsParser extends BaseParser { ) { node.innerComments = trailingComments; } else { - node.trailingComments = trailingComments; + // TrailingComments maybe contain innerComments + const firstTrailingCommentIndex = trailingComments.findIndex( + comment => comment.end >= node.end, + ); + + if (firstTrailingCommentIndex > 0) { + node.innerComments = trailingComments.slice( + 0, + firstTrailingCommentIndex, + ); + node.trailingComments = trailingComments.slice( + firstTrailingCommentIndex, + ); + } else { + node.trailingComments = trailingComments; + } } } diff --git a/packages/babel-parser/test/fixtures/comments/regression/11469/input.js b/packages/babel-parser/test/fixtures/comments/regression/11469/input.js new file mode 100644 index 000000000000..242927b03af0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/11469/input.js @@ -0,0 +1,18 @@ +class A { + test() { + // this.member = 'value'; + } + + /* Trailing comment */ +} + +class B { + test() { + // this.member = 'value'; + } +} + +class C { + test() {} + // this.member = 'value'; +} diff --git a/packages/babel-parser/test/fixtures/comments/regression/11469/output.json b/packages/babel-parser/test/fixtures/comments/regression/11469/output.json new file mode 100644 index 000000000000..c926a81919a8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/11469/output.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start":0,"end":193,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":193,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":82,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"}, + "name": "A" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":82,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":12,"end":54,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}}, + "static": false, + "key": { + "type": "Identifier", + "start":12,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"test"}, + "name": "test" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":19,"end":54,"loc":{"start":{"line":2,"column":9},"end":{"line":4,"column":3}}, + "body": [], + "directives": [], + "innerComments": [ + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":25,"end":50,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}} + } + ] + }, + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Trailing comment ", + "start":58,"end":80,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":24}} + } + ] + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":84,"end":140,"loc":{"start":{"line":9,"column":0},"end":{"line":13,"column":1}}, + "id": { + "type": "Identifier", + "start":90,"end":91,"loc":{"start":{"line":9,"column":6},"end":{"line":9,"column":7},"identifierName":"B"}, + "name": "B" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":92,"end":140,"loc":{"start":{"line":9,"column":8},"end":{"line":13,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":96,"end":138,"loc":{"start":{"line":10,"column":2},"end":{"line":12,"column":3}}, + "static": false, + "key": { + "type": "Identifier", + "start":96,"end":100,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":6},"identifierName":"test"}, + "name": "test" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":103,"end":138,"loc":{"start":{"line":10,"column":9},"end":{"line":12,"column":3}}, + "body": [], + "directives": [], + "innerComments": [ + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":109,"end":134,"loc":{"start":{"line":11,"column":4},"end":{"line":11,"column":29}} + } + ] + } + } + ] + } + }, + { + "type": "ClassDeclaration", + "start":142,"end":193,"loc":{"start":{"line":15,"column":0},"end":{"line":18,"column":1}}, + "id": { + "type": "Identifier", + "start":148,"end":149,"loc":{"start":{"line":15,"column":6},"end":{"line":15,"column":7},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":150,"end":193,"loc":{"start":{"line":15,"column":8},"end":{"line":18,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":154,"end":163,"loc":{"start":{"line":16,"column":2},"end":{"line":16,"column":11}}, + "static": false, + "key": { + "type": "Identifier", + "start":154,"end":158,"loc":{"start":{"line":16,"column":2},"end":{"line":16,"column":6},"identifierName":"test"}, + "name": "test" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":161,"end":163,"loc":{"start":{"line":16,"column":9},"end":{"line":16,"column":11}}, + "body": [], + "directives": [] + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":166,"end":191,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":27}} + } + ] + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":25,"end":50,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}} + }, + { + "type": "CommentBlock", + "value": " Trailing comment ", + "start":58,"end":80,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":24}} + }, + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":109,"end":134,"loc":{"start":{"line":11,"column":4},"end":{"line":11,"column":29}} + }, + { + "type": "CommentLine", + "value": " this.member = 'value';", + "start":166,"end":191,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":27}} + } + ] +} diff --git a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-type-parameter-extends-type-parameter/output.js b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-type-parameter-extends-type-parameter/output.js index 45e9c5e696fa..13fdfbcd9ab7 100644 --- a/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-type-parameter-extends-type-parameter/output.js +++ b/packages/babel-plugin-transform-flow-comments/test/fixtures/flow-comments/class-type-parameter-extends-type-parameter/output.js @@ -24,7 +24,7 @@ Bar /*:: */ /*g*/ -{} -/*h*/ - +{ + /*h*/ +} /*i*/