Skip to content

fix: properly assign trailing comments #12471

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

Merged
merged 3 commits into from
Jul 18, 2024
Merged

fix: properly assign trailing comments #12471

merged 3 commits into from
Jul 18, 2024

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jul 17, 2024

Trailing comments were added even if they started before the node end, which violates the definition of trailing comments. This fixes that: Previously they were stuffed somewhere, but arguably at the wrong positions. For example the last comment in a function body got assigned as a trailing comma for the body, which is wrong, because the body ends after the comma. The special case is comments at the end of a body or an expression tag - they are added even if there are multiple, and they are added regardless of whether they are separated by newlines or not. This ensures the expression tag end is calculated correctly.

Fixes #12466

Alternative to #12469. Comparison (also see #12469 (comment)):

  • this PR has a more strict understanding of trailing comments , with the consequence that some never show up in the AST, which means the AST is a bit lossy, which might be a problem for certain AST->string converters and adds them at the correct positions
  • fix: allow trailing comments on functions #12469 fixes it by adjusting the index more correctly, while keeping all the trailing comments as is. The AST is not lossy, but the trailing commas are at the wrong places sometimes

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Trailing comments were added even if they started before the node end, which violates the definition of trailing comments.
This fixes that, with the consequence that some comments no longer show up in the AST at all. Previously they were stuffed _somewhere_, but arguably at the wrong positions. For example the last comment in a function body got assigned as a trailing comma for the body, which is wrong, because the body ends _after_ the comma.
The special case is comments at the end of an expression tag - they are added even if there are multiple, and they are added regardless of whether they are separated by newlines or not. This ensures the expression tag end is calculated correctly.

Fixes #12466
Copy link

changeset-bot bot commented Jul 17, 2024

🦋 Changeset detected

Latest commit: 1280a60

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@paoloricciuti
Copy link
Member

Ohhh now i get it...it did seem strange to me that the comments at the end of the body were added as the trailing comments of the whole arrow function expression but i naively assumed the parsing was done by acorn and it was just a weird quirk of acorn. Instead the comments are manually added later. My bad.

@dummdidumm
Copy link
Member Author

dummdidumm commented Jul 17, 2024

Updated the PR to handle multiple trailing comments in a body, which resolves the one drawback this PR previously had (swallowing comments)

@paoloricciuti
Copy link
Member

Updated the PR to handle multiple trailing comments in a body, which resolves the one drawback this PR previously had (swallowing comments)

I guess if this doesn't have drawbacks is a no brainer...should I close mine? Sorry for misjudging the issue initially

@Rich-Harris
Copy link
Member

Man, comments are a PITA to deal with. Let's go with this one since it's slightly more expansive

@Rich-Harris Rich-Harris merged commit 5d82573 into main Jul 18, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the comments-fix branch July 18, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants