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 comments print in IfStatement #15076

Merged
merged 5 commits into from Jul 29, 2023
Merged

Conversation

fisker
Copy link
Member

@fisker fisker commented Jul 11, 2023

Description

Fixes #15007

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@fisker fisker marked this pull request as ready for review July 11, 2023 06:18

// TODO[@fisker]: fix this
// if (a) var a = 1; /* comment 6e */
// else return 2;
Copy link
Member Author

Choose a reason for hiding this comment

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

@sosukesuzuki

markerForIfWithoutBlockAndSameLineComment was introduced in #12017, do you remember why we attach the comment as dangling instead of trailing comment?

Many nodes can be a child of IfStatement, though this code is not reasonable, but still valid, the comment is not printed.

Copy link
Member

Choose a reason for hiding this comment

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

I forget... but I tried to run tests in local with:

-        addDanglingComment(
-          precedingNode,
-          comment,
-          markerForIfWithoutBlockAndSameLineComment,
-        );
+        addTrailingComment(precedingNode, comment);

then I got the spanpshot diffs like this:

- if (a === 0) expr; // comment A3
- else if (a === 1) expr; // comment B3
+ if (a === 0)
+   expr; // comment A3
+ else if (a === 1)
+   expr; // comment B3

At the time, I may have wanted to avoid this line break.

@@ -93,26 +93,44 @@ parsers: ["babel", "flow", "typescript"]
printWidth: 80
| printWidth
=====================================input======================================
if (a === 0) doSomething(); // comment A1
if (a === 0) doSomething(); // comment A1
Copy link
Member

Choose a reason for hiding this comment

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

Where does this extra spacing come from? Not sure if i is expected in output, but maybe I am missing something.

Copy link
Member Author

@fisker fisker Jul 11, 2023

Choose a reason for hiding this comment

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

@fisker fisker changed the title Fix dangling comment print Fix comments print in IfStatement Jul 29, 2023
@fisker fisker merged commit 29854ca into prettier:main Jul 29, 2023
27 checks passed
@fisker fisker deleted the dangling-comments branch July 29, 2023 02:01
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Feb 15, 2024
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.

Skip single characters was not printed. Please report this error!
3 participants