diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 7f6b6e2de6e2..5ef57efe2fd9 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -91,7 +91,7 @@ export default class CommentsParser extends BaseParser { const lastComment = last(this.state.leadingComments); if (firstChild.type === "ObjectProperty") { - if (lastComment.start >= node.start) { + if (lastComment.start >= node.start && lastComment.end <= node.end) { if (this.state.commentPreviousNode) { for (j = 0; j < this.state.leadingComments.length; j++) { if ( diff --git a/packages/babel-parser/test/fixtures/comments/regression/10230/input.js b/packages/babel-parser/test/fixtures/comments/regression/10230/input.js new file mode 100644 index 000000000000..131dbd7a00f7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/10230/input.js @@ -0,0 +1,7 @@ +A = { + a: 42, + // One +} + +// Two +B diff --git a/packages/babel-parser/test/fixtures/comments/regression/10230/output.json b/packages/babel-parser/test/fixtures/comments/regression/10230/output.json new file mode 100644 index 000000000000..0c3dcfdf9744 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/regression/10230/output.json @@ -0,0 +1,239 @@ +{ + "type": "File", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "expression": { + "type": "AssignmentExpression", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "operator": "=", + "left": { + "type": "Identifier", + "start": 0, + "end": 1, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + }, + "identifierName": "A" + }, + "name": "A" + }, + "right": { + "type": "ObjectExpression", + "start": 4, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 8, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "a" + }, + "name": "a" + }, + "computed": false, + "shorthand": false, + "value": { + "type": "NumericLiteral", + "start": 11, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 5 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "expression": { + "type": "Identifier", + "start": 34, + "end": 35, + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + }, + "identifierName": "B" + }, + "name": "B" + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " Two", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 6 + } + } + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " One", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + } + }, + { + "type": "CommentLine", + "value": " Two", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 6 + } + } + } + ] +} \ No newline at end of file