Skip to content

Terminate a custom property after a free semicolon before a comment - #2158

Merged
ai merged 2 commits into
postcss:mainfrom
wahidrizka:fix-custom-property-before
Sep 18, 2026
Merged

ai merged 2 commits into
postcss:mainfrom
wahidrizka:fix-custom-property-before

Conversation

@wahidrizka

@wahidrizka wahidrizka commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

#2126 made the stringifier decide whether a declaration is a custom property from raws.before: it counts as one unless before ends with a non-space character, which excludes the */_ hack prefix. But the parser also puts free semicolons into before (Parser#freeSemicolon), so a custom property after a double semicolon is no longer terminated before a following comment, and the comment is lost when the output is parsed again. This worked in 8.5.26:

let root = postcss.parse('a{color:red;;--x:blue}')
root.first.append(new postcss.Comment({ text: 'note' }))
root.toString()
// 8.5.28: 'a{color:red;;--x:blue/* note */}'   re-parses as decl, decl
// 8.5.26: 'a{color:red;;--x:blue;/* note */}'  re-parses as decl, decl, comment

a{color:red;;--x:blue} is valid CSS: CSS Syntax 3 discards a lone ; inside a block (consume a block's contents), and Chrome 152 reads the rule as color: red; --x: blue. So --x is a real custom property here, which is also what the parser decides from its first token.

A -- property now counts as a custom property when before ends with a space or a semicolon. The hack-prefix test from #2126 still passes, and the new test fails without the change. The unit tests (702) and ESLint pass, and line coverage stays at 100%.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of custom properties when preceded by semicolons, whitespace, parser-generated prefixes, or stray tokens.
    • Preserved correct formatting and parsing when comments follow custom property declarations.
  • Tests

    • Added coverage for custom properties following free semicolons and preceding comments.

isCustomProperty() from postcss#2126 counts a declaration as a custom property
unless raws.before ends with a non-space character. The parser also
puts free semicolons and tokens like `:` in front of the name into
before, so the check fails both ways: after `;;` a custom property is
no longer terminated and an appended comment is lost on re-parse
(8.5.26 kept it), and `a{: --x:red/*c*/}` gains a semicolon.
@coderabbitai

coderabbitai Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 29d92afd-1f3a-4208-9dd7-2a472ff3a065

📥 Commits

Reviewing files that changed from the base of the PR and between 5df8356 and f1caa72.

📒 Files selected for processing (2)
  • lib/stringifier.js
  • test/stringifier.test.js
💤 Files with no reviewable changes (1)
  • test/stringifier.test.js

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

The stringifier now recognizes custom properties after whitespace or semicolons. A test verifies serialization and reparsing when a comment follows the property.

Changes

Custom Property Stringification

Layer / File(s) Summary
Custom property prefix handling and validation
lib/stringifier.js, test/stringifier.test.js
isCustomProperty accepts whitespace or semicolon prefixes before -- properties. The test verifies serialized output and reparsed node types.

Priority: ⬇️ Low

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Suggested reviewers: ai

Merge Risk: ⚪ Minimal · up to f1caa

The change addresses the reported custom-property round-trip case without an established remaining production risk.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: terminating a custom property before a following comment when a free semicolon precedes it.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread test/stringifier.test.js Outdated
})

test('keeps property after a stray token before a comment unchanged', () => {
for (let css of ['a{: --x:red/*c*/}', 'a{] --x:red/*c*/}']) {

@ai ai Sep 15, 2026 •

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.

I need some real proofs that in browser or any other good reference it will be red value and not --x:red/*c*/

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You are right to ask. A browser does not read that input as a declaration at all. CSS Syntax 3 only consumes a declaration when it starts with an ident; otherwise it is dropped as a bad declaration. In Chrome 152 (Edge 153 gives the same), the rules .t1{: --x:red/*c*/} and .t2{] --x:red/*c*/} end up with no declarations (cssText is .t1 { }), while .t3{--x:red/*c*/} gives --x: red.

So there is no reference for how that input should print. I removed this test and narrowed the change: only a semicolon at the end of before is now treated like a space, so input like this prints as it does on main.

What is left is the double-semicolon case, which is valid CSS: Chrome reads .t4{color:red;;--x:blue} as color: red; --x: blue, and 8.5.26 kept a comment appended after that --x. I updated the title and description to match.

Browsers drop a declaration that does not start with a name, so there
is no reference for how input like `a{: --x:red/*c*/}` should print.
Leave that as it is on main and keep only the fix for the regression
from postcss#2126: a free semicolon at the end of before no longer stops a
custom property from being terminated before a following comment.
@wahidrizka wahidrizka changed the title Match the parser when deciding if a declaration is a custom property Terminate a custom property after a free semicolon before a comment Sep 18, 2026
@ai
ai merged commit a295dd0 into postcss:main Sep 18, 2026
11 checks passed
@ai

ai commented Sep 18, 2026

Copy link
Copy Markdown
Member

Thanks

metonym added a commit to metonym/caligula that referenced this pull request Oct 11, 2026
…8.5.29 does

PostCSS 8.5.29 (postcss/postcss#2158) counts a stray `;` in a custom
property's leading raws as whitespace, so `.a{;--x:y;/**/}` keeps the
`;` after the custom property once the rule after the comment is
removed. Match it and drop the D_AFTER_SEMI flag that tracked the old
behavior.
metonym added a commit to metonym/caligula that referenced this pull request Oct 12, 2026
…8.5.29 does

PostCSS 8.5.29 (postcss/postcss#2158) counts a stray `;` in a custom
property's leading raws as whitespace, so `.a{;--x:y;/**/}` keeps the
`;` after the custom property once the rule after the comment is
removed. Match it and drop the D_AFTER_SEMI flag that tracked the old
behavior.
metonym added a commit to metonym/caligula that referenced this pull request Oct 12, 2026
…8.5.29 does

PostCSS 8.5.29 (postcss/postcss#2158) counts a stray `;` in a custom
property's leading raws as whitespace, so `.a{;--x:y;/**/}` keeps the
`;` after the custom property once the rule after the comment is
removed. Match it and drop the D_AFTER_SEMI flag that tracked the old
behavior.
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