From ac936e315a04ae31e3d59c9cd4afa45d4e2e89e9 Mon Sep 17 00:00:00 2001 From: aardito2 Date: Sun, 18 Jun 2017 16:57:42 -0400 Subject: [PATCH] Fix comment attachment for call expressions (#575) --- src/parser/comments.js | 37 ++- .../actual.js | 5 + .../expected.json | 245 ++++++++++++++ .../actual.js | 7 + .../expected.json | 298 ++++++++++++++++++ 5 files changed, 579 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/actual.js create mode 100644 test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/expected.json create mode 100644 test/fixtures/comments/basic/switch-function-call-no-semicolon/actual.js create mode 100644 test/fixtures/comments/basic/switch-function-call-no-semicolon/expected.json diff --git a/src/parser/comments.js b/src/parser/comments.js index 9f06446030..328c4d9abd 100644 --- a/src/parser/comments.js +++ b/src/parser/comments.js @@ -86,23 +86,34 @@ export default class CommentsParser extends BaseParser { // Attach comments that follow a trailing comma on the last // property in an object literal or a trailing comma in function arguments // as trailing comments - if (firstChild && - (firstChild.type === "ObjectProperty" || - (node.type === "CallExpression")) && - this.state.leadingComments.length > 0) { + if (firstChild && this.state.leadingComments.length > 0) { const lastComment = last(this.state.leadingComments); - if (lastComment.start >= node.start) { - 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 (firstChild.type === "ObjectProperty") { + if (lastComment.start >= node.start) { + 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) { + firstChild.trailingComments = this.state.leadingComments; + this.state.leadingComments = []; } } + } + } else if (node.type === "CallExpression" && node.arguments && node.arguments.length) { + const lastArg = last(node.arguments); - if (this.state.leadingComments.length > 0) { - firstChild.trailingComments = this.state.leadingComments; - this.state.leadingComments = []; + if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) { + if (this.state.commentPreviousNode) { + if (this.state.leadingComments.length > 0) { + lastArg.trailingComments = this.state.leadingComments; + this.state.leadingComments = []; + } } } } diff --git a/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/actual.js b/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/actual.js new file mode 100644 index 0000000000..0e633e18a0 --- /dev/null +++ b/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/actual.js @@ -0,0 +1,5 @@ +switch (x) { + case 1: + console.log('1') + // comment +} diff --git a/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/expected.json b/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/expected.json new file mode 100644 index 0000000000..afde57632b --- /dev/null +++ b/test/fixtures/comments/basic/switch-function-call-no-semicolon-no-default/expected.json @@ -0,0 +1,245 @@ +{ + "type": "File", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "SwitchStatement", + "start": 0, + "end": 60, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "discriminant": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "x" + }, + "name": "x" + }, + "cases": [ + { + "type": "SwitchCase", + "start": 15, + "end": 43, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "start": 27, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "expression": { + "type": "CallExpression", + "start": 27, + "end": 43, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "callee": { + "type": "MemberExpression", + "start": 27, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "object": { + "type": "Identifier", + "start": 27, + "end": 34, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "console" + }, + "name": "console" + }, + "property": { + "type": "Identifier", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "log" + }, + "name": "log" + }, + "computed": false + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 39, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "extra": { + "rawValue": "1", + "raw": "'1'" + }, + "value": "1" + } + ], + "trailingComments": null + }, + "trailingComments": null + } + ], + "test": { + "type": "NumericLiteral", + "start": 20, + "end": 21, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " comment", + "start": 48, + "end": 58, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + ] + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " comment", + "start": 48, + "end": 58, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + ] +} \ No newline at end of file diff --git a/test/fixtures/comments/basic/switch-function-call-no-semicolon/actual.js b/test/fixtures/comments/basic/switch-function-call-no-semicolon/actual.js new file mode 100644 index 0000000000..b00d8168f1 --- /dev/null +++ b/test/fixtures/comments/basic/switch-function-call-no-semicolon/actual.js @@ -0,0 +1,7 @@ +switch(x) { + case 1: + console.log('1') + // comment + default: + break; +} diff --git a/test/fixtures/comments/basic/switch-function-call-no-semicolon/expected.json b/test/fixtures/comments/basic/switch-function-call-no-semicolon/expected.json new file mode 100644 index 0000000000..5f05d417be --- /dev/null +++ b/test/fixtures/comments/basic/switch-function-call-no-semicolon/expected.json @@ -0,0 +1,298 @@ +{ + "type": "File", + "start": 0, + "end": 81, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 81, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "SwitchStatement", + "start": 0, + "end": 81, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 7, + "column": 1 + } + }, + "discriminant": { + "type": "Identifier", + "start": 7, + "end": 8, + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + }, + "identifierName": "x" + }, + "name": "x" + }, + "cases": [ + { + "type": "SwitchCase", + "start": 14, + "end": 42, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "consequent": [ + { + "type": "ExpressionStatement", + "start": 26, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "expression": { + "type": "CallExpression", + "start": 26, + "end": 42, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 20 + } + }, + "callee": { + "type": "MemberExpression", + "start": 26, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 15 + } + }, + "object": { + "type": "Identifier", + "start": 26, + "end": 33, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 11 + }, + "identifierName": "console" + }, + "name": "console" + }, + "property": { + "type": "Identifier", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 3, + "column": 12 + }, + "end": { + "line": 3, + "column": 15 + }, + "identifierName": "log" + }, + "name": "log" + }, + "computed": false + }, + "arguments": [ + { + "type": "StringLiteral", + "start": 38, + "end": 41, + "loc": { + "start": { + "line": 3, + "column": 16 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "extra": { + "rawValue": "1", + "raw": "'1'" + }, + "value": "1" + } + ], + "trailingComments": null + }, + "trailingComments": null + } + ], + "test": { + "type": "NumericLiteral", + "start": 19, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " comment", + "start": 47, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + ] + }, + { + "type": "SwitchCase", + "start": 60, + "end": 79, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 6, + "column": 10 + } + }, + "consequent": [ + { + "type": "BreakStatement", + "start": 73, + "end": 79, + "loc": { + "start": { + "line": 6, + "column": 4 + }, + "end": { + "line": 6, + "column": 10 + } + }, + "label": null, + "leadingComments": null + } + ], + "test": null, + "leadingComments": [ + { + "type": "CommentLine", + "value": " comment", + "start": 47, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + ] + } + ] + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " comment", + "start": 47, + "end": 57, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 14 + } + } + } + ] +} \ No newline at end of file