Skip to content

Commit

Permalink
Fix innercomments (#11697)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaodahong committed Jun 13, 2020
1 parent 183acba commit e15a5c7
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 4 deletions.
17 changes: 16 additions & 1 deletion packages/babel-parser/src/parser/comments.js
Expand Up @@ -264,7 +264,22 @@ export default class CommentsParser extends BaseParser {
) {
node.innerComments = trailingComments;
} else {
node.trailingComments = trailingComments;
// TrailingComments maybe contain innerComments
const firstTrailingCommentIndex = trailingComments.findIndex(
comment => comment.end >= node.end,
);

if (firstTrailingCommentIndex > 0) {
node.innerComments = trailingComments.slice(
0,
firstTrailingCommentIndex,
);
node.trailingComments = trailingComments.slice(
firstTrailingCommentIndex,
);
} else {
node.trailingComments = trailingComments;
}
}
}

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

/* Trailing comment */
}

class B {
test() {
// this.member = 'value';
}
}

class C {
test() {}
// this.member = 'value';
}
@@ -0,0 +1,177 @@
{
"type": "File",
"start":0,"end":193,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"column":1}},
"program": {
"type": "Program",
"start":0,"end":193,"loc":{"start":{"line":1,"column":0},"end":{"line":18,"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}}
}
]
}
]
}
},
{
"type": "ClassDeclaration",
"start":84,"end":140,"loc":{"start":{"line":9,"column":0},"end":{"line":13,"column":1}},
"id": {
"type": "Identifier",
"start":90,"end":91,"loc":{"start":{"line":9,"column":6},"end":{"line":9,"column":7},"identifierName":"B"},
"name": "B"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":92,"end":140,"loc":{"start":{"line":9,"column":8},"end":{"line":13,"column":1}},
"body": [
{
"type": "ClassMethod",
"start":96,"end":138,"loc":{"start":{"line":10,"column":2},"end":{"line":12,"column":3}},
"static": false,
"key": {
"type": "Identifier",
"start":96,"end":100,"loc":{"start":{"line":10,"column":2},"end":{"line":10,"column":6},"identifierName":"test"},
"name": "test"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":103,"end":138,"loc":{"start":{"line":10,"column":9},"end":{"line":12,"column":3}},
"body": [],
"directives": [],
"innerComments": [
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":109,"end":134,"loc":{"start":{"line":11,"column":4},"end":{"line":11,"column":29}}
}
]
}
}
]
}
},
{
"type": "ClassDeclaration",
"start":142,"end":193,"loc":{"start":{"line":15,"column":0},"end":{"line":18,"column":1}},
"id": {
"type": "Identifier",
"start":148,"end":149,"loc":{"start":{"line":15,"column":6},"end":{"line":15,"column":7},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":150,"end":193,"loc":{"start":{"line":15,"column":8},"end":{"line":18,"column":1}},
"body": [
{
"type": "ClassMethod",
"start":154,"end":163,"loc":{"start":{"line":16,"column":2},"end":{"line":16,"column":11}},
"static": false,
"key": {
"type": "Identifier",
"start":154,"end":158,"loc":{"start":{"line":16,"column":2},"end":{"line":16,"column":6},"identifierName":"test"},
"name": "test"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":161,"end":163,"loc":{"start":{"line":16,"column":9},"end":{"line":16,"column":11}},
"body": [],
"directives": []
},
"trailingComments": [
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":166,"end":191,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":27}}
}
]
}
]
}
}
],
"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}}
},
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":109,"end":134,"loc":{"start":{"line":11,"column":4},"end":{"line":11,"column":29}}
},
{
"type": "CommentLine",
"value": " this.member = 'value';",
"start":166,"end":191,"loc":{"start":{"line":17,"column":2},"end":{"line":17,"column":27}}
}
]
}
Expand Up @@ -24,7 +24,7 @@ Bar
/*:: <R>*/

/*g*/
{}
/*h*/

{
/*h*/
}
/*i*/

0 comments on commit e15a5c7

Please sign in to comment.