Fix innercomments (#11697)
This commit is contained in:
parent
183acbae25
commit
e15a5c7509
@ -263,10 +263,25 @@ export default class CommentsParser extends BaseParser {
|
|||||||
last(trailingComments).end <= node.end
|
last(trailingComments).end <= node.end
|
||||||
) {
|
) {
|
||||||
node.innerComments = trailingComments;
|
node.innerComments = trailingComments;
|
||||||
|
} else {
|
||||||
|
// 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 {
|
} else {
|
||||||
node.trailingComments = trailingComments;
|
node.trailingComments = trailingComments;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
stack.push(node);
|
stack.push(node);
|
||||||
}
|
}
|
||||||
|
|||||||
18
packages/babel-parser/test/fixtures/comments/regression/11469/input.js
vendored
Normal file
18
packages/babel-parser/test/fixtures/comments/regression/11469/input.js
vendored
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
class A {
|
||||||
|
test() {
|
||||||
|
// this.member = 'value';
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Trailing comment */
|
||||||
|
}
|
||||||
|
|
||||||
|
class B {
|
||||||
|
test() {
|
||||||
|
// this.member = 'value';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class C {
|
||||||
|
test() {}
|
||||||
|
// this.member = 'value';
|
||||||
|
}
|
||||||
177
packages/babel-parser/test/fixtures/comments/regression/11469/output.json
vendored
Normal file
177
packages/babel-parser/test/fixtures/comments/regression/11469/output.json
vendored
Normal file
@ -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}}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@ -24,7 +24,7 @@ Bar
|
|||||||
/*:: <R>*/
|
/*:: <R>*/
|
||||||
|
|
||||||
/*g*/
|
/*g*/
|
||||||
{}
|
{
|
||||||
/*h*/
|
/*h*/
|
||||||
|
}
|
||||||
/*i*/
|
/*i*/
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user