Skip to content

Extract shared condition parsing into parse-condition.ts - #288

Merged
bartveneman merged 3 commits into
mainfrom
claude/extract-condition-parser
Aug 16, 2026
Merged

Extract shared condition parsing into parse-condition.ts#288
bartveneman merged 3 commits into
mainfrom
claude/extract-condition-parser

Conversation

@bartveneman

Copy link
Copy Markdown
Member

Summary

Prep refactor split out of #253, so that PR stays focused on the if()-parsing feature itself.

Pulls media-feature (incl. range syntax), supports-condition (incl. compound and/or/not), and style()/selector()/font-tech() function-condition parsing out of AtRulePreludeParser into a new ConditionParser class (src/parse-condition.ts), so this logic has a single implementation instead of being duplicated by an upcoming change to ValueNodeParser (needed so if()'s media()/supports()/style() condition functions produce real MediaFeature/FeatureRange/SupportsQuery/SupportsDeclaration nodes instead of ad-hoc ones).

Design notes

  • ConditionParser takes an already-constructed ValueNodeParser rather than importing and instantiating its own, since parse-atrule-prelude.ts already imports value-node-parser.ts — a runtime import the other way would be circular. The constructor parameter is typed via import type, which is erased at build time and creates no runtime dependency in either direction.
  • ConditionParser owns its own Lexer, separate from whichever class composes it — matching this codebase's existing sub-parser convention (e.g. how AtRulePreludeParser already composes ValueNodeParser). Where AtRulePreludeParser calls into it mid-scan on its own lexer, it now explicitly reseeks to ConditionParser.end_position afterward, since the delegated work no longer advances AtRulePreludeParser's lexer as a side effect the way an in-class method call would have.

Test plan

  • Pure refactor: no behavior change intended for @media/@supports/@container/@import parsing.
  • Full existing test suite passes unchanged (1391 tests, no assertion changes).
  • tsc --noEmit, oxlint, oxfmt --check all clean.
  • pnpm run build succeeds with no circular-import issues; publint clean.

Generated by Claude Code

Pulls media-feature (incl. range syntax), supports-condition (incl.
compound and/or/not), and style()/selector()/font-tech() function-
condition parsing out of AtRulePreludeParser into a new ConditionParser
class, so this logic has one implementation instead of being
duplicated in a soon-to-follow if()-parsing change to ValueNodeParser.

ConditionParser takes an already-constructed ValueNodeParser rather
than importing and instantiating its own, since parse-atrule-prelude.ts
already imports value-node-parser.ts — a plain runtime import the
other way would be circular. The constructor parameter is typed via
`import type`, which is erased at build time and creates no runtime
dependency in either direction.

ConditionParser owns its own Lexer, separate from whichever class
composes it (matching this codebase's existing sub-parser convention,
e.g. how AtRulePreludeParser already composes ValueNodeParser). Where
AtRulePreludeParser calls into it mid-scan on its own lexer, it now
explicitly reseeks to ConditionParser.end_position afterward, since
the delegated work no longer advances AtRulePreludeParser's lexer as
a side effect the way an in-class method call would have.

Pure refactor: no behavior change for @media/@supports/@container/
@import parsing, verified by the full existing test suite (1391
tests, unchanged assertions) plus a clean build with no circular-
import issues.
@pkg-pr-new

pkg-pr-new Bot commented Aug 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@projectwallace/css-parser@a5968e9

commit: a5968e9

@github-actions

github-actions Bot commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

⚠️ Package Size Increase

📦 Package 📏 Base Size 📏 Source Size 📈 Size Change
@projectwallace/css-parser 41.2 kB 42.1 kB +902 B

Comment thread src/parse-condition.ts Outdated
Comment thread src/parse-condition.ts Outdated
…ment

- Condense the end_position getter's doc comment to match this file's
  usual comment length elsewhere.
- Give the range-operator's second-char check in parse_feature_range an
  explicit block body instead of a single-line if.
@bartveneman
bartveneman merged commit 9f93942 into main Aug 16, 2026
16 checks passed
@bartveneman
bartveneman deleted the claude/extract-condition-parser branch August 16, 2026 10:34
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