@chikx/compiler 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/package.json +3 -3
- package/src/emitter/index.ts +658 -0
- package/src/formatter/index.ts +544 -0
- package/src/host/node.ts +85 -0
- package/src/index.ts +1021 -0
- package/src/ir/factories.ts +85 -0
- package/src/ir/index.ts +28 -0
- package/src/ir/types.ts +145 -0
- package/src/lexer/at-keywords.ts +35 -0
- package/src/lexer/characters.ts +82 -0
- package/src/lexer/comments.ts +27 -0
- package/src/lexer/diagnostics.ts +42 -0
- package/src/lexer/dollar-refs.ts +23 -0
- package/src/lexer/index.ts +37 -0
- package/src/lexer/names.ts +49 -0
- package/src/lexer/numbers.ts +59 -0
- package/src/lexer/scanner.ts +199 -0
- package/src/lexer/strings.ts +52 -0
- package/src/lexer/urls.ts +63 -0
- package/src/lowering/expand-groups.ts +206 -0
- package/src/lowering/group.ts +119 -0
- package/src/lowering/index.ts +43 -0
- package/src/lowering/lower.ts +441 -0
- package/src/lowering/media.ts +314 -0
- package/src/lowering/merge.ts +163 -0
- package/src/lowering/property-families.ts +194 -0
- package/src/lowering/spans.ts +118 -0
- package/src/naming/index.ts +44 -0
- package/src/naming/mapgen.ts +252 -0
- package/src/naming/merge.ts +114 -0
- package/src/naming/rewrite.ts +333 -0
- package/src/naming/scan.ts +267 -0
- package/src/naming/table.ts +153 -0
- package/src/naming/types.ts +100 -0
- package/src/output/import-path.ts +30 -0
- package/src/output/index.ts +213 -0
- package/src/output/manifest.ts +28 -0
- package/src/output/paths.ts +200 -0
- package/src/output/resolve.ts +401 -0
- package/src/output/validate.ts +251 -0
- package/src/parser/cursor.ts +138 -0
- package/src/parser/declaration.ts +247 -0
- package/src/parser/diagnostics.ts +75 -0
- package/src/parser/group.ts +303 -0
- package/src/parser/index.ts +50 -0
- package/src/parser/modifier.ts +60 -0
- package/src/parser/recovery.ts +88 -0
- package/src/parser/rule.ts +370 -0
- package/src/parser/statement.ts +47 -0
- package/src/parser/statements.ts +667 -0
- package/src/parser/value.ts +265 -0
- package/src/resolver/graph.ts +467 -0
- package/src/resolver/groups.ts +177 -0
- package/src/resolver/index.ts +68 -0
- package/src/resolver/keyframes.ts +131 -0
- package/src/resolver/paths.ts +142 -0
- package/src/resolver/rules.ts +54 -0
- package/src/resolver/scopes.ts +342 -0
- package/src/resolver/types.ts +259 -0
- package/src/selectors/flatten.ts +448 -0
- package/src/selectors/index.ts +14 -0
- package/src/selectors/scan.ts +227 -0
- package/src/selectors/specificity.ts +319 -0
- package/src/setup-list.ts +172 -0
- package/src/sourcemaps/index.ts +25 -0
- package/src/sourcemaps/mappings.ts +190 -0
- package/src/sourcemaps/vlq.ts +29 -0
- package/src/types/ast.ts +318 -0
- package/src/types/diagnostics.ts +124 -0
- package/src/types/options.ts +286 -0
- package/src/types/result.ts +121 -0
- package/src/types/span.ts +18 -0
- package/src/types/token.ts +77 -0
- package/src/variables/ast.ts +182 -0
- package/src/variables/expand.ts +319 -0
- package/src/variables/index.ts +38 -0
- package/src/variables/naming.ts +46 -0
- package/src/variables/runtime.ts +336 -0
- package/src/variables/substitute.ts +148 -0
- package/src/variables/walk.ts +147 -0
- package/dist/output/importPath.d.ts +0 -27
- package/dist/output/importPath.d.ts.map +0 -1
- package/dist/output/importPath.js +0 -84
- package/dist/output/importPath.js.map +0 -1
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
import {
|
|
2
|
+
atRuleContextIr,
|
|
3
|
+
declarationIr,
|
|
4
|
+
nativeBlockIr,
|
|
5
|
+
ruleIr,
|
|
6
|
+
stylesheetIr,
|
|
7
|
+
} from "../ir/factories.js";
|
|
8
|
+
import type {
|
|
9
|
+
AtRuleContextIr,
|
|
10
|
+
DeclarationIr,
|
|
11
|
+
MediaConditionIr,
|
|
12
|
+
NativeBlockChild,
|
|
13
|
+
NativeBlockIr,
|
|
14
|
+
RuleIr,
|
|
15
|
+
StylesheetIr,
|
|
16
|
+
} from "../ir/types.js";
|
|
17
|
+
import type {
|
|
18
|
+
BlockItem,
|
|
19
|
+
ConditionalBlock,
|
|
20
|
+
Declaration,
|
|
21
|
+
NativeAtRule,
|
|
22
|
+
Stylesheet,
|
|
23
|
+
} from "../types/ast.js";
|
|
24
|
+
import type { Diagnostic, DiagnosticCode } from "../types/diagnostics.js";
|
|
25
|
+
import { error } from "../types/diagnostics.js";
|
|
26
|
+
import type { CompileOptions } from "../types/options.js";
|
|
27
|
+
import type { Span } from "../types/span.js";
|
|
28
|
+
import { buildMediaAliasTable, resolveMediaCondition } from "./media.js";
|
|
29
|
+
import type { ConditionAlias, MediaAliasTable } from "./media.js";
|
|
30
|
+
import { declarationSegmentSpans, modifierAliasSpans, sameCondition } from "./spans.js";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* AST to immutable CSS IR lowering (SPECIFICATION.md §5).
|
|
34
|
+
*
|
|
35
|
+
* The pass is pure and deterministic: it never reads the filesystem, never
|
|
36
|
+
* mutates the AST and never reorders, merges or deduplicates declarations. Each
|
|
37
|
+
* authored value segment becomes one {@link DeclarationIr}; consecutive segments
|
|
38
|
+
* with the same condition form one {@link RuleIr}. A run is flushed at a nested
|
|
39
|
+
* rule, a conditional block or a native at-rule (§5.1), so later base
|
|
40
|
+
* declarations stay later and keep native cascade precedence (§5.2).
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/** At-rules whose contents cannot carry Chikx conditions (SPECIFICATION.md §3.5). */
|
|
44
|
+
const RESTRICTED_NATIVE_AT_RULES: ReadonlySet<string> = new Set([
|
|
45
|
+
"keyframes",
|
|
46
|
+
"font-face",
|
|
47
|
+
"property",
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Descriptor at-rules whose block holds selector-less declarations by design:
|
|
52
|
+
* `@font-face` descriptors, `@property` registrations (SPECIFICATION.md §3.5) and
|
|
53
|
+
* classic `@page` boxes, which take declarations natively. Any other native
|
|
54
|
+
* block that reaches a lowerer without an enclosing rule has invalid structure
|
|
55
|
+
* and is diagnosed instead of silently dropped.
|
|
56
|
+
*/
|
|
57
|
+
const DESCRIPTOR_NATIVE_AT_RULES: ReadonlySet<string> = new Set(["font-face", "property", "page"]);
|
|
58
|
+
|
|
59
|
+
type NodeSink = (node: RuleIr | NativeBlockIr) => void;
|
|
60
|
+
|
|
61
|
+
/** The selector and ancestor chain a declaration run belongs to. */
|
|
62
|
+
interface Owner {
|
|
63
|
+
readonly selector: string;
|
|
64
|
+
readonly ancestors: readonly string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Inherited lowering state at one point in the walk. */
|
|
68
|
+
interface Scope {
|
|
69
|
+
/** Resolved Chikx condition inherited from enclosing conditional blocks. */
|
|
70
|
+
readonly condition: MediaConditionIr | null;
|
|
71
|
+
/** Alias references that produced {@link Scope.condition}, for later combination. */
|
|
72
|
+
readonly aliases: readonly ConditionAlias[];
|
|
73
|
+
/** Native at-rule stack, outermost-first. */
|
|
74
|
+
readonly context: readonly AtRuleContextIr[];
|
|
75
|
+
/** Name of the enclosing restricted at-rule, or `null` when conditions are allowed. */
|
|
76
|
+
readonly restricted: string | null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface OpenRun {
|
|
80
|
+
readonly condition: MediaConditionIr | null;
|
|
81
|
+
readonly declarations: DeclarationIr[];
|
|
82
|
+
readonly span: Span;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const ROOT_SCOPE: Scope = Object.freeze({
|
|
86
|
+
condition: null,
|
|
87
|
+
aliases: Object.freeze([]),
|
|
88
|
+
context: Object.freeze([]),
|
|
89
|
+
restricted: null,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
class Lowerer {
|
|
93
|
+
readonly #table: MediaAliasTable;
|
|
94
|
+
readonly #diagnostics: Diagnostic[] = [];
|
|
95
|
+
|
|
96
|
+
constructor(table: MediaAliasTable) {
|
|
97
|
+
this.#table = table;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
get diagnostics(): Diagnostic[] {
|
|
101
|
+
return this.#diagnostics;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
run(ast: Stylesheet): StylesheetIr {
|
|
105
|
+
const rules: (RuleIr | NativeBlockIr)[] = [];
|
|
106
|
+
for (const item of ast.items) {
|
|
107
|
+
switch (item.kind) {
|
|
108
|
+
case "rule":
|
|
109
|
+
this.#lowerBody(
|
|
110
|
+
item.body,
|
|
111
|
+
{ selector: item.selector, ancestors: [] },
|
|
112
|
+
ROOT_SCOPE,
|
|
113
|
+
(node) => rules.push(node),
|
|
114
|
+
);
|
|
115
|
+
break;
|
|
116
|
+
case "native-at-rule":
|
|
117
|
+
this.#lowerNative(item, null, ROOT_SCOPE, (node) => rules.push(node));
|
|
118
|
+
break;
|
|
119
|
+
default:
|
|
120
|
+
// @setup, @use, @variables and @media-types emit no CSS; @runtime is
|
|
121
|
+
// out of scope for A05 (P3/A08 owns custom-property emission).
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return stylesheetIr(rules);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#report(code: DiagnosticCode, message: string, span: Span): void {
|
|
129
|
+
this.#diagnostics.push(error(code, message, span));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
#lowerBody(items: readonly BlockItem[], owner: Owner, scope: Scope, sink: NodeSink): void {
|
|
133
|
+
let open: OpenRun | null = null;
|
|
134
|
+
|
|
135
|
+
const flush = (): void => {
|
|
136
|
+
if (open === null) return;
|
|
137
|
+
if (open.declarations.length > 0) {
|
|
138
|
+
sink(
|
|
139
|
+
ruleIr({
|
|
140
|
+
selector: owner.selector,
|
|
141
|
+
ancestors: owner.ancestors,
|
|
142
|
+
declarations: open.declarations,
|
|
143
|
+
condition: open.condition,
|
|
144
|
+
context: scope.context,
|
|
145
|
+
span: open.span,
|
|
146
|
+
}),
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
open = null;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const add = (declaration: DeclarationIr, condition: MediaConditionIr | null): void => {
|
|
153
|
+
if (open !== null && !sameCondition(open.condition, condition)) flush();
|
|
154
|
+
if (open === null) {
|
|
155
|
+
open = { condition, declarations: [declaration], span: declaration.span };
|
|
156
|
+
} else {
|
|
157
|
+
open.declarations.push(declaration);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
for (const item of items) {
|
|
162
|
+
switch (item.kind) {
|
|
163
|
+
case "declaration": {
|
|
164
|
+
if (scope.restricted !== null) {
|
|
165
|
+
this.#lowerRestrictedDeclaration(item, scope, add);
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
const spans = declarationSegmentSpans(item);
|
|
169
|
+
add(
|
|
170
|
+
declarationIr(item.property, item.value, spans.base, item.important),
|
|
171
|
+
scope.condition,
|
|
172
|
+
);
|
|
173
|
+
for (let index = 0; index < item.variants.length; index++) {
|
|
174
|
+
const variant = item.variants[index];
|
|
175
|
+
if (variant === undefined) continue;
|
|
176
|
+
const span = spans.variants[index] ?? item.span;
|
|
177
|
+
if (variant.aliases.length === 0) {
|
|
178
|
+
add(
|
|
179
|
+
declarationIr(item.property, variant.value, span, variant.important),
|
|
180
|
+
scope.condition,
|
|
181
|
+
);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const entries = [...scope.aliases, ...modifierAliasSpans(variant)];
|
|
185
|
+
const resolution = resolveMediaCondition(entries, this.#table, variant.span);
|
|
186
|
+
if (!resolution.ok) {
|
|
187
|
+
this.#diagnostics.push(resolution.diagnostic);
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
add(
|
|
191
|
+
declarationIr(item.property, variant.value, span, variant.important),
|
|
192
|
+
resolution.condition,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
case "conditional-block": {
|
|
198
|
+
flush();
|
|
199
|
+
this.#lowerConditional(item, owner, scope, sink);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
case "rule": {
|
|
203
|
+
flush();
|
|
204
|
+
const child: Owner = {
|
|
205
|
+
selector: item.selector,
|
|
206
|
+
ancestors: [...owner.ancestors, owner.selector],
|
|
207
|
+
};
|
|
208
|
+
this.#lowerBody(item.body, child, scope, sink);
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
case "native-at-rule": {
|
|
212
|
+
flush();
|
|
213
|
+
this.#lowerNative(item, owner, scope, sink);
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
flush();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Emit a declaration inside `@keyframes`, `@font-face` or `@property`.
|
|
224
|
+
*
|
|
225
|
+
* Plain declarations pass through; inline modifiers are a Chikx transformation
|
|
226
|
+
* the context cannot express, so CHIKX3001 is reported and the declaration is
|
|
227
|
+
* omitted (§3.5, §4.3).
|
|
228
|
+
*/
|
|
229
|
+
#lowerRestrictedDeclaration(
|
|
230
|
+
declaration: Declaration,
|
|
231
|
+
scope: Scope,
|
|
232
|
+
add: (declaration: DeclarationIr, condition: MediaConditionIr | null) => void,
|
|
233
|
+
): void {
|
|
234
|
+
if (declaration.variants.length > 0 || scope.aliases.length > 0) {
|
|
235
|
+
this.#report(
|
|
236
|
+
"CHIKX3001",
|
|
237
|
+
`Unsupported Chikx transformation in this context: inline modifiers are not allowed inside @${scope.restricted as string}.`,
|
|
238
|
+
declaration.span,
|
|
239
|
+
);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
add(
|
|
243
|
+
declarationIr(
|
|
244
|
+
declaration.property,
|
|
245
|
+
declaration.value,
|
|
246
|
+
declaration.span,
|
|
247
|
+
declaration.important,
|
|
248
|
+
),
|
|
249
|
+
null,
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
#lowerConditional(block: ConditionalBlock, owner: Owner, scope: Scope, sink: NodeSink): void {
|
|
254
|
+
if (scope.restricted !== null) {
|
|
255
|
+
this.#report(
|
|
256
|
+
"CHIKX3001",
|
|
257
|
+
`Unsupported Chikx transformation in this context: conditional blocks are not allowed inside @${scope.restricted}.`,
|
|
258
|
+
block.span,
|
|
259
|
+
);
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const entries = [...scope.aliases, ...modifierAliasSpans(block.variant)];
|
|
263
|
+
const resolution = resolveMediaCondition(entries, this.#table, block.span);
|
|
264
|
+
if (!resolution.ok) {
|
|
265
|
+
this.#diagnostics.push(resolution.diagnostic);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
this.#lowerBody(
|
|
269
|
+
block.body,
|
|
270
|
+
owner,
|
|
271
|
+
{
|
|
272
|
+
condition: resolution.condition,
|
|
273
|
+
aliases: resolution.aliases,
|
|
274
|
+
context: scope.context,
|
|
275
|
+
restricted: null,
|
|
276
|
+
},
|
|
277
|
+
sink,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
#lowerNative(atRule: NativeAtRule, owner: Owner | null, scope: Scope, sink: NodeSink): void {
|
|
282
|
+
if (atRule.block === undefined) {
|
|
283
|
+
sink(
|
|
284
|
+
nativeBlockIr({
|
|
285
|
+
name: atRule.name,
|
|
286
|
+
prelude: atRule.prelude,
|
|
287
|
+
statement: true,
|
|
288
|
+
children: [],
|
|
289
|
+
span: atRule.span,
|
|
290
|
+
}),
|
|
291
|
+
);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const context = atRuleContextIr(atRule.name, atRule.prelude, atRule.span);
|
|
296
|
+
const nextScope: Scope = { ...scope, context: [...scope.context, context] };
|
|
297
|
+
const lowerName = atRule.name.toLowerCase();
|
|
298
|
+
const ownRestriction = RESTRICTED_NATIVE_AT_RULES.has(lowerName);
|
|
299
|
+
const descriptorBlock = DESCRIPTOR_NATIVE_AT_RULES.has(lowerName);
|
|
300
|
+
if (ownRestriction || descriptorBlock || scope.restricted !== null) {
|
|
301
|
+
const restricted = ownRestriction ? atRule.name : (scope.restricted ?? atRule.name);
|
|
302
|
+
this.#lowerRestrictedBlock(atRule, nextScope, restricted, sink);
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const children: NativeBlockChild[] = [];
|
|
307
|
+
const childSink: NodeSink = (node) => children.push(node);
|
|
308
|
+
|
|
309
|
+
if (owner !== null) {
|
|
310
|
+
// A native at-rule inside a rule: direct declarations still belong to the
|
|
311
|
+
// enclosing selector, nested rules are its descendants.
|
|
312
|
+
this.#lowerBody(atRule.block, owner, nextScope, childSink);
|
|
313
|
+
} else {
|
|
314
|
+
// A top-level native at-rule has no enclosing selector. Nested rules and
|
|
315
|
+
// nested native blocks can still form CSS; a bare declaration or a bare
|
|
316
|
+
// conditional block cannot, so both are diagnosed rather than dropped
|
|
317
|
+
// silently (SPECIFICATION.md §3.4, ADR-0004).
|
|
318
|
+
for (const item of atRule.block) {
|
|
319
|
+
switch (item.kind) {
|
|
320
|
+
case "rule":
|
|
321
|
+
this.#lowerBody(
|
|
322
|
+
item.body,
|
|
323
|
+
{ selector: item.selector, ancestors: [] },
|
|
324
|
+
nextScope,
|
|
325
|
+
childSink,
|
|
326
|
+
);
|
|
327
|
+
break;
|
|
328
|
+
case "native-at-rule":
|
|
329
|
+
this.#lowerNative(item, null, nextScope, childSink);
|
|
330
|
+
break;
|
|
331
|
+
case "declaration":
|
|
332
|
+
this.#report("CHIKX1001", "declaration outside a rule", item.span);
|
|
333
|
+
break;
|
|
334
|
+
case "conditional-block":
|
|
335
|
+
this.#report("CHIKX1001", "conditional block outside a rule", item.span);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
sink(
|
|
342
|
+
nativeBlockIr({
|
|
343
|
+
name: atRule.name,
|
|
344
|
+
prelude: atRule.prelude,
|
|
345
|
+
children,
|
|
346
|
+
span: atRule.span,
|
|
347
|
+
}),
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Lower a native at-rule that cannot carry Chikx conditions.
|
|
353
|
+
*
|
|
354
|
+
* Descriptor declarations are emitted as {@link DeclarationIr} children
|
|
355
|
+
* because they are not selector-bound; keyframe steps stay {@link RuleIr}
|
|
356
|
+
* nodes with their authored keyframe selector text. A direct declaration in a
|
|
357
|
+
* non-descriptor block (`@keyframes`, nested `@media`, ...) has no rule to
|
|
358
|
+
* belong to, so it is diagnosed as CHIKX1001 instead of silently dropped.
|
|
359
|
+
*/
|
|
360
|
+
#lowerRestrictedBlock(
|
|
361
|
+
atRule: NativeAtRule,
|
|
362
|
+
scope: Scope,
|
|
363
|
+
restricted: string,
|
|
364
|
+
sink: NodeSink,
|
|
365
|
+
): void {
|
|
366
|
+
const restrictedScope: Scope = { ...scope, restricted };
|
|
367
|
+
const children: NativeBlockChild[] = [];
|
|
368
|
+
const descriptorBlock =
|
|
369
|
+
scope.restricted === null && DESCRIPTOR_NATIVE_AT_RULES.has(atRule.name.toLowerCase());
|
|
370
|
+
|
|
371
|
+
for (const item of atRule.block ?? []) {
|
|
372
|
+
switch (item.kind) {
|
|
373
|
+
case "declaration": {
|
|
374
|
+
if (!descriptorBlock) {
|
|
375
|
+
this.#report("CHIKX1001", "declaration outside a rule", item.span);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
if (item.variants.length > 0 || scope.condition !== null) {
|
|
379
|
+
this.#report(
|
|
380
|
+
"CHIKX3001",
|
|
381
|
+
`Unsupported Chikx transformation in this context: inline modifiers are not allowed inside @${restricted}.`,
|
|
382
|
+
item.span,
|
|
383
|
+
);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
children.push(declarationIr(item.property, item.value, item.span, item.important));
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
case "conditional-block":
|
|
390
|
+
this.#report(
|
|
391
|
+
"CHIKX3001",
|
|
392
|
+
`Unsupported Chikx transformation in this context: conditional blocks are not allowed inside @${restricted}.`,
|
|
393
|
+
item.span,
|
|
394
|
+
);
|
|
395
|
+
break;
|
|
396
|
+
case "rule":
|
|
397
|
+
this.#lowerBody(
|
|
398
|
+
item.body,
|
|
399
|
+
{ selector: item.selector, ancestors: [] },
|
|
400
|
+
restrictedScope,
|
|
401
|
+
(node) => children.push(node),
|
|
402
|
+
);
|
|
403
|
+
break;
|
|
404
|
+
case "native-at-rule":
|
|
405
|
+
this.#lowerNative(item, null, restrictedScope, (node) => children.push(node));
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
sink(
|
|
411
|
+
nativeBlockIr({
|
|
412
|
+
name: atRule.name,
|
|
413
|
+
prelude: atRule.prelude,
|
|
414
|
+
children,
|
|
415
|
+
span: atRule.span,
|
|
416
|
+
}),
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Lower a parsed stylesheet into the immutable CSS IR.
|
|
423
|
+
*
|
|
424
|
+
* Pure and deterministic: the AST is treated as read-only and the result is
|
|
425
|
+
* frozen. `options.media` supplies project-level alias overrides; a file's
|
|
426
|
+
* `@media-types` still wins (ADR-0006). Diagnostics are CHIKX3001 errors in
|
|
427
|
+
* source order, and a rejected declaration or block contributes no IR.
|
|
428
|
+
*
|
|
429
|
+
* Out of scope by design: `@runtime` blocks emit no IR (P3/A08), `$variable`
|
|
430
|
+
* values stay raw (A08), selectors stay nested and unprefixed (A06/A09) and no
|
|
431
|
+
* CSS text is produced (A06).
|
|
432
|
+
*/
|
|
433
|
+
export function lower(
|
|
434
|
+
ast: Stylesheet,
|
|
435
|
+
options: CompileOptions,
|
|
436
|
+
): { ir: StylesheetIr; diagnostics: Diagnostic[] } {
|
|
437
|
+
const table = buildMediaAliasTable(ast, options.media);
|
|
438
|
+
const lowerer = new Lowerer(table);
|
|
439
|
+
const ir = lowerer.run(ast);
|
|
440
|
+
return { ir, diagnostics: [...lowerer.diagnostics] };
|
|
441
|
+
}
|