Refactor trailing comma comment adjustment (#10380)
* Refactor trailing comment adjustment Following up from https://github.com/babel/babel/pull/10369 - Unify the logic for adjusting trailing comments into a separate function - Use it for all three cases, which fixes a bug for ObjectExpressions and CallExpressions - Update tests to check for the fixed bug * Fix tests - Only modify trailingComments if necessary - Update snapshots of a couple of affected tests * Drop the underscore in adjustCommentsAfterTrailingComma_ * Handle ArrayPattern * Handle ObjectPattern * Handle import and export declarations These have to be handled a bit differently, because the node is visited after the and before the declaration. So we intercept when we are going from the last specifier to the source node. * Remove unnecessary check
This commit is contained in:
committed by
Huáng Jùnliàng
parent
e5afa57cca
commit
c0e3fa0081
6
packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/input.js
vendored
Normal file
6
packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/input.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
const {
|
||||
/* One */
|
||||
x
|
||||
/* Two */,
|
||||
/* Three */
|
||||
} /* Four */ = {};
|
||||
292
packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/output.json
vendored
Normal file
292
packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/output.json
vendored
Normal file
@@ -0,0 +1,292 @@
|
||||
{
|
||||
"type": "File",
|
||||
"start": 0,
|
||||
"end": 69,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"program": {
|
||||
"type": "Program",
|
||||
"start": 0,
|
||||
"end": 69,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"sourceType": "script",
|
||||
"interpreter": null,
|
||||
"body": [
|
||||
{
|
||||
"type": "VariableDeclaration",
|
||||
"start": 0,
|
||||
"end": 69,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 0
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 18
|
||||
}
|
||||
},
|
||||
"declarations": [
|
||||
{
|
||||
"type": "VariableDeclarator",
|
||||
"start": 6,
|
||||
"end": 68,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 17
|
||||
}
|
||||
},
|
||||
"id": {
|
||||
"type": "ObjectPattern",
|
||||
"start": 6,
|
||||
"end": 52,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 1,
|
||||
"column": 6
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 1
|
||||
}
|
||||
},
|
||||
"properties": [
|
||||
{
|
||||
"type": "ObjectProperty",
|
||||
"start": 22,
|
||||
"end": 23,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 3
|
||||
}
|
||||
},
|
||||
"method": false,
|
||||
"key": {
|
||||
"type": "Identifier",
|
||||
"start": 22,
|
||||
"end": 23,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x"
|
||||
},
|
||||
"computed": false,
|
||||
"shorthand": true,
|
||||
"value": {
|
||||
"type": "Identifier",
|
||||
"start": 22,
|
||||
"end": 23,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 3,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 3,
|
||||
"column": 3
|
||||
},
|
||||
"identifierName": "x"
|
||||
},
|
||||
"name": "x"
|
||||
},
|
||||
"leadingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " One ",
|
||||
"start": 10,
|
||||
"end": 19,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Two ",
|
||||
"start": 26,
|
||||
"end": 35,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 11
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Three ",
|
||||
"start": 39,
|
||||
"end": 50,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 13
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"extra": {
|
||||
"shorthand": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"trailingComments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Four ",
|
||||
"start": 53,
|
||||
"end": 63,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 6,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 12
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"init": {
|
||||
"type": "ObjectExpression",
|
||||
"start": 66,
|
||||
"end": 68,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 6,
|
||||
"column": 15
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 17
|
||||
}
|
||||
},
|
||||
"properties": []
|
||||
}
|
||||
}
|
||||
],
|
||||
"kind": "const"
|
||||
}
|
||||
],
|
||||
"directives": []
|
||||
},
|
||||
"comments": [
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " One ",
|
||||
"start": 10,
|
||||
"end": 19,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 2,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 2,
|
||||
"column": 11
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Two ",
|
||||
"start": 26,
|
||||
"end": 35,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 4,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 4,
|
||||
"column": 11
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Three ",
|
||||
"start": 39,
|
||||
"end": 50,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 5,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 5,
|
||||
"column": 13
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "CommentBlock",
|
||||
"value": " Four ",
|
||||
"start": 53,
|
||||
"end": 63,
|
||||
"loc": {
|
||||
"start": {
|
||||
"line": 6,
|
||||
"column": 2
|
||||
},
|
||||
"end": {
|
||||
"line": 6,
|
||||
"column": 12
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user