Skip to content

Commit

Permalink
fix: assign comment to ObjectProperty only when inside an ObjectExpre…
Browse files Browse the repository at this point in the history
…ssion (#10292)

Fixes #10230
  • Loading branch information
JLHwung authored and existentialism committed Aug 13, 2019
1 parent 22df2ae commit 75090f1
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/comments.js
Expand Up @@ -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 (
Expand Down
@@ -0,0 +1,7 @@
A = {
a: 42,
// One
}

// Two
B
@@ -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
}
}
}
]
}

0 comments on commit 75090f1

Please sign in to comment.