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

Conversation

RobinHeidenis
Copy link
Contributor

PR Checklist

Overview

When adding braces to (if) statements that have comments above or below them they would be reprinted, resulting in duplicated comments. This PR aims to fix that by deleting the leadingComments and trailingComments properties from the AST nodes parsed by the plugin.

Removing these can cause extra newlines being printed (in place of the comments), so they are being filtered out by trim()ing the generated code.

I've ran this modified version of the plugin on my company's internal project, and I haven't seen anything weird or wrong being outputted. I've also added two tests to verify the behavior.

I'm not sure if deleting the leadingComments and trailingComments is the best way to do this, but it does seem to work properly. 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.

🚀

@RobinHeidenis RobinHeidenis marked this pull request as ready for review October 9, 2024 16:15
@RobinHeidenis
Copy link
Contributor Author

Small note: I couldn't get ESLint to work locally, as it's installing v9 and the eslintrc is still in the old format. Installing ESLint v8 manually results in a circular dependency in the config with the vitest plugin. Hopefully it works in the pipelines

Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

Looks great to me, nice new test coverage! ✨

Just one question/suggestion on simplifying a bit, then I think we're good to go.

Comment on lines 19 to 24
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

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author Needs an action taken by the original poster label Oct 10, 2024
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.

@JoshuaKGoldberg
Copy link
Owner

If you merge from the latest main then the unrelated Lint and Prettier failures should go away. Sorry for the noise. 🙂

@github-actions github-actions bot removed the status: waiting for author Needs an action taken by the original poster label Oct 11, 2024
Copy link
Owner

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

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

🎇

@JoshuaKGoldberg JoshuaKGoldberg merged commit 30a759c into JoshuaKGoldberg:main Oct 11, 2024
11 checks passed
@JoshuaKGoldberg
Copy link
Owner

@all-contributors please add @RobinHeidenis for code.

🤖 Beep boop! This comment was added automatically by all-contributors-auto-action.
Not all contributions can be detected from Git & GitHub alone. Please comment any missing contribution types this bot missed.
...and of course, thank you for contributing! 💙

Copy link
Contributor

@JoshuaKGoldberg

I've put up a pull request to add @RobinHeidenis! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 Bug: comments are duplicated on prettier run
2 participants