-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor trailing comment adjustment #10380
Changes from 3 commits
cdf6dc9
f67dcbb
d50f9d6
5696c94
10bea52
7819484
816b1e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,48 @@ 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--; | ||
} | ||
} | ||
|
||
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,85 +126,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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I skimmed through https://github.com/babel/babylon/blob/master/ast/spec.md and looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeas please! And also |
||
case "ObjectExpression": | ||
this.adjustCommentsAfterTrailingComma(node, node.properties); | ||
break; | ||
case "CallExpression": | ||
this.adjustCommentsAfterTrailingComma(node, node.arguments); | ||
break; | ||
case "ArrayExpression": | ||
this.adjustCommentsAfterTrailingComma(node, node.elements); | ||
break; | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
fn(a, { b }, /* comment */); | ||
fn(a, { b }, /* comment 1 */) /* comment 2 */; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
fn(a, b, /* comment */); | ||
fn(a, b, /* comment 1 */) /* comment 2*/; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
this.state.commentPreviousNode
will always be non-empty as long aselements
is not empty.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah good point.