Format at-rule preludes with structured parsing instead of regexes#230
Open
bartveneman wants to merge 1 commit into
Open
Format at-rule preludes with structured parsing instead of regexes#230bartveneman wants to merge 1 commit into
bartveneman wants to merge 1 commit into
Conversation
format_atrule_prelude reformatted @media/@supports/@container/@layer/ @scope/@import preludes with 13 hand-written regexes applied to raw text. This missed real cases the regex whitelist didn't cover - e.g. @container STYLE(--foo: bar) never got lowercased, since "style" wasn't in the hardcoded function-name list - and any new syntax needs a new regex to work correctly. @projectwallace/css-parser (already used for everything else here) ships a real structured parser for this that format-css previously disabled (parse_atrule_preludes: false). Flip it on and add a new internal printer, used only by print_atrule, that dispatches on each prelude child's node type instead of pattern-matching text. format_atrule_prelude itself - the exported partial formatter - is untouched: same signature, same regex implementation, same tests, same README example. No public API changes. It's now also reused internally as the fallback whenever the structured parser can't represent something losslessly (an at-rule it doesn't recognize, a comment inside a prelude, or the standalone `@layer name;` statement form, which has an upstream bug that splits dotted names like `base.normalize` at the dot - see PARSER_ISSUES.md for this and several other upstream issues found and worked around while building this). Verified via differential testing against the previous implementation across ~170 hand-picked and combinatorial cases (plus the existing 259-test suite) to catch regressions the unit tests alone wouldn't.
Contributor
|
| 📦 Package | 📋 Versions |
|---|---|
| @babel/parser | 2 versions
|
| @babel/types | 2 versions
|
| @babel/helper-string-parser | 2 versions
|
| @babel/helper-validator-identifier | 2 versions
|
| @rolldown/binding-wasm32-wasi | 2 versions
|
| picomatch | 2 versions
|
| rolldown | 2 versions
|
| @oxc-project/types | 2 versions
|
| @rolldown/binding-android-arm64 | 2 versions
|
| @rolldown/binding-darwin-arm64 | 2 versions
|
| @rolldown/binding-darwin-x64 | 2 versions
|
| @rolldown/binding-freebsd-x64 | 2 versions
|
| @rolldown/binding-linux-arm-gnueabihf | 2 versions
|
| @rolldown/binding-linux-arm64-gnu | 2 versions
|
| @rolldown/binding-linux-arm64-musl | 2 versions
|
| @rolldown/binding-linux-ppc64-gnu | 2 versions
|
| @rolldown/binding-linux-s390x-gnu | 2 versions
|
| @rolldown/binding-linux-x64-gnu | 2 versions
|
| @rolldown/binding-linux-x64-musl | 2 versions
|
| @rolldown/binding-openharmony-arm64 | 2 versions
|
| @rolldown/binding-win32-arm64-msvc | 2 versions
|
| @rolldown/binding-win32-x64-msvc | 2 versions
|
💡 To find out what depends on a specific package, run: pnpm -r why example-package
⚠️ Package Size Increase
| 📦 Package | 📏 Base Size | 📏 Source Size | 📈 Size Change |
|---|---|---|---|
| @projectwallace/format-css | 8.6 kB | 11.8 kB | +3.2 kB |
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.
Summary
format_atrule_preludereformatted@media/@supports/@container/@layer/@scope/@importpreludes with 13 hand-written regexes applied to raw text. This missed cases the regex whitelist didn't cover — e.g.@container STYLE(--foo: bar)never got lowercased, sincestylewasn't in the hardcoded function-name list — and any new at-rule syntax needs a new regex to work correctly.@projectwallace/css-parser(already used for everything else in this library) ships a real structured parser for this that was previously disabled (parse_atrule_preludes: false). Flipped it on and added a new internal printer, used only byprint_atrule, that dispatches on each prelude child's node type instead of pattern-matching text.format_atrule_prelude— the exported partial formatter documented in the README — is completely untouched: same signature, same regex implementation, same tests. It's now also reused internally as the safety-net fallback whenever the structured parser can't represent something losslessly (an at-rule it doesn't recognize, a comment inside a prelude, or the standalone@layer name;statement form).@layernames getting split at the dot and corrupting cascade semantics if naively reprinted,calc()/env()/other function calls silently dropped from media-feature and@supportscondition values,@supports selector(...)returning nothing at all, and a few smaller ones). Full writeup with repro snippets inPARSER_ISSUES.md, since these are worth fixing upstream.@namespace svg url(http://...)— the old regex incorrectly inserted a space insidehttp://, which the new structured path fixes automatically).Test plan
vitest run— all 269 tests pass (259 existing, unchanged, + 10 new covering the previously-uncovered cases this rework specifically fixes:@container style()lowercasing, function calls surviving in feature/condition values, dotted@layernames, unrecognized at-rules not losing their prelude, nested@supportsboolean groups)tsc --noEmit— no type errorsoxlint/oxfmt --check— cleanGenerated by Claude Code