Skip to content

Commit

Permalink
Refactor trailing comment adjustment
Browse files Browse the repository at this point in the history
Following up from babel#10369

- Unify the logic for adjusting trailing comments into a separate
  function
- Use it for all three cases, which fixes a bug for ObjectExpressions
  and CallExpressions
- Update tests to check for the fixed bug
  • Loading branch information
Shrey Banga committed Aug 31, 2019
1 parent 8a775a3 commit cdf6dc9
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 115 deletions.
129 changes: 50 additions & 79 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}

Expand Down
@@ -1 +1 @@
fn(a, { b }, /* comment */);
fn(a, { b }, /* comment 1 */) /* comment 2 */;
@@ -1,29 +1,29 @@
{
"type": "File",
"start": 0,
"end": 28,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
"column": 46
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
"column": 46
}
},
"sourceType": "script",
Expand All @@ -32,29 +32,29 @@
{
"type": "ExpressionStatement",
"start": 0,
"end": 28,
"end": 46,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
"column": 46
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 27,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
"column": 29
}
},
"callee": {
Expand Down Expand Up @@ -166,22 +166,40 @@
"trailingComments": [
{
"type": "CommentBlock",
"value": " comment ",
"value": " comment 1 ",
"start": 13,
"end": 26,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 13
},
"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
}
}
}
]
}
}
Expand All @@ -191,17 +209,33 @@
"comments": [
{
"type": "CommentBlock",
"value": " comment ",
"value": " comment 1 ",
"start": 13,
"end": 26,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 13
},
"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
}
}
}
Expand Down
@@ -1 +1 @@
fn(a, b, /* comment */);
fn(a, b, /* comment 1 */) /* comment 2*/;

0 comments on commit cdf6dc9

Please sign in to comment.