Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix custom property losing its semicolon before a comment#2117

Merged
ai merged 1 commit into
postcss:mainpostcss/postcss:mainfrom
sarathfrancis90:fix/custom-property-semicolon-before-commentsarathfrancis90/postcss:fix/custom-property-semicolon-before-commentCopy head branch name to clipboard
Jul 22, 2026
Merged

Fix custom property losing its semicolon before a comment#2117
ai merged 1 commit into
postcss:mainpostcss/postcss:mainfrom
sarathfrancis90:fix/custom-property-semicolon-before-commentsarathfrancis90/postcss:fix/custom-property-semicolon-before-commentCopy head branch name to clipboard

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

Mutating a rule so that a custom property declaration is immediately followed by a comment node, then stringifying, produces CSS that loses the comment on re-parse:

let css = postcss.parse("a{--x:red}")
css.first.append(postcss.comment({ text: "note" }))

css.toString()          // => "a{--x:red/* note */}"
postcss.parse(css.toString()).first.nodes.length // => 1, the comment is gone

The stringifier skips trailing comment siblings when deciding whether the last real child needs a terminating ;. That is fine for normal declarations, because the parser tokenizes a following /* as a separate comment. But a custom property keeps everything up to the next ; or } as its value, so without the semicolon the comment is folded into the value and the comment node disappears. The same class of bug was fixed for childless at-rules in #2115; this is the declaration counterpart.

The fix emits the semicolon when a custom property still has following siblings, right next to the existing at-rule check in pushBody. Naturally parsed CSS is unaffected: a custom property can only be followed by a separate comment sibling if the source already had a ; there (otherwise the comment is part of the value), and that case already sets raws.semicolon.

Tested with two new cases in test/stringifier.test.js covering append() and after(), both asserting the output and that it re-parses back to decl,comment. Full pnpm test (unit, lint, types, integration, size) is green.

A custom property declaration that is the last non-comment child but is
followed by comment siblings was stringified without its terminating
semicolon. Unlike a normal declaration, a custom property keeps everything
up to the next `;` or `}` as its value, so on re-parse the trailing
comments were folded into the value and the comment nodes disappeared.

Building such a tree with append()/insertAfter()/after() therefore
silently dropped the comments. Emit the semicolon when a custom property
still has following siblings so the output round-trips, mirroring the
existing handling for childless at-rules.
@ai
ai merged commit f49d691 into postcss:main Jul 22, 2026
10 checks passed
@ai

ai commented Jul 22, 2026

Copy link
Copy Markdown
Member

Released in 8.5.22.

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.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.