Skip to content

Commit 7402e33

Browse files
committed
add rule(…) ast helper
This keeps the codebase in a similar state as before, but this just creates one of the follow nodes: `AtRule | StyleRule`
1 parent 8ac9389 commit 7402e33

File tree

5 files changed

+38
-43
lines changed

5 files changed

+38
-43
lines changed

packages/tailwindcss/src/ast.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { parseAtRule } from './css-parser'
2+
13
export type StyleRule = {
24
kind: 'style-rule'
35
selector: string
@@ -34,6 +36,7 @@ export type AtRoot = {
3436
nodes: AstNode[]
3537
}
3638

39+
export type Rule = StyleRule | AtRule
3740
export type AstNode = StyleRule | AtRule | Declaration | Comment | Context | AtRoot
3841

3942
export function styleRule(selector: string, nodes: AstNode[] = []): StyleRule {
@@ -53,6 +56,14 @@ export function atRule(name: string, params: string = '', nodes: AstNode[] = [])
5356
}
5457
}
5558

59+
export function rule(selector: string, nodes: AstNode[] = []): StyleRule | AtRule {
60+
if (selector[0] === '@') {
61+
return parseAtRule(selector, nodes)
62+
}
63+
64+
return styleRule(selector, nodes)
65+
}
66+
5667
export function decl(property: string, value: string | undefined): Declaration {
5768
return {
5869
kind: 'declaration',

packages/tailwindcss/src/compat/plugin-api.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { substituteAtApply } from '../apply'
2-
import { atRule, decl, styleRule, type AstNode } from '../ast'
2+
import { atRule, decl, rule, styleRule, type AstNode } from '../ast'
33
import type { Candidate, CandidateModifier, NamedUtilityValue } from '../candidate'
44
import { substituteFunctions } from '../css-functions'
55
import * as CSS from '../css-parser'
@@ -432,11 +432,7 @@ export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] {
432432
for (let [name, value] of entries) {
433433
if (typeof value !== 'object') {
434434
if (!name.startsWith('--') && value === '@slot') {
435-
if (name[0] === '@') {
436-
ast.push(CSS.parseAtRule(name, [atRule('slot')]))
437-
} else {
438-
ast.push(styleRule(name, [atRule('slot')]))
439-
}
435+
ast.push(rule(name, [atRule('slot')]))
440436
} else {
441437
// Convert camelCase to kebab-case:
442438
// https://github.com/postcss/postcss-js/blob/b3db658b932b42f6ac14ca0b1d50f50c4569805b/parser.js#L30-L35
@@ -449,19 +445,11 @@ export function objectToAst(rules: CssInJs | CssInJs[]): AstNode[] {
449445
if (typeof item === 'string') {
450446
ast.push(decl(name, item))
451447
} else {
452-
if (name[0] === '@') {
453-
ast.push(CSS.parseAtRule(name, objectToAst(item)))
454-
} else {
455-
ast.push(styleRule(name, objectToAst(item)))
456-
}
448+
ast.push(rule(name, objectToAst(item)))
457449
}
458450
}
459451
} else if (value !== null) {
460-
if (name[0] === '@') {
461-
ast.push(CSS.parseAtRule(name, objectToAst(value)))
462-
} else {
463-
ast.push(styleRule(name, objectToAst(value)))
464-
}
452+
ast.push(rule(name, objectToAst(value)))
465453
}
466454
}
467455

packages/tailwindcss/src/compile.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { atRule, decl, styleRule, walk, WalkAction, type AstNode, type AtRule, type StyleRule } from './ast'
1+
import {
2+
atRule,
3+
decl,
4+
rule,
5+
walk,
6+
WalkAction,
7+
type AstNode,
8+
type Rule,
9+
type StyleRule,
10+
} from './ast'
211
import { type Candidate, type Variant } from './candidate'
312
import { substituteFunctions } from './css-functions'
4-
import { parseAtRule } from './css-parser'
513
import { type DesignSystem } from './design-system'
614
import GLOBAL_PROPERTY_ORDER from './property-order'
715
import { asColor, type Utility } from './utilities'
@@ -170,7 +178,7 @@ export function compileAstNodes(candidate: Candidate, designSystem: DesignSystem
170178
}
171179

172180
export function applyVariant(
173-
node: AtRule | StyleRule,
181+
node: Rule,
174182
variant: Variant,
175183
variants: Variants,
176184
depth: number = 0,
@@ -182,11 +190,7 @@ export function applyVariant(
182190
// E.g. `[>img]:flex` is not valid, but `has-[>img]:flex` is
183191
if (variant.relative && depth === 0) return null
184192

185-
if (variant.selector[0] === '@') {
186-
node.nodes = [parseAtRule(variant.selector, node.nodes)]
187-
} else {
188-
node.nodes = [styleRule(variant.selector, node.nodes)]
189-
}
193+
node.nodes = [rule(variant.selector, node.nodes)]
190194
return
191195
}
192196

packages/tailwindcss/src/css-parser.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {
22
atRule,
33
comment,
4-
styleRule,
4+
rule,
55
type AstNode,
66
type AtRule,
77
type Comment,
88
type Declaration,
9-
type StyleRule,
9+
type Rule,
1010
} from './ast'
1111

1212
const BACKSLASH = 0x5c
@@ -35,9 +35,9 @@ export function parse(input: string) {
3535
let ast: AstNode[] = []
3636
let licenseComments: Comment[] = []
3737

38-
let stack: (StyleRule | AtRule | null)[] = []
38+
let stack: (Rule | null)[] = []
3939

40-
let parent = null as (StyleRule | AtRule) | null
40+
let parent = null as Rule | null
4141
let node = null as AstNode | null
4242

4343
let buffer = ''
@@ -347,11 +347,7 @@ export function parse(input: string) {
347347
closingBracketStack += '}'
348348

349349
// At this point `buffer` should resemble a selector or an at-rule.
350-
if (buffer.charCodeAt(0) === AT_SIGN) {
351-
node = parseAtRule(buffer)
352-
} else {
353-
node = styleRule(buffer.trim(), [])
354-
}
350+
node = rule(buffer.trim())
355351

356352
// Attach the rule to the parent in case it's nested.
357353
if (parent) {
@@ -502,6 +498,7 @@ export function parse(input: string) {
502498
export function parseAtRule(buffer: string, nodes: AstNode[] = []): AtRule {
503499
let splitIdx = buffer.search(/[\s(]/g)
504500
if (splitIdx === -1) return atRule(buffer.slice(1).trim(), '', nodes)
501+
505502
let name = buffer.slice(1, splitIdx).trim()
506503
let params = buffer.slice(splitIdx).trim()
507504
return atRule(name, params, nodes)

packages/tailwindcss/src/variants.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@ import {
33
atRoot,
44
atRule,
55
decl,
6+
rule,
67
styleRule,
78
walk,
89
type AstNode,
910
type AtRule,
11+
type Rule,
1012
type StyleRule,
1113
} from './ast'
1214
import { type Variant } from './candidate'
13-
import { parseAtRule } from './css-parser'
1415
import type { Theme } from './theme'
1516
import { DefaultMap } from './utils/default-map'
1617
import { isPositiveInteger } from './utils/infer-data-type'
1718
import { segment } from './utils/segment'
1819

1920
type VariantFn<T extends Variant['kind']> = (
20-
rule: AtRule | StyleRule,
21+
rule: Rule,
2122
variant: Extract<Variant, { kind: T }>,
2223
) => null | void
2324

@@ -324,13 +325,7 @@ export function createVariants(theme: Theme): Variants {
324325
variants.static(
325326
name,
326327
(r) => {
327-
r.nodes = selectors.map((selector) => {
328-
if (selector[0] === '@') {
329-
return parseAtRule(selector, r.nodes)
330-
} else {
331-
return styleRule(selector, r.nodes)
332-
}
333-
})
328+
r.nodes = selectors.map((selector) => rule(selector, r.nodes))
334329
},
335330
{ compounds },
336331
)
@@ -438,7 +433,7 @@ export function createVariants(theme: Theme): Variants {
438433
if (atRules.length > 1) return WalkAction.Stop
439434
if (styleRules.length > 1) return WalkAction.Stop
440435

441-
let rules: (StyleRule | AtRule)[] = []
436+
let rules: Rule[] = []
442437

443438
for (let node of styleRules) {
444439
let selector = negateSelector(node.selector)

0 commit comments

Comments
 (0)