Skip to content

Fix missing space when AtRule#params is set after parsing - #2113

Merged
ai merged 1 commit into
postcss:mainfrom
sarathfrancis90:fix-atrule-params-space
Jul 19, 2026
Merged

ai merged 1 commit into
postcss:mainfrom
sarathfrancis90:fix-atrule-params-space

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

Setting AtRule#params on an at-rule that was parsed without params glues the params onto the name:

let root = postcss.parse('@layer{a{color:black}}')
root.first.params = 'utilities'
root.toString() //=> '@layerutilities{a{color:black}}'

@layerutilities is a single at-word, so the output no longer parses back to the at-rule that produced it. Same for @media{}, @scope{}, @font-face{} — anything written without params.

atruleStart() already has a fallback that inserts the space when raws.afterName is missing, but the parser sets raws.afterName = '' explicitly for a params-less at-rule, so the fallback never gets a chance to run.

I still prefer the raw when it's there, and only fall back to a space when the params would otherwise merge into the at-rule name token. @media(min-width:0) and @import"a.css" stay byte-identical: ( and " end the at-word on their own, and those delimiters are the only way the parser can produce an empty afterName together with non-empty params.

Found this fuzzing mutation round-trips over the postcss-parser-tests fixtures — across ~10k parse → set params → re-parse cases this was the only invariant that broke (100 distinct failures before, 0 after). Added tests for both directions; pnpm test is green.

@ai
ai merged commit c30586d into postcss:main Jul 19, 2026
10 checks passed
@ai

ai commented Jul 19, 2026

Copy link
Copy Markdown
Member

Thanks. Released in 8.5.20.

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