Fix trailingComments for FunctionExpression that is CallExpression arguments (#8488)

This commit is contained in:
Yi Li 2018-08-22 01:29:11 +08:00 committed by Brian Ng
parent f8b0a5579f
commit b439013cd4
3 changed files with 276 additions and 0 deletions

View File

@ -122,6 +122,15 @@ export default class CommentsParser extends BaseParser {
lastComment.end <= node.end lastComment.end <= node.end
) { ) {
if (this.state.commentPreviousNode) { if (this.state.commentPreviousNode) {
for (j = 0; j < this.state.leadingComments.length; j++) {
if (
this.state.leadingComments[j].end <
this.state.commentPreviousNode.end
) {
this.state.leadingComments.splice(j, 1);
j--;
}
}
if (this.state.leadingComments.length > 0) { if (this.state.leadingComments.length > 0) {
lastArg.trailingComments = this.state.leadingComments; lastArg.trailingComments = this.state.leadingComments;
this.state.leadingComments = []; this.state.leadingComments = [];

View File

@ -0,0 +1,6 @@
test(function() {
var a = 1;
// one
}
// two
);

View File

@ -0,0 +1,261 @@
{
"type": "File",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 51,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 2
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 50,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 6,
"column": 1
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "test"
},
"name": "test"
},
"arguments": [
{
"type": "FunctionExpression",
"start": 5,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 4,
"column": 1
}
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 41,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 20,
"end": 30,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 12
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 24,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 11
}
},
"id": {
"type": "Identifier",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 7
},
"identifierName": "a"
},
"name": "a"
},
"init": {
"type": "NumericLiteral",
"start": 28,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 11
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "var",
"trailingComments": [
{
"type": "CommentLine",
"value": " one",
"start": 33,
"end": 39,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
}
}
]
}
],
"directives": []
},
"trailingComments": [
{
"type": "CommentLine",
"value": " two",
"start": 42,
"end": 48,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 6
}
}
}
]
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " one",
"start": 33,
"end": 39,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
}
},
{
"type": "CommentLine",
"value": " two",
"start": 42,
"end": 48,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 5,
"column": 6
}
}
}
]
}