Skip to content

fix: don't delete the last node when adjacent comments precede <script> - #560

Merged
dummdidumm merged 1 commit into
sveltejs:mainfrom
eknathdj:fix/adjacent-comments-delete-last-node
Nov 17, 2026
Merged

dummdidumm merged 1 commit into
sveltejs:mainfrom
eknathdj:fix/adjacent-comments-delete-last-node

Conversation

@eknathdj

Copy link
Copy Markdown
Contributor

Fixes #555

When two comments with no whitespace between them sit right above <script> or <style>, removeAndGetLeadingComments adds a placeholder text node (start: -1) for the missing newline. That placeholder is not in ast.fragment.nodes, so siblings.splice(siblings.indexOf(text), 1) becomes splice(-1, 1) and removes the last node of the markup.

This skips the splice when the node is not found, as suggested in the issue.

Added test/formatting/samples/adjacent-comments-above-script using the multi-node repro from the issue. On main it fails with <p>z</p> missing from the output; with the fix the full suite passes (310 tests).

removeAndGetLeadingComments pushes a placeholder text node for adjacent comments. The placeholder is not in the fragment, so splice(indexOf(...), 1) became splice(-1, 1) and removed the last node of the markup. Skip the splice when the node is not found.

Fixes sveltejs#555

Assisted-by: Kiro (Claude)

@dummdidumm dummdidumm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

@dummdidumm
dummdidumm merged commit f3cd71d into sveltejs:main Nov 17, 2026
5 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 17, 2026
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.

Two adjacent HTML comments above <script> delete the last node of the markup

2 participants