Skip to content
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

fix: duplicated comments #588

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/preprocess.test.ts
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps someone else could verify these changes, as my company's codebase has mostly if statements that are being rewritten by this plugin, and not any of the other examples in the tests like while and for statements.

👍 yeah agreement from me, I can't find any issues. If anything pops up in the future then great, it should be represented here as a test case.

Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,46 @@ _ = {
],
c: "d"
};
`,
],
[
`
const p = 'test';


//pre comment
//this is another pre comment
if (a) b;
//post comment

someOtherCode();
`,
`
const p = 'test';


//pre comment
//this is another pre comment
if(a){b}
//post comment

someOtherCode();
`,
],
[
`
// pre comment
if (a)
// inner comment
b;
// post comment
`,
`
// pre comment
if(a){
// inner comment
b}
// post comment
`,
],
])("%s becomes %s", (input, expected, filepath = "test.ts") => {
Expand Down
5 changes: 4 additions & 1 deletion src/reprintAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export function reprintAst(code: string, collectedNodes: CollectibleNode[]) {
// See https://github.com/prettier/prettier/issues/9114 for a Prettier AST format API.

// @ts-ignore
output += (generate.default || generate)(collectedNode, printOptions).code;
output += (generate.default || generate)(
collectedNode,
printOptions,
).code.trim();

lastEnd = collectedNode.end!;
}
Expand Down
8 changes: 8 additions & 0 deletions src/traverseAndModifyAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export function traverseAndModifyAst(ast: Node) {
modifyNodeIfMissingBrackets(node) &&
nodeNotAlreadySeen(node, seenNodes)
) {
// Remove comments from the original node to avoid duplication
if (node.leadingComments) {
delete node.leadingComments;
}
if (node.trailingComments) {
delete node.trailingComments;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question] Any reason for the ifs? No tests fail if I simplify them out:

Suggested change
if (node.leadingComments) {
delete node.leadingComments;
}
if (node.trailingComments) {
delete node.trailingComments;
}
delete node.leadingComments;
delete node.trailingComments;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch :D I forgot delete doesn't throw if the property doesn't exist on the object, so the if statements are unnecessary. I've removed them


modifiedNodes.add(node);
}
}
Expand Down
Loading