diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 60f3c1a23f9b..bdeac9c140cb 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -38,6 +38,45 @@ 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; + } + + 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--; + } + } + + const newTrailingComments = []; + while (this.state.leadingComments.length) { + const leadingComment = this.state.leadingComments.shift(); + if (leadingComment.end < node.end) { + newTrailingComments.push(leadingComment); + } else { + if (node.trailingComments === undefined) { + node.trailingComments = []; + } + node.trailingComments.push(leadingComment); + } + } + + if (newTrailingComments.length > 0) { + lastElement.trailingComments = newTrailingComments; + } else if (lastElement.trailingComments !== undefined) { + lastElement.trailingComments = []; + } + } + processComment(node: Node): void { if (node.type === "Program" && node.body.length > 0) return; @@ -84,86 +123,33 @@ 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": + case "ObjectPattern": + this.adjustCommentsAfterTrailingComma(node, node.properties); + break; + case "CallExpression": + this.adjustCommentsAfterTrailingComma(node, node.arguments); + break; + case "ArrayExpression": + case "ArrayPattern": + this.adjustCommentsAfterTrailingComma(node, node.elements); + break; } + } else if ( + this.state.commentPreviousNode && + ((this.state.commentPreviousNode.type === "ImportSpecifier" && + node.type !== "ImportSpecifier") || + (this.state.commentPreviousNode.type === "ExportSpecifier" && + node.type !== "ExportSpecifier")) + ) { + this.adjustCommentsAfterTrailingComma(node, [ + this.state.commentPreviousNode, + ]); } if (lastChild) { diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/input.js new file mode 100644 index 000000000000..3d234bad027c --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/input.js @@ -0,0 +1,6 @@ +const [ + /* One */ + x + /* Two */, + /* Three */ +] /* Four */ = []; diff --git a/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/output.json new file mode 100644 index 000000000000..124041c4bb99 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/array-pattern-trailing-comma/output.json @@ -0,0 +1,254 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 6, + "column": 17 + } + }, + "id": { + "type": "ArrayPattern", + "start": 6, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "elements": [ + { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x", + "leadingComments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 10, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Two ", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 39, + "end": 50, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + ] + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Four ", + "start": 53, + "end": 63, + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 12 + } + } + } + ] + }, + "init": { + "type": "ArrayExpression", + "start": 66, + "end": 68, + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 17 + } + }, + "elements": [] + } + } + ], + "kind": "const" + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 10, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Two ", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 39, + "end": 50, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 53, + "end": 63, + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 12 + } + } + } + ] +} \ No newline at end of file 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-expression-trailing-comma/input.js b/packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/input.js new file mode 100644 index 000000000000..78f80a9968e5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/input.js @@ -0,0 +1,6 @@ +const { + /* One */ + x + /* Two */, + /* Three */ +} /* Four */ = {}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/output.json b/packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/output.json new file mode 100644 index 000000000000..5f2a5b29b779 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-expression-trailing-comma/output.json @@ -0,0 +1,292 @@ +{ + "type": "File", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 69, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 18 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 68, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 6, + "column": 17 + } + }, + "id": { + "type": "ObjectPattern", + "start": 6, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectProperty", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x" + }, + "computed": false, + "shorthand": true, + "value": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 3 + }, + "identifierName": "x" + }, + "name": "x" + }, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 10, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Two ", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 39, + "end": 50, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + } + } + ], + "extra": { + "shorthand": true + } + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Four ", + "start": 53, + "end": 63, + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 12 + } + } + } + ] + }, + "init": { + "type": "ObjectExpression", + "start": 66, + "end": 68, + "loc": { + "start": { + "line": 6, + "column": 15 + }, + "end": { + "line": 6, + "column": 17 + } + }, + "properties": [] + } + } + ], + "kind": "const" + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 10, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Two ", + "start": 26, + "end": 35, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 39, + "end": 50, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 53, + "end": 63, + "loc": { + "start": { + "line": 6, + "column": 2 + }, + "end": { + "line": 6, + "column": 12 + } + } + } + ] +} \ No newline at end of file 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 diff --git a/packages/babel-parser/test/fixtures/comments/basic/switch-function-call-no-semicolon/output.json b/packages/babel-parser/test/fixtures/comments/basic/switch-function-call-no-semicolon/output.json index f732250d6daa..4452426a990f 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/switch-function-call-no-semicolon/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/switch-function-call-no-semicolon/output.json @@ -250,25 +250,7 @@ "label": null } ], - "test": null, - "leadingComments": [ - { - "type": "CommentLine", - "value": " comment", - "start": 47, - "end": 57, - "loc": { - "start": { - "line": 4, - "column": 4 - }, - "end": { - "line": 4, - "column": 14 - } - } - } - ] + "test": null } ] } diff --git a/packages/babel-parser/test/fixtures/comments/regression/10230/output.json b/packages/babel-parser/test/fixtures/comments/regression/10230/output.json index 0c3dcfdf9744..9e185da12f9c 100644 --- a/packages/babel-parser/test/fixtures/comments/regression/10230/output.json +++ b/packages/babel-parser/test/fixtures/comments/regression/10230/output.json @@ -143,11 +143,47 @@ "raw": "42" }, "value": 42 - } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " One", + "start": 17, + "end": 23, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + } + } + ] } ] } - } + }, + "trailingComments": [ + { + "type": "CommentLine", + "value": " Two", + "start": 27, + "end": 33, + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 6 + } + } + } + ] }, { "type": "ExpressionStatement", @@ -179,25 +215,7 @@ "identifierName": "B" }, "name": "B" - }, - "leadingComments": [ - { - "type": "CommentLine", - "value": " Two", - "start": 27, - "end": 33, - "loc": { - "start": { - "line": 6, - "column": 0 - }, - "end": { - "line": 6, - "column": 6 - } - } - } - ] + } } ], "directives": [] diff --git a/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/input.js b/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/input.js new file mode 100644 index 000000000000..2524b3f08c75 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/input.js @@ -0,0 +1,5 @@ +export { + /* One */ Foo + /* Two */, + /* Three */ +} /* Four */ from "foo"; diff --git a/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/output.json new file mode 100644 index 000000000000..a663fd513d11 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/modules/export-declaration-trailing-comma/output.json @@ -0,0 +1,256 @@ +{ + "type": "File", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "specifiers": [ + { + "type": "ExportSpecifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "local": { + "type": "Identifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "exported": { + "type": "Identifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Two ", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 54, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 12 + } + } + } + ] + } + ], + "source": { + "type": "StringLiteral", + "start": 70, + "end": 75, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 23 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + }, + "declaration": null + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Two ", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 54, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 12 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/input.js b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/input.js new file mode 100644 index 000000000000..1b933ff9af5c --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/input.js @@ -0,0 +1,5 @@ +import { + /* One */ Foo + /* Two */, + /* Three */ +} /* Four */ from "foo"; diff --git a/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json new file mode 100644 index 000000000000..a5ebc224c99b --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2015/modules/import-declaration-trailing-comma/output.json @@ -0,0 +1,255 @@ +{ + "type": "File", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 24 + } + }, + "specifiers": [ + { + "type": "ImportSpecifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + } + }, + "imported": { + "type": "Identifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "local": { + "type": "Identifier", + "start": 21, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 15 + }, + "identifierName": "Foo" + }, + "name": "Foo" + }, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " Two ", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 54, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 12 + } + } + } + ] + } + ], + "source": { + "type": "StringLiteral", + "start": 70, + "end": 75, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 23 + } + }, + "extra": { + "rawValue": "foo", + "raw": "\"foo\"" + }, + "value": "foo" + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " One ", + "start": 11, + "end": 20, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Two ", + "start": 27, + "end": 36, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 11 + } + } + }, + { + "type": "CommentBlock", + "value": " Three ", + "start": 40, + "end": 51, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 13 + } + } + }, + { + "type": "CommentBlock", + "value": " Four ", + "start": 54, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 12 + } + } + } + ] +} \ No newline at end of file