Support compound if() conditions with not/and/or operators - #289
Merged
Conversation
…binator if()'s branch-condition parsing only ever looked at a single token: a TOKEN_FUNCTION (style()/media()/supports()) or a bare identifier. For `not style(--scheme: light)`, the leading "not" identifier was taken as the entire condition, after which the colon-finder blindly skipped tokens (including style()'s own nested ':') until it hit the first stray ')' — silently losing style() and corrupting everything after it. Condition parsing now consumes the full <boolean-expr[ <if-test> ]> grammar (not/and/or chains of style()/media()/supports() calls), same as the compound grammar already supported inside supports(). A compound condition is exposed as a new IfCondition node (IfBranch.condition), while a single test or "else" stays unwrapped as before. Also fixes style()'s own query to accept the compound form (e.g. `style((--scheme: dark) or (--scheme: very-dark))`), mirroring the fallback supports() already had. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013PZZLGd2cEKsg1tDbRiy5D
Contributor
|
| 📦 Package | 📏 Base Size | 📏 Source Size | 📈 Size Change |
|---|---|---|---|
| @projectwallace/css-parser | 44.5 kB | 45.5 kB | +1 kB |
commit: |
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013PZZLGd2cEKsg1tDbRiy5D
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
This PR adds support for parsing compound
if()conditions that combine multiple test functions using logical operators (not,and,or). Previously, only simple single-function conditions likeif(style(--x: 1): ...)were supported. Now conditions likeif(not style(--scheme: light): ...)andif(style(--a: 1) and media(width > 600px): ...)are properly parsed.Key Changes
New
IF_CONDITIONnode type: Introduced a wrapper node type to represent compound conditions combining multiple test functions with logical operators. Simple single-function conditions remain unwrapped for backward compatibility.New
PRELUDE_OPERATORnode type: Added to represent thenot,and, andoroperators within conditions, matching the structure used in@supportscompound conditions.Enhanced
parse_if_condition()method: Replaced the simple conditional logic with a comprehensive parser that:not,and,or) asPRELUDE_OPERATORnodesIF_CONDITIONnode while keeping simple conditions unwrappedExtended
style()function parsing: Updatedparse_if_condition_function()to support the full compoundand/or/notgrammar withinstyle()conditions (e.g.,style((--scheme: dark) or (--scheme: very-dark))), delegating to the existingConditionParserfor complex expressions.Helper methods: Added
is_and_or_not()andnext_significant_token()to support lookahead parsing and operator detection.Type system updates: Updated
IfBranch.conditiontype to includeIfCondition, and added corresponding type guards and exports.Notable Implementation Details
IF_CONDITIONonly when necessary (multiple components or operators present), preserving the simple unwrapped structure for the common single-function case@supportscondition parsing, allowing consistent traversal of operator/function sequenceshttps://claude.ai/code/session_013PZZLGd2cEKsg1tDbRiy5D