Skip to content

fix: preserve trailing comments in embedded expressions - #549

Open
00200200 wants to merge 1 commit into
sveltejs:mainfrom
00200200:codex/preserve-expression-trailing-comments
Open

00200200 wants to merge 1 commit into
sveltejs:mainfrom
00200200:codex/preserve-expression-trailing-comments

Conversation

@00200200

Copy link
Copy Markdown

Fixes #536.

Formatting {#if true /* condition */}{/if} currently removes the comment. Svelte attaches it to trailingComments, outside the expression's end position. Include those comments in the text passed to Prettier. Add a line-suffix boundary so a trailing // comment stays inside the Svelte expression instead of becoming literal markup after its closing brace.

The new JS and TS fixtures cover conditionals, each/await/key blocks, attributes, HTML tags, multiple trailing comments, comments inside parentheses, and line comments. The formatting harness also checks a second formatting pass.

Validation on Node 20.19.5, locked Svelte 5.56.0 and Prettier 3.8.3:

  • Both new regression fixtures fail on unchanged base commit 7809486 and pass with the fix.
  • pnpm test: 311 tests passed.
  • pnpm build, pnpm lint, and git diff --check: passed.
  • Built-plugin smoke tests preserve the reported block comment and a trailing line comment; a second pass is unchanged.
  • pnpm check fails with missing estree declarations and three resulting implicit-any errors. The unchanged base has the same four errors with the same installed dependencies.

Scope: this preserves trailing comments attached to embedded nodes by the Svelte compiler. Comments the compiler leaves unattached, such as a comment after an expression's closing parenthesis, are outside this fix. The additional CI dependency versions have not been run locally.

AI disclosure: OpenAI Codex investigated the issue, prepared the code and tests, and wrote this PR description at the account owner's request. No human code review took place during this session.

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.

Formatting deletes JS comments from components in some cases

1 participant