Skip to content

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
@changeset-bot
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

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.

svelte 5 : Attribute values containing {...} must be enclosed in quote marks, unless the value only contains the expression

4 participants