Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leave trailing comments after handling a possible trailing comma #10445

Merged
merged 2 commits into from Sep 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -38,7 +38,19 @@ export default class CommentsParser extends BaseParser {
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) {
return;
}
Expand All @@ -59,17 +71,20 @@ export default class CommentsParser extends BaseParser {
}

const newTrailingComments = [];
while (this.state.leadingComments.length) {
const leadingComment = this.state.leadingComments.shift();
for (let i = 0; i < this.state.leadingComments.length; i++) {
const leadingComment = this.state.leadingComments[i];
if (leadingComment.end < node.end) {
newTrailingComments.push(leadingComment);
this.state.leadingComments.splice(i, 1);
i--;
} else {
if (node.trailingComments === undefined) {
node.trailingComments = [];
}
node.trailingComments.push(leadingComment);
}
}
if (takeAllComments) this.state.leadingComments.length = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this.state.leadingComments = [] is more idiomatic and should be marginally faster than this.state.leadingComments.length = 0 by saving calls to StoreICTrampoline.
The former is compiled to

                  -- </Users/jh/code/babel/packages/babel-parser/lib/index.js:6266:14> --
0x1fafdfb87845   545  48b8a92cd4eaa8090000 REX.W movq rax,0x9a8ead42ca9    ;; object: 0x09a8ead42ca9 <Map(PACKED_ELEMENTS)>
0x1fafdfb8784f   54f  498947ff       REX.W movq [r15-0x1],rax
0x1fafdfb87853   553  498b9da0000000 REX.W movq rbx,[r13+0xa0] (root (empty_fixed_array))
0x1fafdfb8785a   55a  49895f07       REX.W movq [r15+0x7],rbx
0x1fafdfb8785e   55e  49895f0f       REX.W movq [r15+0xf],rbx
0x1fafdfb87862   562  49c7471700000000 REX.W movq [r15+0x17],0x0
                  -- </Users/jh/code/babel/packages/babel-parser/lib/index.js:6266:37> --
0x1fafdfb8786a   56a  4d89b9cf000000 REX.W movq [r9+0xcf],r15
0x1fafdfb87871   571  48c7c30000fcff REX.W movq rbx,0xfffc0000
0x1fafdfb87878   578  4923d9         REX.W andq rbx,r9
0x1fafdfb8787b   57b  f6430804       testb [rbx+0x8],0x4

The latter is compiled to

                  -- </Users/jh/code/babel/packages/babel-parser/lib/index.js:6268:44> --
0x1fafde852e4e   44e  33c0           xorl rax,rax
0x1fafde852e50   450  498b8d280a0000 REX.W movq rcx,[r13+0xa28] (root (length_string))
0x1fafde852e57   457  48bf000000002b000000 REX.W movq rdi,0x2b00000000
0x1fafde852e61   461  488b75e0       REX.W movq rsi,[rbp-0x20]
                  -- Inlined Trampoline to StoreICTrampoline --
0x1fafde852e65   465  49ba40fb6c0001000000 REX.W movq r10,0x1006cfb40  (StoreICTrampoline)
0x1fafde852e6f   46f  41ffd2         call r10
0x1fafdfb877aa   4aa  4c8b4518       REX.W movq r8,[rbp+0x18]
0x1fafdfb877ae   4ae  48b8a92cd4eaa8090000 REX.W movq rax,0x9a8ead42ca9    ;; object: 0x09a8ead42ca9 <Map(PACKED_ELEMENTS)>
0x1fafdfb877b8   4b8  49bce990a15ea8090000 REX.W movq r12,0x9a85ea190e9    ;; object: 0x09a85ea190e9 <Map(HOLEY_ELEMENTS)>
0x1fafdfb877c2   4c2  49bbf994a15ea8090000 REX.W movq r11,0x9a85ea194f9    ;; object: 0x09a85ea194f9 <Map(HOLEY_ELEMENTS)>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I thought that resetting the length was faster because it doesn't create a new array. Will update 👍


if (newTrailingComments.length > 0) {
lastElement.trailingComments = newTrailingComments;
Expand Down Expand Up @@ -130,16 +145,20 @@ export default class CommentsParser extends BaseParser {
if (firstChild) {
switch (node.type) {
case "ObjectExpression":
case "ObjectPattern":
this.adjustCommentsAfterTrailingComma(node, node.properties);
break;
case "ObjectPattern":
this.adjustCommentsAfterTrailingComma(node, node.properties, true);
break;
case "CallExpression":
this.adjustCommentsAfterTrailingComma(node, node.arguments);
break;
case "ArrayExpression":
case "ArrayPattern":
this.adjustCommentsAfterTrailingComma(node, node.elements);
break;
case "ArrayPattern":
this.adjustCommentsAfterTrailingComma(node, node.elements, true);
break;
}
} else if (
this.state.commentPreviousNode &&
Expand All @@ -148,9 +167,11 @@ export default class CommentsParser extends BaseParser {
(this.state.commentPreviousNode.type === "ExportSpecifier" &&
node.type !== "ExportSpecifier"))
) {
this.adjustCommentsAfterTrailingComma(node, [
this.state.commentPreviousNode,
]);
this.adjustCommentsAfterTrailingComma(
node,
[this.state.commentPreviousNode],
true,
);
}

if (lastChild) {
Expand Down
Expand Up @@ -250,7 +250,25 @@
"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
}
}
}
]
}
]
}
Expand Down
Expand Up @@ -215,7 +215,25 @@
"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": []
Expand Down
@@ -0,0 +1,5 @@
const socket = socketClient(address)
/* istanbul ignore next */
socket.on('connect', function () {
debug('Connected to ' + address)
})