Skip to content

fix: preserve CSS comments when printing an AST#18475

Open
manuel3108 wants to merge 1 commit into
mainfrom
fix/preserve-css-comments-when-printing-an-ast
Open

fix: preserve CSS comments when printing an AST#18475
manuel3108 wants to merge 1 commit into
mainfrom
fix/preserve-css-comments-when-printing-an-ast

Conversation

@manuel3108

Copy link
Copy Markdown
Member

This was identified by sveltejs/cli#1138.

CSS comments were discarded by the parser, so print(parse(source)) could not reproduce them. This adds CSS comment nodes to the public AST and preserves inline comments through raw values, allowing the printer to retain them.

Although this extends public AST child unions, I don’t think it should be considered breaking. print has existed for roughly seven months without anyone reporting this limitation, and adding a previously missing node type is a corrective AST change rather than removing or changing existing nodes.

The implementation is more intrusive than I would have liked, particularly because CSS analysis must now ignore comment children. I’m open to less invasive alternatives.

Alternatives considered

  • Store comments in container-local comments arrays on stylesheets, blocks, and selector lists, then merge them with structural children by source position while printing. This avoids widening existing child unions, but comments are no longer represented in their natural order.
  • Attach comments through leadingComments and trailingComments properties. I remember this pattern causing attachment and printing issues in esrap, so I wanted to avoid repeating it here.

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.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

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

@changeset-bot

changeset-bot Bot commented Jun 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9d01b8b

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

@svelte-docs-bot

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@18475

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.

1 participant