diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 60f3c1a23f9b..a1d3102e4d25 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -38,6 +38,42 @@ export default class CommentsParser extends BaseParser { this.state.leadingComments.push(comment); } + adjustCommentsAfterTrailingComma_(node: Node, elements: Node[]) { + if (elements.length === 0) { + return; + } + if (this.state.leadingComments.length === 0) { + return; + } + if (!this.state.commentPreviousNode) { + return; + } + + const lastElement = last(elements); + + for (let 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--; + } + } + + lastElement.trailingComments = []; + while (this.state.leadingComments.length) { + const leadingComment = this.state.leadingComments.shift(); + if (leadingComment.end < node.end) { + lastElement.trailingComments.push(leadingComment); + } else { + if (node.trailingComments === undefined) { + node.trailingComments = []; + } + node.trailingComments.push(leadingComment); + } + } + } + processComment(node: Node): void { if (node.type === "Program" && node.body.length > 0) return; @@ -84,85 +120,20 @@ export default class CommentsParser extends BaseParser { if (!lastChild && firstChild) lastChild = firstChild; - // Attach comments that follow a trailing comma on the last - // property in an object literal or a trailing comma in function arguments - // or a trailing comma in array expressions as trailing comments - if (firstChild && this.state.leadingComments.length > 0) { - const lastComment = last(this.state.leadingComments); - - if (firstChild.type === "ObjectProperty") { - if (lastComment.start >= node.start && 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) { - firstChild.trailingComments = this.state.leadingComments; - this.state.leadingComments = []; - } - } - } - } else if ( - node.type === "CallExpression" && - node.arguments && - node.arguments.length - ) { - const lastArg = last(node.arguments); - - if ( - lastArg && - lastComment.start >= lastArg.start && - 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 = []; - } - } - } - } else if (node.type === "ArrayExpression" && node.elements.length > 0) { - 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--; - } - } - - const lastElement = last(node.elements); - lastElement.trailingComments = []; - while (this.state.leadingComments.length) { - const leadingComment = this.state.leadingComments.shift(); - if (leadingComment.end < node.end) { - lastElement.trailingComments.push(leadingComment); - } else { - if (node.trailingComments === undefined) { - node.trailingComments = []; - } - node.trailingComments.push(leadingComment); - } - } - } + // Adjust comments that follow a trailing comma on the last element in a + // comma separated list of nodes to be the trailing comments on the last + // element + if (firstChild) { + switch (node.type) { + case "ObjectExpression": + this.adjustCommentsAfterTrailingComma_(node, node.properties); + break; + case "CallExpression": + this.adjustCommentsAfterTrailingComma_(node, node.arguments); + break; + case "ArrayExpression": + this.adjustCommentsAfterTrailingComma_(node, node.elements); + break; } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/input.js b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/input.js index 0936c59b4e55..cbeebe5ee3b6 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/input.js +++ b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/input.js @@ -1 +1 @@ -fn(a, { b }, /* comment */); +fn(a, { b }, /* comment 1 */) /* comment 2 */; diff --git a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/output.json b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/output.json index ecc67f22abd0..3e22bc1678e5 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma-shorthand/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 28, + "end": 46, "loc": { "start": { "line": 1, @@ -9,13 +9,13 @@ }, "end": { "line": 1, - "column": 28 + "column": 46 } }, "program": { "type": "Program", "start": 0, - "end": 28, + "end": 46, "loc": { "start": { "line": 1, @@ -23,7 +23,7 @@ }, "end": { "line": 1, - "column": 28 + "column": 46 } }, "sourceType": "script", @@ -32,7 +32,7 @@ { "type": "ExpressionStatement", "start": 0, - "end": 28, + "end": 46, "loc": { "start": { "line": 1, @@ -40,13 +40,13 @@ }, "end": { "line": 1, - "column": 28 + "column": 46 } }, "expression": { "type": "CallExpression", "start": 0, - "end": 27, + "end": 29, "loc": { "start": { "line": 1, @@ -54,7 +54,7 @@ }, "end": { "line": 1, - "column": 27 + "column": 29 } }, "callee": { @@ -166,9 +166,9 @@ "trailingComments": [ { "type": "CommentBlock", - "value": " comment ", + "value": " comment 1 ", "start": 13, - "end": 26, + "end": 28, "loc": { "start": { "line": 1, @@ -176,12 +176,30 @@ }, "end": { "line": 1, - "column": 26 + "column": 28 } } } ] } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " comment 2 ", + "start": 30, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 45 + } + } + } ] } } @@ -191,9 +209,9 @@ "comments": [ { "type": "CommentBlock", - "value": " comment ", + "value": " comment 1 ", "start": 13, - "end": 26, + "end": 28, "loc": { "start": { "line": 1, @@ -201,7 +219,23 @@ }, "end": { "line": 1, - "column": 26 + "column": 28 + } + } + }, + { + "type": "CommentBlock", + "value": " comment 2 ", + "start": 30, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 30 + }, + "end": { + "line": 1, + "column": 45 } } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/input.js index d8611dab0c54..ebebb26100af 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/input.js +++ b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/input.js @@ -1 +1 @@ -fn(a, b, /* comment */); +fn(a, b, /* comment 1 */) /* comment 2*/; diff --git a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/output.json index 756beb301cf3..6147660b79f9 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/function-trailing-comma/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 24, + "end": 41, "loc": { "start": { "line": 1, @@ -9,13 +9,13 @@ }, "end": { "line": 1, - "column": 24 + "column": 41 } }, "program": { "type": "Program", "start": 0, - "end": 24, + "end": 41, "loc": { "start": { "line": 1, @@ -23,7 +23,7 @@ }, "end": { "line": 1, - "column": 24 + "column": 41 } }, "sourceType": "script", @@ -32,7 +32,7 @@ { "type": "ExpressionStatement", "start": 0, - "end": 24, + "end": 41, "loc": { "start": { "line": 1, @@ -40,13 +40,13 @@ }, "end": { "line": 1, - "column": 24 + "column": 41 } }, "expression": { "type": "CallExpression", "start": 0, - "end": 23, + "end": 25, "loc": { "start": { "line": 1, @@ -54,7 +54,7 @@ }, "end": { "line": 1, - "column": 23 + "column": 25 } }, "callee": { @@ -111,9 +111,9 @@ "trailingComments": [ { "type": "CommentBlock", - "value": " comment ", + "value": " comment 1 ", "start": 9, - "end": 22, + "end": 24, "loc": { "start": { "line": 1, @@ -121,12 +121,30 @@ }, "end": { "line": 1, - "column": 22 + "column": 24 } } } ] } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " comment 2", + "start": 26, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 40 + } + } + } ] } } @@ -136,9 +154,9 @@ "comments": [ { "type": "CommentBlock", - "value": " comment ", + "value": " comment 1 ", "start": 9, - "end": 22, + "end": 24, "loc": { "start": { "line": 1, @@ -146,7 +164,23 @@ }, "end": { "line": 1, - "column": 22 + "column": 24 + } + } + }, + { + "type": "CommentBlock", + "value": " comment 2", + "start": 26, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 40 } } } diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/input.js index acd5921a7c0e..695835903290 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/input.js +++ b/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/input.js @@ -2,4 +2,4 @@ var obj = { a: '1', // comment 1 b: '2', // comment 2 c: '3', // comment 3 -}; +}; // comment 4 diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/output.json index 0004e27a14fd..82f13bbfed5e 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/object-property-trailing-comma/output.json @@ -1,7 +1,7 @@ { "type": "File", "start": 0, - "end": 83, + "end": 96, "loc": { "start": { "line": 1, @@ -9,13 +9,13 @@ }, "end": { "line": 5, - "column": 2 + "column": 15 } }, "program": { "type": "Program", "start": 0, - "end": 83, + "end": 96, "loc": { "start": { "line": 1, @@ -23,7 +23,7 @@ }, "end": { "line": 5, - "column": 2 + "column": 15 } }, "sourceType": "script", @@ -313,7 +313,25 @@ } } ], - "kind": "var" + "kind": "var", + "trailingComments": [ + { + "type": "CommentLine", + "value": " comment 4", + "start": 84, + "end": 96, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 15 + } + } + } + ] } ], "directives": [] @@ -366,6 +384,22 @@ "column": 22 } } + }, + { + "type": "CommentLine", + "value": " comment 4", + "start": 84, + "end": 96, + "loc": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 15 + } + } } ] } \ No newline at end of file