From d1d705dfd3351f1a388710d7b5a6cd8ffb88c161 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 08:07:21 +0000 Subject: [PATCH] feat!: always add media range feature name as child Co-Authored-By: Claude Sonnet 5.5 --- README.md | 1 - src/node-types.test.ts | 3 +- src/parse-atrule-prelude.test.ts | 52 ++++++++++++++------------------ src/parse-atrule-prelude.ts | 38 +++-------------------- src/parse-condition.ts | 27 +++++------------ src/parse-declaration.ts | 11 ++----- src/parse-value.test.ts | 17 ++++------- src/parse-value.ts | 8 ++--- src/parse.ts | 16 ++-------- src/value-node-parser.ts | 16 ++-------- 10 files changed, 52 insertions(+), 137 deletions(-) diff --git a/README.md b/README.md index ad69694..fcb33f3 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,6 @@ Options: - `parse_values` (default `true`) — parse declaration values into nodes - `parse_selectors` (default `true`) — parse selectors into nodes - `parse_atrule_preludes` (default `true`) — parse at-rule preludes into nodes -- `media_feature_range_name_as_child` (default `false`) — also add the feature name of a media range (`width` in `(400px < width < 1000px)`) as an `Identifier` child, so the children read in source order. `name` is set either way. **Deprecated:** this will become the default in 0.20.0 and the option will be removed, so opt in now to prepare - `on_comment` — called for every comment (`{ start, end, length, line, column }`); comments are never part of the tree ### Sub-parsers diff --git a/src/node-types.test.ts b/src/node-types.test.ts index 20d6840..edf5e4f 100644 --- a/src/node-types.test.ts +++ b/src/node-types.test.ts @@ -339,7 +339,8 @@ describe('type narrowing — compile-time', () => { (Dimension | Function | Identifier | PreludeOperator)[] >() - const [op, dim] = range.children + const [name, op, dim] = range.children + expect(name.type_name).toBe('Identifier') expect(op.type_name).toBe('Operator') expect(dim.type_name).toBe('Dimension') }) diff --git a/src/parse-atrule-prelude.test.ts b/src/parse-atrule-prelude.test.ts index 9273bcf..8fe93f5 100644 --- a/src/parse-atrule-prelude.test.ts +++ b/src/parse-atrule-prelude.test.ts @@ -765,11 +765,12 @@ describe('At-Rule Prelude Nodes', () => { expect(range?.type).toBe(FEATURE_RANGE) expect(range?.name).toBe('width') - expect(range?.children.length).toBe(2) // dimension + operator + expect(range?.children.length).toBe(3) // name + operator + dimension // Verify child types - expect(range?.children[0].type).toBe(PRELUDE_OPERATOR) // >= - expect(range?.children[1].type).toBe(DIMENSION) // 400px + expect(range?.children[0].type).toBe(IDENTIFIER) // width + expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // >= + expect(range?.children[2].type).toBe(DIMENSION) // 400px }) test('should keep a function value whole in range syntax', () => { @@ -777,8 +778,8 @@ describe('At-Rule Prelude Nodes', () => { const range = (query as MediaQuery).children[0] as FeatureRange expect(range.text).toBe('(width >= calc(1px + 2px))') - expect(range.children.map((c) => c.text)).toEqual(['>=', 'calc(1px + 2px)']) - const fn = range.children[1] as Function + expect(range.children.map((c) => c.text)).toEqual(['width', '>=', 'calc(1px + 2px)']) + const fn = range.children[2] as Function expect(fn.name).toBe('calc') expect(fn.value).toBe('1px + 2px') }) @@ -787,44 +788,34 @@ describe('At-Rule Prelude Nodes', () => { const [query] = parse_atrule_prelude('media', '(calc(1px) < width < 5px)') const range = (query as MediaQuery).children[0] as FeatureRange - expect(range.children.map((c) => c.text)).toEqual(['calc(1px)', '<', '<', '5px']) + expect(range.children.map((c) => c.text)).toEqual(['calc(1px)', '<', 'width', '<', '5px']) }) test('should tokenize a bare = as its own comparator', () => { const [query] = parse_atrule_prelude('media', '(width=>1000px)') const range = (query as MediaQuery).children[0] as FeatureRange - expect(range.children.map((c) => c.text)).toEqual(['=', '>', '1000px']) + expect(range.children.map((c) => c.text)).toEqual(['width', '=', '>', '1000px']) }) test('should tokenize >= and <= as a single comparator', () => { const [query] = parse_atrule_prelude('media', '(100px<=width>=1000px)') const range = (query as MediaQuery).children[0] as FeatureRange - expect(range.children.map((c) => c.text)).toEqual(['100px', '<=', '>=', '1000px']) + expect(range.children.map((c) => c.text)).toEqual(['100px', '<=', 'width', '>=', '1000px']) }) - test('should not add the feature name as a child by default', () => { + test('should add the feature name as a child', () => { const [query] = parse_atrule_prelude('media', '(500px > width > 1000px)') const range = (query as MediaQuery).children[0] as FeatureRange - expect(range.name).toBe('width') - expect(range.children.map((c) => c.text)).toEqual(['500px', '>', '>', '1000px']) - }) - - test('should add the feature name as a child with media_feature_range_name_as_child', () => { - const [query] = parse_atrule_prelude('media', '(500px > width > 1000px)', { - media_feature_range_name_as_child: true, - }) - const range = (query as MediaQuery).children[0] as FeatureRange - expect(range.name).toBe('width') expect(range.children.map((c) => c.text)).toEqual(['500px', '>', 'width', '>', '1000px']) expect(range.children[2].type).toBe(IDENTIFIER) }) - test('should add the feature name as a child via parse() with media_feature_range_name_as_child', () => { - const ast = parse('@media (width >= 400px) {}', { media_feature_range_name_as_child: true }) + test('should add the feature name as a child via parse()', () => { + const ast = parse('@media (width >= 400px) {}') const range = (ast.first_child as Atrule).prelude!.first_child!.first_child as FeatureRange expect(range.children.map((c) => c.type_name)).toEqual([ @@ -847,15 +838,16 @@ describe('At-Rule Prelude Nodes', () => { expect(range?.type).toBe(FEATURE_RANGE) expect(range?.name).toBe('width') - expect(range?.children.length).toBe(4) // dim, op, op, dim + expect(range?.children.length).toBe(5) // dim, op, name, op, dim // Verify child types expect(range?.children[0].type).toBe(DIMENSION) // 50px expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // <= - expect(range?.children[2].type).toBe(PRELUDE_OPERATOR) // <= + expect(range?.children[2].type).toBe(IDENTIFIER) // width + expect(range?.children[3].type).toBe(PRELUDE_OPERATOR) // <= expect((range!.children[1] as PreludeOperator).value).toBe('<=') - expect((range!.children[2] as PreludeOperator).value).toBe('<=') - expect(range?.children[3].type).toBe(DIMENSION) // 100px + expect((range!.children[3] as PreludeOperator).value).toBe('<=') + expect(range?.children[4].type).toBe(DIMENSION) // 100px }) test('should parse range syntax with less-than', () => { @@ -871,11 +863,12 @@ describe('At-Rule Prelude Nodes', () => { expect(range?.type).toBe(FEATURE_RANGE) expect(range?.name).toBe('width') - expect(range?.children.length).toBe(2) + expect(range?.children.length).toBe(3) // Verify child types expect(range?.children[0].type).toBe(DIMENSION) // 400px expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // < + expect(range?.children[2].type).toBe(IDENTIFIER) // width expect((range!.children[1] as PreludeOperator).value).toBe('<') }) @@ -892,11 +885,12 @@ describe('At-Rule Prelude Nodes', () => { expect(range?.type).toBe(FEATURE_RANGE) expect(range?.name).toBe('width') - expect(range?.children.length).toBe(2) + expect(range?.children.length).toBe(3) // Verify child types - expect(range?.children[0].type).toBe(PRELUDE_OPERATOR) // = - expect(range?.children[1].type).toBe(DIMENSION) // 500px + expect(range?.children[0].type).toBe(IDENTIFIER) // width + expect(range?.children[1].type).toBe(PRELUDE_OPERATOR) // = + expect(range?.children[2].type).toBe(DIMENSION) // 500px }) test('should parse comma-separated media queries', () => { diff --git a/src/parse-atrule-prelude.ts b/src/parse-atrule-prelude.ts index 8c58864..6d3a2a8 100644 --- a/src/parse-atrule-prelude.ts +++ b/src/parse-atrule-prelude.ts @@ -40,16 +40,6 @@ import { ValueNodeParser } from './value-node-parser' import { ConditionParser } from './parse-condition' import { SelectorParser } from './parse-selector' -/** @internal */ -export interface PreludeParserOptions { - /** - * Defaults to `false`, but this will become the default behavior in 0.20.0. - * - * @deprecated Opt-in only as a migration aid: in 0.20.0 this behavior is always on and the option will be removed. - */ - media_feature_range_name_as_child?: boolean -} - export class AtRulePreludeParser { private lexer: Lexer private arena: CSSDataArena @@ -63,23 +53,14 @@ export class AtRulePreludeParser { // Deep-parses the parenthesised arguments of @scope into real SelectorLists private selector_parser: SelectorParser - constructor( - arena: CSSDataArena, - source: string, - media_feature_range_name_as_child: boolean = false, - ) { + constructor(arena: CSSDataArena, source: string) { this.arena = arena this.source = source // Create a lexer instance for prelude parsing this.lexer = new Lexer(source) this.prelude_end = 0 - this.value_node_parser = new ValueNodeParser(arena, source, media_feature_range_name_as_child) - this.condition_parser = new ConditionParser( - arena, - source, - this.value_node_parser, - media_feature_range_name_as_child, - ) + this.value_node_parser = new ValueNodeParser(arena, source) + this.condition_parser = new ConditionParser(arena, source, this.value_node_parser) this.selector_parser = new SelectorParser(arena, source) } @@ -853,23 +834,14 @@ export class AtRulePreludeParser { * Parse an at-rule prelude string and return an array of AST nodes * @param at_rule_name - The name of the at-rule (e.g., "media", "supports", "layer") * @param prelude - The at-rule prelude to parse (e.g., "(min-width: 768px)", "utilities") - * @param options - Parser options (`media_feature_range_name_as_child`: also add the feature name of a media range as an Identifier child. This will become the default behavior in 0.20.0.) * @returns An array of CSSNode objects representing the parsed prelude */ -export function parse_atrule_prelude( - at_rule_name: string, - prelude: string, - options?: PreludeParserOptions, -): AnyNode[] { +export function parse_atrule_prelude(at_rule_name: string, prelude: string): AnyNode[] { // Create an arena for the prelude nodes const arena = new CSSDataArena(CSSDataArena.capacity_for_source(prelude.length)) // Create prelude parser - const prelude_parser = new AtRulePreludeParser( - arena, - prelude, - options?.media_feature_range_name_as_child ?? false, - ) + const prelude_parser = new AtRulePreludeParser(arena, prelude) // Parse the entire source as an at-rule prelude const node_indices = prelude_parser.parse_prelude(at_rule_name, 0, prelude.length) diff --git a/src/parse-condition.ts b/src/parse-condition.ts index 8c23cef..f9a3e3d 100644 --- a/src/parse-condition.ts +++ b/src/parse-condition.ts @@ -62,16 +62,7 @@ export class ConditionParser { // doesn't depend on ValueNodeParser/AtRulePreludeParser), so this is safely owned here. private selector_parser: SelectorParser - // Opt-in: also add a FEATURE_RANGE's feature name as an Identifier child (in source order) - private media_feature_range_name_as_child: boolean - - constructor( - arena: CSSDataArena, - source: string, - value_node_parser: ValueNodeParser, - media_feature_range_name_as_child: boolean = false, - ) { - this.media_feature_range_name_as_child = media_feature_range_name_as_child + constructor(arena: CSSDataArena, source: string, value_node_parser: ValueNodeParser) { this.arena = arena this.source = source this.lexer = new Lexer(source) @@ -312,16 +303,14 @@ export class ConditionParser { // Feature name feature_name_start = this.lexer.token_start feature_name_end = this.lexer.token_end - if (this.media_feature_range_name_as_child) { - const name_first = this.parse_feature_value(feature_name_start, feature_name_end) - if (name_first !== 0) { - if (first_child === 0) { - first_child = name_first - } else { - this.arena.set_next_sibling(last_child, name_first) - } - last_child = this.arena.get_last_sibling(name_first) + const name_first = this.parse_feature_value(feature_name_start, feature_name_end) + if (name_first !== 0) { + if (first_child === 0) { + first_child = name_first + } else { + this.arena.set_next_sibling(last_child, name_first) } + last_child = this.arena.get_last_sibling(name_first) } } else { // Value (may itself be a short chain, e.g. a single dimension node). A function diff --git a/src/parse-declaration.ts b/src/parse-declaration.ts index 6eab64c..cfbe8fb 100644 --- a/src/parse-declaration.ts +++ b/src/parse-declaration.ts @@ -40,17 +40,10 @@ export class DeclarationParser { // Reused lookahead snapshot (never nested) to avoid an allocation per declaration private lookahead_position: LexerPosition = new Lexer('').save_position() - constructor( - arena: CSSDataArena, - source: string, - parse_values: boolean = true, - media_feature_range_name_as_child: boolean = false, - ) { + constructor(arena: CSSDataArena, source: string, parse_values: boolean = true) { this.arena = arena this.source = source - this.value_parser = parse_values - ? new ValueParser(arena, source, media_feature_range_name_as_child) - : null + this.value_parser = parse_values ? new ValueParser(arena, source) : null } // Parse a declaration range into a declaration node (standalone use) diff --git a/src/parse-value.test.ts b/src/parse-value.test.ts index eb2fed1..b28ff97 100644 --- a/src/parse-value.test.ts +++ b/src/parse-value.test.ts @@ -1583,11 +1583,8 @@ describe('Value Node Types', () => { expect(at(decl_x.next_sibling)).toEqual([6, 3]) }) - test('with media_feature_range_name_as_child', () => { - const ast = parse( - 'div { color: if(media((400px <= width) and (hover)): blue; else: red) }', - { media_feature_range_name_as_child: true }, - ) + test('range includes the feature name as a child', () => { + const ast = parse('div { color: if(media((400px <= width) and (hover)): blue; else: red) }') const decl = (ast.first_child as Rule).block!.first_child as Declaration const fn = getBranch(decl.value!.first_child as Function, 0)?.first_child as Function const range = (fn.children[0] as any).first_child @@ -1616,15 +1613,13 @@ describe('Value Node Types', () => { const range = mediaFunc.children[0] as FeatureRange expect(range.type).toBe(FEATURE_RANGE) expect(range.name).toBe('width') - expect(range.children.map((c) => c.text)).toEqual(['400px', '<=', '<=', '800px']) + expect(range.children.map((c) => c.text)).toEqual(['400px', '<=', 'width', '<=', '800px']) expect((range.children[1] as PreludeOperator).value).toBe('<=') - expect((range.children[2] as PreludeOperator).value).toBe('<=') + expect((range.children[3] as PreludeOperator).value).toBe('<=') }) - test('media() range includes the feature name as a child with media_feature_range_name_as_child', () => { - const ast = parse('div { color: if(media(400px <= width): blue; else: red) }', { - media_feature_range_name_as_child: true, - }) + test('media() range includes the feature name as a child', () => { + const ast = parse('div { color: if(media(400px <= width): blue; else: red) }') const decl = (ast.first_child as Rule).block!.first_child as Declaration const mediaFunc = getBranch(decl.value!.first_child as Function, 0)?.first_child as Function const range = mediaFunc.children[0] as FeatureRange diff --git a/src/parse-value.ts b/src/parse-value.ts index 41cfdb9..f3fb3ca 100644 --- a/src/parse-value.ts +++ b/src/parse-value.ts @@ -9,13 +9,9 @@ export class ValueParser { private nodes: ValueNodeParser private arena: CSSDataArena - constructor( - arena: CSSDataArena, - source: string, - media_feature_range_name_as_child: boolean = false, - ) { + constructor(arena: CSSDataArena, source: string) { this.arena = arena - this.nodes = new ValueNodeParser(arena, source, media_feature_range_name_as_child) + this.nodes = new ValueNodeParser(arena, source) } // Parse a declaration value range into a VALUE wrapper node diff --git a/src/parse.ts b/src/parse.ts index 1707c9a..1e59790 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -51,13 +51,6 @@ export interface ParserOptions { parse_values?: boolean parse_selectors?: boolean parse_atrule_preludes?: boolean - /** - * Also add the feature name of a media range, e.g. `width` in `(400px < width)`, as an Identifier child. - * Defaults to `false`, but this will become the default in 0.20.0. - * - * @deprecated Opt-in only as a migration aid: in 0.20.0 this behavior is always on and the option will be removed. - */ - media_feature_range_name_as_child?: boolean on_comment?: (info: CommentInfo) => void } @@ -129,14 +122,9 @@ export class Parser { ? new SelectorParser(this.arena, source) : null this.prelude_parser = this.parse_atrule_preludes_enabled - ? new AtRulePreludeParser(this.arena, source, opts.media_feature_range_name_as_child ?? false) + ? new AtRulePreludeParser(this.arena, source) : null - this.declaration_parser = new DeclarationParser( - this.arena, - source, - this.parse_values_enabled, - opts.media_feature_range_name_as_child ?? false, - ) + this.declaration_parser = new DeclarationParser(this.arena, source, this.parse_values_enabled) } // Get the arena (for internal/advanced use only) diff --git a/src/value-node-parser.ts b/src/value-node-parser.ts index 1f3f69f..be6072e 100644 --- a/src/value-node-parser.ts +++ b/src/value-node-parser.ts @@ -76,20 +76,12 @@ export class ValueNodeParser { this.arena, this.source, new ValueNodeParser(this.arena, this.source), - this.media_feature_range_name_as_child, ) } return this._condition_parser } - private media_feature_range_name_as_child: boolean - - constructor( - arena: CSSDataArena, - source: string, - media_feature_range_name_as_child: boolean = false, - ) { - this.media_feature_range_name_as_child = media_feature_range_name_as_child + constructor(arena: CSSDataArena, source: string) { this.arena = arena this.source = source this.lexer = new Lexer(source) @@ -783,11 +775,7 @@ export class ValueNodeParser { private get media_prelude_parser(): AtRulePreludeParser { if (this._media_prelude_parser === null) { - this._media_prelude_parser = new AtRulePreludeParser( - this.arena, - this.source, - this.media_feature_range_name_as_child, - ) + this._media_prelude_parser = new AtRulePreludeParser(this.arena, this.source) } return this._media_prelude_parser }