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

Fix innercomments #11697

Merged
merged 5 commits into from Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
12 changes: 11 additions & 1 deletion packages/babel-parser/src/parser/comments.js
Expand Up @@ -264,7 +264,17 @@ export default class CommentsParser extends BaseParser {
) {
node.innerComments = trailingComments;
} else {
node.trailingComments = trailingComments;
// TrailingComments maybe contain innerComments
const findIndex = trailingComments.findIndex(
shaodahong marked this conversation as resolved.
Show resolved Hide resolved
comment => comment.end >= node.end,
);

if (findIndex !== -1 && findIndex !== 0) {
kaicataldo marked this conversation as resolved.
Show resolved Hide resolved
node.innerComments = trailingComments.slice(0, findIndex);
node.trailingComments = trailingComments.slice(findIndex);
} else {
node.trailingComments = trailingComments;
}
}
}

Expand Down
@@ -0,0 +1,7 @@
class A {
test() {
// this.member = 'value';
}

/* Trailing comment */
}
@@ -0,0 +1,77 @@
{
"type": "File",
"start":0,"end":82,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"program": {
"type": "Program",
"start":0,"end":82,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":82,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":82,"loc":{"start":{"line":1,"column":8},"end":{"line":7,"column":1}},
"body": [
{
"type": "ClassMethod",
"start":12,"end":54,"loc":{"start":{"line":2,"column":2},"end":{"line":4,"column":3}},
"static": false,
"key": {
"type": "Identifier",
"start":12,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"test"},
"name": "test"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":19,"end":54,"loc":{"start":{"line":2,"column":9},"end":{"line":4,"column":3}},
"body": [],
"directives": [],
"innerComments": [
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":25,"end":50,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}}
}
]
},
"trailingComments": [
{
"type": "CommentBlock",
"value": " Trailing comment ",
"start":58,"end":80,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":24}}
}
]
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":25,"end":50,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}}
},
{
"type": "CommentBlock",
"value": " Trailing comment ",
"start":58,"end":80,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":24}}
}
]
}
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved