fix: handle @scope at-rule edge cases#91
Open
merklebonsai wants to merge 2 commits intocss-modules:masterfrom
Open
fix: handle @scope at-rule edge cases#91merklebonsai wants to merge 2 commits intocss-modules:masterfrom
merklebonsai wants to merge 2 commits intocss-modules:masterfrom
Conversation
Author
|
Under honest disclosure - this PR is produced together with Claude. Yet, this is not a slop; more of an opposite actually. I've specifically ran Claude to identify edge cases I'm missing (e.g. comments that include |
| } | ||
|
|
||
| return { start, end }; | ||
| } |
Member
There was a problem hiding this comment.
Is it AI generated code? because I see a lot of rooms to improve
Member
There was a problem hiding this comment.
Ask Claude to simplify it and make faster, he knows how to do it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two related fixes for the
@scopeat-rule handler. Two atomic commits.Commit 1 —
fix: parse @scope params with paren-aware tokenizerFixes #90.
The current
@scopehandler splitsatRule.paramson the substring"to":This naively matches
toanywhere in the params, including inside class names (.buttoncontainsbu+to+n; same goes for.tooltip,.toolbar,.photo,.story,.into-view, etc.) and inside attribute selector values ([data-section="footer"],[role="button"]). The result is malformed CSS with extrato ()clauses and class names truncated mid-token, that never match the DOM.The fix replaces
.split("to")withparseScopeParams— a small character-by-character tokenizer that walks the params with paren-depth and string-literal tracking. Thetokeyword is detected only at depth 0, between two parenthesized groups, with a word boundary check so identifiers starting withto(liketools,topology) don't match.The same tokenizer also handles two adjacent legal-CSS edge cases that the old
split("to")would have mishandled regardless of the substring issue:/* something with parens */)\(,\)) — legal per the CSS spec, occasionally emitted by CSS-in-JS pipelinesWhen params can't be parsed, the plugin now emits
result.warn(...)via the postcss API so module-name leakage is diagnosable rather than silent.Commit 2 —
fix: prevent crash on body-less @scope at-ruleThe
@scopebranch in the at-rule walker callsatRule.nodes.forEach(...)unconditionally. When postcss parses an@scopeat-rule with no body (e.g.@scope (.foo);),atRule.nodesisundefinedand.forEachthrowsCannot read properties of undefined (reading 'forEach').The non-scope at-rule branch already handles this with
else if (atRule.nodes). This commit adds the same guard to the@scopebranch.Why we hit this
We're shipping a visual app builder that emits CSS with
@scoperules where component-boundary markers can land in any of the params positions, and component names are user-supplied. Most names hit at least one of the failure modes (any class containing the substringto). Without these fixes,next build— which uses postcss-modules through webpack by default — silently produces broken styles in our generated output. Our codegen needs arbitrary user input to round-trip correctly through every CSS Modules mangler in the ecosystem.Test plan
tosubstring (canonical Bug in handling @scope rule - "to" as a part of a selector #90 case)toin scope-endtotoinside but no scope-end clauseTOkeyword (CSS keywords are case-insensitive):is()/:not()selectors (parser depth correctness)toand unbalanced parens\(in identifier@scopeno longer crashesyarn lint(eslint + prettier) clean.