Leave trailing comments after handling a possible trailing com… (#10445)
* Leave trailing comments aftre handling a possible trailing comma * perf
This commit is contained in:
parent
962015f7e7
commit
3069747a81
@ -38,7 +38,19 @@ export default class CommentsParser extends BaseParser {
|
|||||||
this.state.leadingComments.push(comment);
|
this.state.leadingComments.push(comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
adjustCommentsAfterTrailingComma(node: Node, elements: Node[]) {
|
adjustCommentsAfterTrailingComma(
|
||||||
|
node: Node,
|
||||||
|
elements: Node[],
|
||||||
|
// When the current node is followed by a token which hasn't a respective AST node, we
|
||||||
|
// need to take all the trailing comments to prevent them from being attached to an
|
||||||
|
// unrelated node. e.g. in
|
||||||
|
// var { x } /* cmt */ = { y }
|
||||||
|
// we don't want /* cmt */ to be attached to { y }.
|
||||||
|
// On the other hand, in
|
||||||
|
// fn(x) [new line] /* cmt */ [new line] y
|
||||||
|
// /* cmt */ is both a trailing comment of fn(x) and a leading comment of y
|
||||||
|
takeAllComments?: boolean,
|
||||||
|
) {
|
||||||
if (this.state.leadingComments.length === 0) {
|
if (this.state.leadingComments.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -59,10 +71,16 @@ export default class CommentsParser extends BaseParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const newTrailingComments = [];
|
const newTrailingComments = [];
|
||||||
while (this.state.leadingComments.length) {
|
for (let i = 0; i < this.state.leadingComments.length; i++) {
|
||||||
const leadingComment = this.state.leadingComments.shift();
|
const leadingComment = this.state.leadingComments[i];
|
||||||
if (leadingComment.end < node.end) {
|
if (leadingComment.end < node.end) {
|
||||||
newTrailingComments.push(leadingComment);
|
newTrailingComments.push(leadingComment);
|
||||||
|
|
||||||
|
// Perf: we don't need to splice if we are going to reset the array anyway
|
||||||
|
if (!takeAllComments) {
|
||||||
|
this.state.leadingComments.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if (node.trailingComments === undefined) {
|
if (node.trailingComments === undefined) {
|
||||||
node.trailingComments = [];
|
node.trailingComments = [];
|
||||||
@ -70,6 +88,7 @@ export default class CommentsParser extends BaseParser {
|
|||||||
node.trailingComments.push(leadingComment);
|
node.trailingComments.push(leadingComment);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (takeAllComments) this.state.leadingComments = [];
|
||||||
|
|
||||||
if (newTrailingComments.length > 0) {
|
if (newTrailingComments.length > 0) {
|
||||||
lastElement.trailingComments = newTrailingComments;
|
lastElement.trailingComments = newTrailingComments;
|
||||||
@ -130,16 +149,20 @@ export default class CommentsParser extends BaseParser {
|
|||||||
if (firstChild) {
|
if (firstChild) {
|
||||||
switch (node.type) {
|
switch (node.type) {
|
||||||
case "ObjectExpression":
|
case "ObjectExpression":
|
||||||
case "ObjectPattern":
|
|
||||||
this.adjustCommentsAfterTrailingComma(node, node.properties);
|
this.adjustCommentsAfterTrailingComma(node, node.properties);
|
||||||
break;
|
break;
|
||||||
|
case "ObjectPattern":
|
||||||
|
this.adjustCommentsAfterTrailingComma(node, node.properties, true);
|
||||||
|
break;
|
||||||
case "CallExpression":
|
case "CallExpression":
|
||||||
this.adjustCommentsAfterTrailingComma(node, node.arguments);
|
this.adjustCommentsAfterTrailingComma(node, node.arguments);
|
||||||
break;
|
break;
|
||||||
case "ArrayExpression":
|
case "ArrayExpression":
|
||||||
case "ArrayPattern":
|
|
||||||
this.adjustCommentsAfterTrailingComma(node, node.elements);
|
this.adjustCommentsAfterTrailingComma(node, node.elements);
|
||||||
break;
|
break;
|
||||||
|
case "ArrayPattern":
|
||||||
|
this.adjustCommentsAfterTrailingComma(node, node.elements, true);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
} else if (
|
} else if (
|
||||||
this.state.commentPreviousNode &&
|
this.state.commentPreviousNode &&
|
||||||
@ -148,9 +171,11 @@ export default class CommentsParser extends BaseParser {
|
|||||||
(this.state.commentPreviousNode.type === "ExportSpecifier" &&
|
(this.state.commentPreviousNode.type === "ExportSpecifier" &&
|
||||||
node.type !== "ExportSpecifier"))
|
node.type !== "ExportSpecifier"))
|
||||||
) {
|
) {
|
||||||
this.adjustCommentsAfterTrailingComma(node, [
|
this.adjustCommentsAfterTrailingComma(
|
||||||
this.state.commentPreviousNode,
|
node,
|
||||||
]);
|
[this.state.commentPreviousNode],
|
||||||
|
true,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lastChild) {
|
if (lastChild) {
|
||||||
|
|||||||
@ -250,7 +250,25 @@
|
|||||||
"label": null
|
"label": null
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"test": null
|
"test": null,
|
||||||
|
"leadingComments": [
|
||||||
|
{
|
||||||
|
"type": "CommentLine",
|
||||||
|
"value": " comment",
|
||||||
|
"start": 47,
|
||||||
|
"end": 57,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 4
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 14
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -215,7 +215,25 @@
|
|||||||
"identifierName": "B"
|
"identifierName": "B"
|
||||||
},
|
},
|
||||||
"name": "B"
|
"name": "B"
|
||||||
}
|
},
|
||||||
|
"leadingComments": [
|
||||||
|
{
|
||||||
|
"type": "CommentLine",
|
||||||
|
"value": " Two",
|
||||||
|
"start": 27,
|
||||||
|
"end": 33,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 6,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 6,
|
||||||
|
"column": 6
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"directives": []
|
"directives": []
|
||||||
|
|||||||
5
packages/babel-parser/test/fixtures/comments/regression/10432/input.js
vendored
Normal file
5
packages/babel-parser/test/fixtures/comments/regression/10432/input.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
const socket = socketClient(address)
|
||||||
|
/* istanbul ignore next */
|
||||||
|
socket.on('connect', function () {
|
||||||
|
debug('Connected to ' + address)
|
||||||
|
})
|
||||||
431
packages/babel-parser/test/fixtures/comments/regression/10432/output.json
vendored
Normal file
431
packages/babel-parser/test/fixtures/comments/regression/10432/output.json
vendored
Normal file
@ -0,0 +1,431 @@
|
|||||||
|
{
|
||||||
|
"type": "File",
|
||||||
|
"start": 0,
|
||||||
|
"end": 136,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"program": {
|
||||||
|
"type": "Program",
|
||||||
|
"start": 0,
|
||||||
|
"end": 136,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"sourceType": "script",
|
||||||
|
"interpreter": null,
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclaration",
|
||||||
|
"start": 0,
|
||||||
|
"end": 36,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 36
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"declarations": [
|
||||||
|
{
|
||||||
|
"type": "VariableDeclarator",
|
||||||
|
"start": 6,
|
||||||
|
"end": 36,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 36
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 6,
|
||||||
|
"end": 12,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 12
|
||||||
|
},
|
||||||
|
"identifierName": "socket"
|
||||||
|
},
|
||||||
|
"name": "socket"
|
||||||
|
},
|
||||||
|
"init": {
|
||||||
|
"type": "CallExpression",
|
||||||
|
"start": 15,
|
||||||
|
"end": 36,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 36
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"callee": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 15,
|
||||||
|
"end": 27,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 15
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 27
|
||||||
|
},
|
||||||
|
"identifierName": "socketClient"
|
||||||
|
},
|
||||||
|
"name": "socketClient"
|
||||||
|
},
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 28,
|
||||||
|
"end": 35,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 28
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 1,
|
||||||
|
"column": 35
|
||||||
|
},
|
||||||
|
"identifierName": "address"
|
||||||
|
},
|
||||||
|
"name": "address"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kind": "const",
|
||||||
|
"trailingComments": [
|
||||||
|
{
|
||||||
|
"type": "CommentBlock",
|
||||||
|
"value": " istanbul ignore next ",
|
||||||
|
"start": 37,
|
||||||
|
"end": 63,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start": 64,
|
||||||
|
"end": 136,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"expression": {
|
||||||
|
"type": "CallExpression",
|
||||||
|
"start": 64,
|
||||||
|
"end": 136,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 2
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"callee": {
|
||||||
|
"type": "MemberExpression",
|
||||||
|
"start": 64,
|
||||||
|
"end": 73,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 9
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"object": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 64,
|
||||||
|
"end": 70,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 6
|
||||||
|
},
|
||||||
|
"identifierName": "socket"
|
||||||
|
},
|
||||||
|
"name": "socket"
|
||||||
|
},
|
||||||
|
"property": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 71,
|
||||||
|
"end": 73,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 9
|
||||||
|
},
|
||||||
|
"identifierName": "on"
|
||||||
|
},
|
||||||
|
"name": "on"
|
||||||
|
},
|
||||||
|
"computed": false
|
||||||
|
},
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 74,
|
||||||
|
"end": 83,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 10
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 19
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "connect",
|
||||||
|
"raw": "'connect'"
|
||||||
|
},
|
||||||
|
"value": "connect"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "FunctionExpression",
|
||||||
|
"start": 85,
|
||||||
|
"end": 135,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 21
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"id": null,
|
||||||
|
"generator": false,
|
||||||
|
"async": false,
|
||||||
|
"params": [],
|
||||||
|
"body": {
|
||||||
|
"type": "BlockStatement",
|
||||||
|
"start": 97,
|
||||||
|
"end": 135,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 3,
|
||||||
|
"column": 33
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 5,
|
||||||
|
"column": 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"body": [
|
||||||
|
{
|
||||||
|
"type": "ExpressionStatement",
|
||||||
|
"start": 101,
|
||||||
|
"end": 133,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 34
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"expression": {
|
||||||
|
"type": "CallExpression",
|
||||||
|
"start": 101,
|
||||||
|
"end": 133,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 34
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"callee": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 101,
|
||||||
|
"end": 106,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 2
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 7
|
||||||
|
},
|
||||||
|
"identifierName": "debug"
|
||||||
|
},
|
||||||
|
"name": "debug"
|
||||||
|
},
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"type": "BinaryExpression",
|
||||||
|
"start": 107,
|
||||||
|
"end": 132,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 8
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 33
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"left": {
|
||||||
|
"type": "StringLiteral",
|
||||||
|
"start": 107,
|
||||||
|
"end": 122,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 8
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 23
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"extra": {
|
||||||
|
"rawValue": "Connected to ",
|
||||||
|
"raw": "'Connected to '"
|
||||||
|
},
|
||||||
|
"value": "Connected to "
|
||||||
|
},
|
||||||
|
"operator": "+",
|
||||||
|
"right": {
|
||||||
|
"type": "Identifier",
|
||||||
|
"start": 125,
|
||||||
|
"end": 132,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 26
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 4,
|
||||||
|
"column": 33
|
||||||
|
},
|
||||||
|
"identifierName": "address"
|
||||||
|
},
|
||||||
|
"name": "address"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"leadingComments": [
|
||||||
|
{
|
||||||
|
"type": "CommentBlock",
|
||||||
|
"value": " istanbul ignore next ",
|
||||||
|
"start": 37,
|
||||||
|
"end": 63,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"directives": []
|
||||||
|
},
|
||||||
|
"comments": [
|
||||||
|
{
|
||||||
|
"type": "CommentBlock",
|
||||||
|
"value": " istanbul ignore next ",
|
||||||
|
"start": 37,
|
||||||
|
"end": 63,
|
||||||
|
"loc": {
|
||||||
|
"start": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 0
|
||||||
|
},
|
||||||
|
"end": {
|
||||||
|
"line": 2,
|
||||||
|
"column": 26
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user