diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 8b71cec739..7f6b6e2de6 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -122,6 +122,15 @@ export default class CommentsParser extends BaseParser { lastComment.end <= node.end ) { 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) { lastArg.trailingComments = this.state.leadingComments; this.state.leadingComments = []; diff --git a/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/input.js b/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/input.js new file mode 100644 index 0000000000..26ce4a7c80 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/input.js @@ -0,0 +1,6 @@ +test(function() { + var a = 1; + // one +} +// two +); diff --git a/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/output.json b/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/output.json new file mode 100644 index 0000000000..a04f1d2886 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/call-expression-function-argument/output.json @@ -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 + } + } + } + ] +} \ No newline at end of file