@chikx/compiler 0.0.0-stage → 0.1.0

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.
Files changed (323) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +40 -2
  3. package/dist/emitter/index.d.ts +96 -0
  4. package/dist/emitter/index.d.ts.map +1 -0
  5. package/dist/emitter/index.js +415 -0
  6. package/dist/emitter/index.js.map +1 -0
  7. package/dist/formatter/index.d.ts +53 -0
  8. package/dist/formatter/index.d.ts.map +1 -0
  9. package/dist/formatter/index.js +464 -0
  10. package/dist/formatter/index.js.map +1 -0
  11. package/dist/host/node.d.ts +34 -0
  12. package/dist/host/node.d.ts.map +1 -0
  13. package/dist/host/node.js +80 -0
  14. package/dist/host/node.js.map +1 -0
  15. package/dist/index.d.ts +93 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +792 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/ir/factories.d.ts +35 -0
  20. package/dist/ir/factories.d.ts.map +1 -0
  21. package/dist/ir/factories.js +47 -0
  22. package/dist/ir/factories.js.map +1 -0
  23. package/dist/ir/index.d.ts +13 -0
  24. package/dist/ir/index.d.ts.map +1 -0
  25. package/dist/ir/index.js +2 -0
  26. package/dist/ir/index.js.map +1 -0
  27. package/dist/ir/types.d.ts +139 -0
  28. package/dist/ir/types.d.ts.map +1 -0
  29. package/dist/ir/types.js +2 -0
  30. package/dist/ir/types.js.map +1 -0
  31. package/dist/lexer/at-keywords.d.ts +27 -0
  32. package/dist/lexer/at-keywords.d.ts.map +1 -0
  33. package/dist/lexer/at-keywords.js +35 -0
  34. package/dist/lexer/at-keywords.js.map +1 -0
  35. package/dist/lexer/characters.d.ts +38 -0
  36. package/dist/lexer/characters.d.ts.map +1 -0
  37. package/dist/lexer/characters.js +74 -0
  38. package/dist/lexer/characters.js.map +1 -0
  39. package/dist/lexer/comments.d.ts +22 -0
  40. package/dist/lexer/comments.d.ts.map +1 -0
  41. package/dist/lexer/comments.js +20 -0
  42. package/dist/lexer/comments.js.map +1 -0
  43. package/dist/lexer/diagnostics.d.ts +24 -0
  44. package/dist/lexer/diagnostics.d.ts.map +1 -0
  45. package/dist/lexer/diagnostics.js +29 -0
  46. package/dist/lexer/diagnostics.js.map +1 -0
  47. package/dist/lexer/dollar-refs.d.ts +13 -0
  48. package/dist/lexer/dollar-refs.d.ts.map +1 -0
  49. package/dist/lexer/dollar-refs.js +22 -0
  50. package/dist/lexer/dollar-refs.js.map +1 -0
  51. package/dist/lexer/index.d.ts +32 -0
  52. package/dist/lexer/index.d.ts.map +1 -0
  53. package/dist/lexer/index.js +26 -0
  54. package/dist/lexer/index.js.map +1 -0
  55. package/dist/lexer/names.d.ts +23 -0
  56. package/dist/lexer/names.d.ts.map +1 -0
  57. package/dist/lexer/names.js +48 -0
  58. package/dist/lexer/names.js.map +1 -0
  59. package/dist/lexer/numbers.d.ts +22 -0
  60. package/dist/lexer/numbers.d.ts.map +1 -0
  61. package/dist/lexer/numbers.js +60 -0
  62. package/dist/lexer/numbers.js.map +1 -0
  63. package/dist/lexer/scanner.d.ts +30 -0
  64. package/dist/lexer/scanner.d.ts.map +1 -0
  65. package/dist/lexer/scanner.js +158 -0
  66. package/dist/lexer/scanner.js.map +1 -0
  67. package/dist/lexer/strings.d.ts +25 -0
  68. package/dist/lexer/strings.d.ts.map +1 -0
  69. package/dist/lexer/strings.js +41 -0
  70. package/dist/lexer/strings.js.map +1 -0
  71. package/dist/lexer/urls.d.ts +28 -0
  72. package/dist/lexer/urls.d.ts.map +1 -0
  73. package/dist/lexer/urls.js +54 -0
  74. package/dist/lexer/urls.js.map +1 -0
  75. package/dist/lowering/expand-groups.d.ts +61 -0
  76. package/dist/lowering/expand-groups.d.ts.map +1 -0
  77. package/dist/lowering/expand-groups.js +117 -0
  78. package/dist/lowering/expand-groups.js.map +1 -0
  79. package/dist/lowering/group.d.ts +30 -0
  80. package/dist/lowering/group.d.ts.map +1 -0
  81. package/dist/lowering/group.js +109 -0
  82. package/dist/lowering/group.js.map +1 -0
  83. package/dist/lowering/index.d.ts +26 -0
  84. package/dist/lowering/index.d.ts.map +1 -0
  85. package/dist/lowering/index.js +23 -0
  86. package/dist/lowering/index.js.map +1 -0
  87. package/dist/lowering/lower.d.ts +21 -0
  88. package/dist/lowering/lower.d.ts.map +1 -0
  89. package/dist/lowering/lower.js +296 -0
  90. package/dist/lowering/lower.js.map +1 -0
  91. package/dist/lowering/media.d.ts +63 -0
  92. package/dist/lowering/media.d.ts.map +1 -0
  93. package/dist/lowering/media.js +237 -0
  94. package/dist/lowering/media.js.map +1 -0
  95. package/dist/lowering/merge.d.ts +35 -0
  96. package/dist/lowering/merge.d.ts.map +1 -0
  97. package/dist/lowering/merge.js +146 -0
  98. package/dist/lowering/merge.js.map +1 -0
  99. package/dist/lowering/property-families.d.ts +50 -0
  100. package/dist/lowering/property-families.d.ts.map +1 -0
  101. package/dist/lowering/property-families.js +175 -0
  102. package/dist/lowering/property-families.js.map +1 -0
  103. package/dist/lowering/spans.d.ts +36 -0
  104. package/dist/lowering/spans.d.ts.map +1 -0
  105. package/dist/lowering/spans.js +100 -0
  106. package/dist/lowering/spans.js.map +1 -0
  107. package/dist/naming/index.d.ts +37 -0
  108. package/dist/naming/index.d.ts.map +1 -0
  109. package/dist/naming/index.js +34 -0
  110. package/dist/naming/index.js.map +1 -0
  111. package/dist/naming/mapgen.d.ts +70 -0
  112. package/dist/naming/mapgen.d.ts.map +1 -0
  113. package/dist/naming/mapgen.js +153 -0
  114. package/dist/naming/mapgen.js.map +1 -0
  115. package/dist/naming/merge.d.ts +62 -0
  116. package/dist/naming/merge.d.ts.map +1 -0
  117. package/dist/naming/merge.js +77 -0
  118. package/dist/naming/merge.js.map +1 -0
  119. package/dist/naming/rewrite.d.ts +14 -0
  120. package/dist/naming/rewrite.d.ts.map +1 -0
  121. package/dist/naming/rewrite.js +191 -0
  122. package/dist/naming/rewrite.js.map +1 -0
  123. package/dist/naming/scan.d.ts +80 -0
  124. package/dist/naming/scan.d.ts.map +1 -0
  125. package/dist/naming/scan.js +193 -0
  126. package/dist/naming/scan.js.map +1 -0
  127. package/dist/naming/table.d.ts +68 -0
  128. package/dist/naming/table.d.ts.map +1 -0
  129. package/dist/naming/table.js +132 -0
  130. package/dist/naming/table.js.map +1 -0
  131. package/dist/naming/types.d.ts +94 -0
  132. package/dist/naming/types.d.ts.map +1 -0
  133. package/dist/naming/types.js +2 -0
  134. package/dist/naming/types.js.map +1 -0
  135. package/dist/output/import-path.d.ts +24 -0
  136. package/dist/output/import-path.d.ts.map +1 -0
  137. package/dist/output/import-path.js +30 -0
  138. package/dist/output/import-path.js.map +1 -0
  139. package/dist/output/importPath.d.ts +27 -0
  140. package/dist/output/importPath.d.ts.map +1 -0
  141. package/dist/output/importPath.js +84 -0
  142. package/dist/output/importPath.js.map +1 -0
  143. package/dist/output/index.d.ts +40 -0
  144. package/dist/output/index.d.ts.map +1 -0
  145. package/dist/output/index.js +138 -0
  146. package/dist/output/index.js.map +1 -0
  147. package/dist/output/manifest.d.ts +17 -0
  148. package/dist/output/manifest.d.ts.map +1 -0
  149. package/dist/output/manifest.js +22 -0
  150. package/dist/output/manifest.js.map +1 -0
  151. package/dist/output/paths.d.ts +54 -0
  152. package/dist/output/paths.d.ts.map +1 -0
  153. package/dist/output/paths.js +183 -0
  154. package/dist/output/paths.js.map +1 -0
  155. package/dist/output/resolve.d.ts +100 -0
  156. package/dist/output/resolve.d.ts.map +1 -0
  157. package/dist/output/resolve.js +313 -0
  158. package/dist/output/resolve.js.map +1 -0
  159. package/dist/output/validate.d.ts +82 -0
  160. package/dist/output/validate.d.ts.map +1 -0
  161. package/dist/output/validate.js +128 -0
  162. package/dist/output/validate.js.map +1 -0
  163. package/dist/parser/cursor.d.ts +55 -0
  164. package/dist/parser/cursor.d.ts.map +1 -0
  165. package/dist/parser/cursor.js +120 -0
  166. package/dist/parser/cursor.js.map +1 -0
  167. package/dist/parser/declaration.d.ts +21 -0
  168. package/dist/parser/declaration.d.ts.map +1 -0
  169. package/dist/parser/declaration.js +201 -0
  170. package/dist/parser/declaration.js.map +1 -0
  171. package/dist/parser/diagnostics.d.ts +52 -0
  172. package/dist/parser/diagnostics.d.ts.map +1 -0
  173. package/dist/parser/diagnostics.js +55 -0
  174. package/dist/parser/diagnostics.js.map +1 -0
  175. package/dist/parser/group.d.ts +26 -0
  176. package/dist/parser/group.d.ts.map +1 -0
  177. package/dist/parser/group.js +219 -0
  178. package/dist/parser/group.js.map +1 -0
  179. package/dist/parser/index.d.ts +30 -0
  180. package/dist/parser/index.d.ts.map +1 -0
  181. package/dist/parser/index.js +48 -0
  182. package/dist/parser/index.js.map +1 -0
  183. package/dist/parser/modifier.d.ts +25 -0
  184. package/dist/parser/modifier.d.ts.map +1 -0
  185. package/dist/parser/modifier.js +41 -0
  186. package/dist/parser/modifier.js.map +1 -0
  187. package/dist/parser/recovery.d.ts +23 -0
  188. package/dist/parser/recovery.d.ts.map +1 -0
  189. package/dist/parser/recovery.js +80 -0
  190. package/dist/parser/recovery.js.map +1 -0
  191. package/dist/parser/rule.d.ts +39 -0
  192. package/dist/parser/rule.d.ts.map +1 -0
  193. package/dist/parser/rule.js +337 -0
  194. package/dist/parser/rule.js.map +1 -0
  195. package/dist/parser/statement.d.ts +21 -0
  196. package/dist/parser/statement.d.ts.map +1 -0
  197. package/dist/parser/statement.js +48 -0
  198. package/dist/parser/statement.js.map +1 -0
  199. package/dist/parser/statements.d.ts +33 -0
  200. package/dist/parser/statements.d.ts.map +1 -0
  201. package/dist/parser/statements.js +605 -0
  202. package/dist/parser/statements.js.map +1 -0
  203. package/dist/parser/value.d.ts +57 -0
  204. package/dist/parser/value.d.ts.map +1 -0
  205. package/dist/parser/value.js +226 -0
  206. package/dist/parser/value.js.map +1 -0
  207. package/dist/resolver/graph.d.ts +20 -0
  208. package/dist/resolver/graph.d.ts.map +1 -0
  209. package/dist/resolver/graph.js +382 -0
  210. package/dist/resolver/graph.js.map +1 -0
  211. package/dist/resolver/groups.d.ts +70 -0
  212. package/dist/resolver/groups.d.ts.map +1 -0
  213. package/dist/resolver/groups.js +143 -0
  214. package/dist/resolver/groups.js.map +1 -0
  215. package/dist/resolver/index.d.ts +46 -0
  216. package/dist/resolver/index.d.ts.map +1 -0
  217. package/dist/resolver/index.js +46 -0
  218. package/dist/resolver/index.js.map +1 -0
  219. package/dist/resolver/keyframes.d.ts +56 -0
  220. package/dist/resolver/keyframes.d.ts.map +1 -0
  221. package/dist/resolver/keyframes.js +102 -0
  222. package/dist/resolver/keyframes.js.map +1 -0
  223. package/dist/resolver/paths.d.ts +37 -0
  224. package/dist/resolver/paths.d.ts.map +1 -0
  225. package/dist/resolver/paths.js +135 -0
  226. package/dist/resolver/paths.js.map +1 -0
  227. package/dist/resolver/rules.d.ts +34 -0
  228. package/dist/resolver/rules.d.ts.map +1 -0
  229. package/dist/resolver/rules.js +52 -0
  230. package/dist/resolver/rules.js.map +1 -0
  231. package/dist/resolver/scopes.d.ts +44 -0
  232. package/dist/resolver/scopes.d.ts.map +1 -0
  233. package/dist/resolver/scopes.js +213 -0
  234. package/dist/resolver/scopes.js.map +1 -0
  235. package/dist/resolver/types.d.ts +238 -0
  236. package/dist/resolver/types.d.ts.map +1 -0
  237. package/dist/resolver/types.js +11 -0
  238. package/dist/resolver/types.js.map +1 -0
  239. package/dist/selectors/flatten.d.ts +95 -0
  240. package/dist/selectors/flatten.d.ts.map +1 -0
  241. package/dist/selectors/flatten.js +384 -0
  242. package/dist/selectors/flatten.js.map +1 -0
  243. package/dist/selectors/index.d.ts +15 -0
  244. package/dist/selectors/index.d.ts.map +1 -0
  245. package/dist/selectors/index.js +14 -0
  246. package/dist/selectors/index.js.map +1 -0
  247. package/dist/selectors/scan.d.ts +54 -0
  248. package/dist/selectors/scan.d.ts.map +1 -0
  249. package/dist/selectors/scan.js +191 -0
  250. package/dist/selectors/scan.js.map +1 -0
  251. package/dist/selectors/specificity.d.ts +28 -0
  252. package/dist/selectors/specificity.d.ts.map +1 -0
  253. package/dist/selectors/specificity.js +297 -0
  254. package/dist/selectors/specificity.js.map +1 -0
  255. package/dist/setup-list.d.ts +41 -0
  256. package/dist/setup-list.d.ts.map +1 -0
  257. package/dist/setup-list.js +122 -0
  258. package/dist/setup-list.js.map +1 -0
  259. package/dist/sourcemaps/index.d.ts +14 -0
  260. package/dist/sourcemaps/index.d.ts.map +1 -0
  261. package/dist/sourcemaps/index.js +13 -0
  262. package/dist/sourcemaps/index.js.map +1 -0
  263. package/dist/sourcemaps/mappings.d.ts +77 -0
  264. package/dist/sourcemaps/mappings.d.ts.map +1 -0
  265. package/dist/sourcemaps/mappings.js +124 -0
  266. package/dist/sourcemaps/mappings.js.map +1 -0
  267. package/dist/sourcemaps/vlq.d.ts +12 -0
  268. package/dist/sourcemaps/vlq.d.ts.map +1 -0
  269. package/dist/sourcemaps/vlq.js +29 -0
  270. package/dist/sourcemaps/vlq.js.map +1 -0
  271. package/dist/types/ast.d.ts +291 -0
  272. package/dist/types/ast.d.ts.map +1 -0
  273. package/dist/types/ast.js +2 -0
  274. package/dist/types/ast.js.map +1 -0
  275. package/dist/types/diagnostics.d.ts +55 -0
  276. package/dist/types/diagnostics.d.ts.map +1 -0
  277. package/dist/types/diagnostics.js +61 -0
  278. package/dist/types/diagnostics.js.map +1 -0
  279. package/dist/types/options.d.ts +274 -0
  280. package/dist/types/options.d.ts.map +1 -0
  281. package/dist/types/options.js +11 -0
  282. package/dist/types/options.js.map +1 -0
  283. package/dist/types/result.d.ts +112 -0
  284. package/dist/types/result.d.ts.map +1 -0
  285. package/dist/types/result.js +2 -0
  286. package/dist/types/result.js.map +1 -0
  287. package/dist/types/span.d.ts +19 -0
  288. package/dist/types/span.d.ts.map +1 -0
  289. package/dist/types/span.js +2 -0
  290. package/dist/types/span.js.map +1 -0
  291. package/dist/types/token.d.ts +68 -0
  292. package/dist/types/token.d.ts.map +1 -0
  293. package/dist/types/token.js +2 -0
  294. package/dist/types/token.js.map +1 -0
  295. package/dist/variables/ast.d.ts +56 -0
  296. package/dist/variables/ast.d.ts.map +1 -0
  297. package/dist/variables/ast.js +167 -0
  298. package/dist/variables/ast.js.map +1 -0
  299. package/dist/variables/expand.d.ts +109 -0
  300. package/dist/variables/expand.d.ts.map +1 -0
  301. package/dist/variables/expand.js +213 -0
  302. package/dist/variables/expand.js.map +1 -0
  303. package/dist/variables/index.d.ts +36 -0
  304. package/dist/variables/index.d.ts.map +1 -0
  305. package/dist/variables/index.js +33 -0
  306. package/dist/variables/index.js.map +1 -0
  307. package/dist/variables/naming.d.ts +39 -0
  308. package/dist/variables/naming.d.ts.map +1 -0
  309. package/dist/variables/naming.js +14 -0
  310. package/dist/variables/naming.js.map +1 -0
  311. package/dist/variables/runtime.d.ts +81 -0
  312. package/dist/variables/runtime.d.ts.map +1 -0
  313. package/dist/variables/runtime.js +228 -0
  314. package/dist/variables/runtime.js.map +1 -0
  315. package/dist/variables/substitute.d.ts +65 -0
  316. package/dist/variables/substitute.d.ts.map +1 -0
  317. package/dist/variables/substitute.js +73 -0
  318. package/dist/variables/substitute.js.map +1 -0
  319. package/dist/variables/walk.d.ts +37 -0
  320. package/dist/variables/walk.d.ts.map +1 -0
  321. package/dist/variables/walk.js +113 -0
  322. package/dist/variables/walk.js.map +1 -0
  323. package/package.json +35 -4
@@ -0,0 +1 @@
1
+ {"version":3,"file":"naming.js","sourceRoot":"","sources":["../../src/variables/naming.ts"],"names":[],"mappings":"AAgCA,sEAAsE;AACtE,MAAM,UAAU,eAAe,CAAC,UAAiC,EAAE;IACjE,OAAO,OAAO,CAAC,MAAM,EAAE,MAAM,IAAI,OAAO,CAAC,MAAM,IAAI,EAAE,CAAC;AACxD,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,IAAY,EAAE,MAAc;IAC9D,OAAO,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,MAAM,IAAI,IAAI,EAAE,CAAC;AAC7D,CAAC"}
@@ -0,0 +1,81 @@
1
+ import type { StylesheetIr } from "../ir/types.js";
2
+ import type { ModuleGraph } from "../resolver/index.js";
3
+ import type { Diagnostic } from "../types/diagnostics.js";
4
+ import type { VariableNamingOptions } from "./naming.js";
5
+ /**
6
+ * Runtime token emission (SPECIFICATION.md §6.2).
7
+ *
8
+ * `@runtime` declarations become native custom properties:
9
+ *
10
+ * ```css
11
+ * :root { --ui-primary: #2563eb; }
12
+ * @media (prefers-color-scheme: dark) { :root { --ui-primary: #60a5fa; } }
13
+ * ```
14
+ *
15
+ * Naming rule: `--<prefix>-<name>`, or `--<name>` when the effective prefix is
16
+ * empty (`naming.prefix` wins over `prefix`, matching §7.2). The prefix is
17
+ * authored configuration and is never sanitized here (P4/A09 owns CSS identifier
18
+ * validation).
19
+ *
20
+ * Emission ownership (§6.2): runtime tokens visible to the entry module — its
21
+ * own `@runtime` declarations, unqualified imports and any namespaced member
22
+ * referenced by the entry or by reachable token modules — are emitted in every
23
+ * consuming entrypoint and deduplicated by custom-property name.
24
+ *
25
+ * A duplicated post-prefix property name is **not** an error (ADR-0016). Native
26
+ * custom properties are live cascade values, so the definition that comes later
27
+ * in merge order wins: style-imported modules in canonical DFS `@use` order
28
+ * (dependencies before importers), then other reachable token modules in graph
29
+ * topological order, and the entry last. That makes an entry's `@runtime`
30
+ * definition override a style-imported module's same-named property, and a later
31
+ * style module override an earlier one. The rule is deliberate and mirrors the
32
+ * §5.4 "later wins is authored CSS" precedent; do not turn it back into an
33
+ * error. Cross-entrypoint collision reporting is P5 project work.
34
+ *
35
+ * Inline variants on runtime tokens become conditional `:root` override rules.
36
+ * Conditions are resolved with the same alias table and AND semantics as
37
+ * lowering (A05), so keys and query text are identical. Declarations are grouped
38
+ * by resolved query in first-appearance order: base declarations first, then one
39
+ * `:root` rule per condition. This preserves each property's authored order
40
+ * while letting the native custom-property cascade do the work — conditional
41
+ * *reference* splitting is deliberately not performed.
42
+ *
43
+ * References inside declaration values are rewritten to `var(--...)`; a compile
44
+ * reference that reaches this pass is expanded textually in its defining module
45
+ * (the pass is safe to call without `substitute`, and idempotent after it).
46
+ */
47
+ /** Options for {@link emitRuntimeTokens}. */
48
+ export interface RuntimeEmitOptions extends VariableNamingOptions {
49
+ /** Project-level media alias overrides, merged before file `@media-types` (ADR-0006). */
50
+ readonly media?: Readonly<Record<string, string>>;
51
+ /**
52
+ * Rule-bearing (`styles`) modules merged into this build, in emission order
53
+ * (ADR-0014). Their rule-level runtime references register custom properties
54
+ * too, because their declarations are emitted in this build even though their
55
+ * `$references` were authored in another file. They are also the first ranks in
56
+ * merge order, so the entry's `@runtime` definition overrides them (ADR-0016).
57
+ */
58
+ readonly ruleModules?: readonly string[];
59
+ }
60
+ /** Result of {@link emitRuntimeTokens}. */
61
+ export interface RuntimeEmitResult {
62
+ /** IR with `:root` rules prepended and runtime references rewritten. */
63
+ readonly ir: StylesheetIr;
64
+ /** CHIKX2001 value-expansion errors and CHIKX3001 condition errors, in walk order. */
65
+ readonly diagnostics: Diagnostic[];
66
+ }
67
+ /**
68
+ * Emit `@runtime` custom properties and rewrite runtime references in `ir`.
69
+ *
70
+ * Signature note (deviation from the task sketch): the input IR is required.
71
+ * Producing `:root` rules alone would leave declaration values untouched, so
72
+ * the prescribed `(graph, entryFile, options)` shape cannot satisfy
73
+ * "references become `var(--...)`" without re-reading the AST.
74
+ *
75
+ * Runtime rules are inserted after leading statement at-rules (`@charset`,
76
+ * `@import`, `@layer a, b;`) so emitted CSS stays valid, and before authored
77
+ * rules. The pass is pure, deterministic and never mutates the input IR; it
78
+ * returns the input instance when there is nothing to add.
79
+ */
80
+ export declare function emitRuntimeTokens(ir: StylesheetIr, graph: ModuleGraph, entryFile: string, options?: RuntimeEmitOptions): RuntimeEmitResult;
81
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../src/variables/runtime.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAKV,YAAY,EACb,MAAM,gBAAgB,CAAC;AAGxB,OAAO,KAAK,EAAE,WAAW,EAAe,MAAM,sBAAsB,CAAC;AAErE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAM1D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,6CAA6C;AAC7C,MAAM,WAAW,kBAAmB,SAAQ,qBAAqB;IAC/D,yFAAyF;IACzF,QAAQ,CAAC,KAAK,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAClD;;;;;;OAMG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC1C;AAED,2CAA2C;AAC3C,MAAM,WAAW,iBAAiB;IAChC,wEAAwE;IACxE,QAAQ,CAAC,EAAE,EAAE,YAAY,CAAC;IAC1B,sFAAsF;IACtF,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAC;CACpC;AAyED;;;;;;;;;;;;GAYG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,YAAY,EAChB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,kBAAuB,GAC/B,iBAAiB,CAgKnB"}
@@ -0,0 +1,228 @@
1
+ import { declarationIr, stylesheetIr } from "../ir/factories.js";
2
+ import { buildMediaAliasTable, resolveMediaCondition } from "../lowering/index.js";
3
+ import { declarationSegmentSpans, modifierAliasSpans } from "../lowering/spans.js";
4
+ import { astReferences, segmentValueTokens } from "./ast.js";
5
+ import { expandValue, resolveReference } from "./expand.js";
6
+ import { effectivePrefix, runtimePropertyName } from "./naming.js";
7
+ import { mapIrDeclarations, rootRule } from "./walk.js";
8
+ /**
9
+ * Canonical path list of the modules reachable from `entry` through `@use`.
10
+ *
11
+ * Entry first, then `ModuleGraph.order` (dependencies before importers, path
12
+ * ties) restricted to reachable modules, then any remaining reachable module
13
+ * (cycle members are absent from `order`) sorted by path. Deterministic for a
14
+ * given graph.
15
+ */
16
+ function reachableModules(graph, entry) {
17
+ const reachable = new Set();
18
+ const queue = [entry];
19
+ while (queue.length > 0) {
20
+ const file = queue.shift();
21
+ if (reachable.has(file))
22
+ continue;
23
+ const record = graph.modules.get(file);
24
+ if (record === undefined)
25
+ continue;
26
+ reachable.add(file);
27
+ for (const dependency of record.dependencies)
28
+ queue.push(dependency);
29
+ }
30
+ const ordered = [];
31
+ for (const file of graph.order) {
32
+ if (reachable.has(file))
33
+ ordered.push(file);
34
+ }
35
+ const seen = new Set(ordered);
36
+ const leftovers = [...reachable].filter((file) => !seen.has(file)).sort();
37
+ return [...ordered, ...leftovers];
38
+ }
39
+ /**
40
+ * Merge rank per module for runtime custom-property resolution (ADR-0016).
41
+ *
42
+ * Higher rank wins. Style-imported modules come first in canonical DFS `@use`
43
+ * post-order (the order their rules are emitted), then any other reachable
44
+ * runtime-token module in graph topological order, and the entry is always last.
45
+ * The ordering is a deterministic projection of the graph; ties cannot occur
46
+ * because every module is assigned at most one rank.
47
+ */
48
+ function moduleMergeRanks(graph, entry, ruleModules) {
49
+ const ranks = new Map();
50
+ let next = 0;
51
+ const assign = (file) => {
52
+ if (file === entry || ranks.has(file))
53
+ return;
54
+ ranks.set(file, next);
55
+ next += 1;
56
+ };
57
+ for (const file of ruleModules ?? [])
58
+ assign(file);
59
+ for (const file of reachableModules(graph, entry))
60
+ assign(file);
61
+ ranks.set(entry, next);
62
+ return ranks;
63
+ }
64
+ /**
65
+ * Emit `@runtime` custom properties and rewrite runtime references in `ir`.
66
+ *
67
+ * Signature note (deviation from the task sketch): the input IR is required.
68
+ * Producing `:root` rules alone would leave declaration values untouched, so
69
+ * the prescribed `(graph, entryFile, options)` shape cannot satisfy
70
+ * "references become `var(--...)`" without re-reading the AST.
71
+ *
72
+ * Runtime rules are inserted after leading statement at-rules (`@charset`,
73
+ * `@import`, `@layer a, b;`) so emitted CSS stays valid, and before authored
74
+ * rules. The pass is pure, deterministic and never mutates the input IR; it
75
+ * returns the input instance when there is nothing to add.
76
+ */
77
+ export function emitRuntimeTokens(ir, graph, entryFile, options = {}) {
78
+ const entry = graph.modules.get(entryFile);
79
+ const diagnostics = [];
80
+ if (entry === undefined)
81
+ return { ir, diagnostics };
82
+ const prefix = effectivePrefix(options);
83
+ const table = buildMediaAliasTable(entry.ast, options.media);
84
+ const ranks = moduleMergeRanks(graph, entryFile, options.ruleModules);
85
+ const emitted = [];
86
+ const byProperty = new Map();
87
+ const register = (token, span) => {
88
+ if (token.tokenType !== "runtime")
89
+ return;
90
+ const property = runtimePropertyName(token.name, prefix);
91
+ const rank = ranks.get(token.module) ?? -1;
92
+ const existing = byProperty.get(property);
93
+ if (existing === undefined) {
94
+ const slot = { property, token, span, rank };
95
+ byProperty.set(property, slot);
96
+ emitted.push(slot);
97
+ return;
98
+ }
99
+ // ADR-0016: native custom properties are live cascade values, so the later
100
+ // definition in merge order wins. Equal ranks are the same definition
101
+ // registered again (a reference and its declaration), so the first span is
102
+ // kept. This intentionally reports no diagnostic.
103
+ if (rank > existing.rank) {
104
+ existing.token = token;
105
+ existing.span = span;
106
+ existing.rank = rank;
107
+ }
108
+ };
109
+ // 1. Unqualified runtime surface of the entry itself: locals in authored
110
+ // order, then unqualified imports (the A07 scope shape).
111
+ for (const token of entry.runtimeScope.tokens.values())
112
+ register(token, token.span);
113
+ // 2. Runtime tokens actually consumed through a reference, either by the
114
+ // entry's rules or by any reachable token module.
115
+ const registerReferences = (file, includeRules) => {
116
+ const record = graph.modules.get(file);
117
+ if (record === undefined)
118
+ return;
119
+ for (const reference of astReferences(record.ast, { includeRules })) {
120
+ const resolution = resolveReference(reference.reference, file, graph, reference.span);
121
+ if (resolution.kind === "runtime")
122
+ register(resolution.token, reference.span);
123
+ }
124
+ };
125
+ registerReferences(entryFile, true);
126
+ for (const file of reachableModules(graph, entryFile)) {
127
+ if (file === entryFile)
128
+ continue;
129
+ registerReferences(file, false);
130
+ }
131
+ // Rule-merge imports (ADR-0014): the merged module's own rules are emitted in
132
+ // this build, so their runtime references need a custom property here.
133
+ for (const file of options.ruleModules ?? []) {
134
+ if (file === entryFile)
135
+ continue;
136
+ registerReferences(file, true);
137
+ }
138
+ const hooks = {
139
+ runtimeRef: (token) => {
140
+ register(token, token.span);
141
+ return `var(${runtimePropertyName(token.name, prefix)})`;
142
+ },
143
+ report: (diagnostic) => diagnostics.push(diagnostic),
144
+ };
145
+ // 3. Defensive value rewrite: after `substitute` there are no `$` references
146
+ // left, but a standalone call still resolves and rewrites them here.
147
+ const segments = segmentValueTokens(entry.ast);
148
+ const originFor = (declaration) => {
149
+ const tokens = segments.get(declaration.span.start);
150
+ return { module: entryFile, start: tokens?.[0]?.start ?? null, fallback: declaration.span };
151
+ };
152
+ const mapped = mapIrDeclarations(ir, (declaration) => {
153
+ if (!declaration.value.includes("$"))
154
+ return declaration;
155
+ const expanded = expandValue(declaration.value, originFor(declaration), graph, hooks);
156
+ if (expanded === null)
157
+ return null;
158
+ if (expanded === declaration.value)
159
+ return declaration;
160
+ return declarationIr(declaration.property, expanded, declaration.span, declaration.important);
161
+ });
162
+ // 4. Render each runtime token (the list can grow while values expand, so a
163
+ // plain index loop reaches a fixed point deterministically).
164
+ const baseDeclarations = [];
165
+ const groups = [];
166
+ const groupByQuery = new Map();
167
+ for (let index = 0; index < emitted.length; index++) {
168
+ const emittedToken = emitted[index];
169
+ if (emittedToken === undefined)
170
+ continue;
171
+ const token = emittedToken.token;
172
+ const declaration = token.declaration;
173
+ if (token.tokenType !== "runtime" || !("variants" in declaration))
174
+ continue;
175
+ const spans = declarationSegmentSpans(declaration);
176
+ const baseValue = expandValue(declaration.value, { module: token.module, start: declaration.tokens?.[0]?.start ?? null, fallback: spans.base }, graph, hooks);
177
+ if (baseValue === null)
178
+ continue;
179
+ baseDeclarations.push(declarationIr(emittedToken.property, baseValue, spans.base, declaration.important));
180
+ for (let variantIndex = 0; variantIndex < declaration.variants.length; variantIndex++) {
181
+ const variant = declaration.variants[variantIndex];
182
+ if (variant === undefined)
183
+ continue;
184
+ const span = spans.variants[variantIndex] ?? declaration.span;
185
+ const value = expandValue(variant.value, { module: token.module, start: variant.tokens?.[0]?.start ?? null, fallback: span }, graph, hooks);
186
+ if (value === null)
187
+ continue;
188
+ const resolution = resolveMediaCondition(modifierAliasSpans(variant), table, variant.span);
189
+ if (!resolution.ok) {
190
+ diagnostics.push(resolution.diagnostic);
191
+ continue;
192
+ }
193
+ const rewritten = declarationIr(emittedToken.property, value, span, variant.important);
194
+ let group = groupByQuery.get(resolution.condition.query);
195
+ if (group === undefined) {
196
+ group = { condition: resolution.condition, declarations: [] };
197
+ groupByQuery.set(resolution.condition.query, group);
198
+ groups.push(group);
199
+ }
200
+ group.declarations.push(rewritten);
201
+ }
202
+ }
203
+ const runtimeRules = [];
204
+ if (baseDeclarations.length > 0)
205
+ runtimeRules.push(rootRule(baseDeclarations, null));
206
+ for (const group of groups) {
207
+ if (group.declarations.length > 0)
208
+ runtimeRules.push(rootRule(group.declarations, group.condition));
209
+ }
210
+ if (runtimeRules.length === 0)
211
+ return { ir: mapped.ir, diagnostics };
212
+ // Keep `@charset`/`@import`/statement `@layer` first; CSS requires at-rule
213
+ // statements before qualified rules.
214
+ const nodes = [...mapped.ir.rules];
215
+ let insertAt = 0;
216
+ while (insertAt < nodes.length) {
217
+ const node = nodes[insertAt];
218
+ if (node !== undefined && !("declarations" in node) && node.statement === true)
219
+ insertAt++;
220
+ else
221
+ break;
222
+ }
223
+ return {
224
+ ir: stylesheetIr([...nodes.slice(0, insertAt), ...runtimeRules, ...nodes.slice(insertAt)]),
225
+ diagnostics,
226
+ };
227
+ }
228
+ //# sourceMappingURL=runtime.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/variables/runtime.ts"],"names":[],"mappings":"AAAA,O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ACZ,CAAC;AACJ,CAAC"}
@@ -0,0 +1,65 @@
1
+ import type { StylesheetIr } from "../ir/types.js";
2
+ import type { ModuleGraph } from "../resolver/index.js";
3
+ import type { Diagnostic } from "../types/diagnostics.js";
4
+ import type { VariableNamingOptions } from "./naming.js";
5
+ /**
6
+ * Compile-time variable substitution (SPECIFICATION.md §6.1).
7
+ *
8
+ * Pure post-lowering pass: for every {@link DeclarationIr} value that contains a
9
+ * `$`, the authored slice is re-scanned with the A03 lexer, each `$name` /
10
+ * `$name.member` reference is resolved against the scope of the module that
11
+ * **authored** the declaration, and compile-time values are spliced in
12
+ * textually. (Values without `$` are passed through untouched: a `dollar-ref`
13
+ * token always contains `$`.) Runtime references become
14
+ * `var(--<prefix>-<name>)` so `@runtime` tokens keep their native cascade
15
+ * (`emitRuntimeTokens` emits the custom-property rules).
16
+ *
17
+ * The authoring module is `declaration.span.file`, not the module being
18
+ * substituted: `@group.<name>` expansion splices another module's declarations
19
+ * at the use site with their own authored spans, so a group default's `$refs`
20
+ * resolve in the defining module while use-site declarations resolve in the
21
+ * consumer (ADR-0017 item 6). For ordinary, non-expanded declarations the two
22
+ * modules are the same.
23
+ *
24
+ * Policies, all deterministic:
25
+ *
26
+ * - An unresolvable reference (undefined token, unknown namespace or member, a
27
+ * bare namespace, a reference cycle or an over-deep chain) is CHIKX2001 at
28
+ * the reference span, and the **declaration segment** is omitted. A `$` never
29
+ * leaks into CSS.
30
+ * - A `@runtime` reference inside a group default is CHIKX2001 and the segment
31
+ * is omitted: owner decision (b) keeps defaults compile-time only, so they are
32
+ * never rewritten with the consuming build's prefix (ADR-0017 item 6).
33
+ * - The built-in `keyframes` namespace (ADR-0015) is not a variable:
34
+ * `$keyframes.<name>` expands to the authored name text of a visible
35
+ * `@keyframes` block.
36
+ * - A rule whose declarations are all omitted is dropped, and a native block
37
+ * whose children are all dropped is dropped too, so no empty shells that
38
+ * could change cascade or at-rule validity are emitted. Statement at-rules
39
+ * (`@import`, ...) are always kept.
40
+ * - Spans are unchanged. The IR carries no value-offset field, so the exact
41
+ * reference offset is recovered from the AST's parsed value tokens (ADR-0005,
42
+ * keyed by segment start); when an IR node has no authored counterpart the
43
+ * segment span is reported instead.
44
+ * - Only the value text changes; `property`, `important`, conditions, native
45
+ * contexts, selectors and node order are preserved byte for byte.
46
+ */
47
+ /** Result of {@link substitute}. */
48
+ export interface SubstituteResult {
49
+ /** Rewritten, re-frozen stylesheet IR (the input instance when nothing changed). */
50
+ readonly ir: StylesheetIr;
51
+ /** CHIKX2001 diagnostics in IR order, one per rejected reference. */
52
+ readonly diagnostics: Diagnostic[];
53
+ }
54
+ /**
55
+ * Substitute every compile-time variable reference in `ir`.
56
+ *
57
+ * @param ir Lowered IR of `entryFile` (parse → `buildModuleGraph` → `lower`).
58
+ * @param graph Module graph produced by A07; supplies scopes and token values.
59
+ * @param entryFile Canonical path of the module that owns `ir`, used for
60
+ * references whose authored module cannot be read from their span.
61
+ * @param options Runtime custom-property prefix used to render `var(...)`
62
+ * references. It must match the prefix passed to `emitRuntimeTokens`.
63
+ */
64
+ export declare function substitute(ir: StylesheetIr, graph: ModuleGraph, entryFile: string, options?: VariableNamingOptions): SubstituteResult;
65
+ //# sourceMappingURL=substitute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"substitute.d.ts","sourceRoot":"","sources":["../../src/variables/substitute.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AAO1D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAEH,oCAAoC;AACpC,MAAM,WAAW,gBAAgB;IAC/B,oFAAoF;IACpF,QAAQ,CAAC,EAAE,EAAE,YAAY,CAAC;IAC1B,qEAAqE;IACrE,QAAQ,CAAC,WAAW,EAAE,UAAU,EAAE,CAAC;CACpC;AAUD;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CACxB,EAAE,EAAE,YAAY,EAChB,KAAK,EAAE,WAAW,EAClB,SAAS,EAAE,MAAM,EACjB,OAAO,GAAE,qBAA0B,GAClC,gBAAgB,CA4DlB"}
@@ -0,0 +1,73 @@
1
+ import { declarationIr } from "../ir/factories.js";
2
+ import { error } from "../types/diagnostics.js";
3
+ import { groupDefaultSegments, segmentValueTokens } from "./ast.js";
4
+ import { expandValue } from "./expand.js";
5
+ import { effectivePrefix, runtimePropertyName } from "./naming.js";
6
+ import { mapIrDeclarations } from "./walk.js";
7
+ /**
8
+ * Substitute every compile-time variable reference in `ir`.
9
+ *
10
+ * @param ir Lowered IR of `entryFile` (parse → `buildModuleGraph` → `lower`).
11
+ * @param graph Module graph produced by A07; supplies scopes and token values.
12
+ * @param entryFile Canonical path of the module that owns `ir`, used for
13
+ * references whose authored module cannot be read from their span.
14
+ * @param options Runtime custom-property prefix used to render `var(...)`
15
+ * references. It must match the prefix passed to `emitRuntimeTokens`.
16
+ */
17
+ export function substitute(ir, graph, entryFile, options = {}) {
18
+ const prefix = effectivePrefix(options);
19
+ const diagnostics = [];
20
+ // Earlier group expansion can splice declarations authored in another module,
21
+ // so the token scope is selected per declaration from its authored span
22
+ // (ADR-0017 item 6). Indexes are built once per module and cached.
23
+ const origins = new Map();
24
+ const originsFor = (file) => {
25
+ const cached = origins.get(file);
26
+ if (cached !== undefined)
27
+ return cached;
28
+ const ast = graph.modules.get(file)?.ast;
29
+ const built = {
30
+ segments: segmentValueTokens(ast),
31
+ groupDefaults: groupDefaultSegments(ast),
32
+ };
33
+ origins.set(file, built);
34
+ return built;
35
+ };
36
+ const mapped = mapIrDeclarations(ir, (declaration) => {
37
+ const authored = declaration.span.file === "" ? entryFile : declaration.span.file;
38
+ const module = graph.modules.has(authored) ? authored : entryFile;
39
+ const index = originsFor(module);
40
+ // Expanded group defaults are the only IR declarations whose segment lives
41
+ // inside a `@group` definition; definitions never emit on their own.
42
+ const groupDefault = index.groupDefaults.has(declaration.span.start);
43
+ if (!declaration.value.includes("$"))
44
+ return declaration;
45
+ let rejectedRuntime = false;
46
+ const hooks = {
47
+ runtimeRef: (token, authoredText) => {
48
+ if (!groupDefault)
49
+ return `var(${runtimePropertyName(token.name, prefix)})`;
50
+ if (!rejectedRuntime) {
51
+ diagnostics.push(error("CHIKX2001", `A @runtime token is not allowed in a group default: "${authoredText}" (ADR-0017); reference runtime tokens at the use site.`, declaration.span));
52
+ }
53
+ rejectedRuntime = true;
54
+ return "";
55
+ },
56
+ report: (diagnostic) => diagnostics.push(diagnostic),
57
+ };
58
+ const tokens = index.segments.get(declaration.span.start);
59
+ const origin = {
60
+ module,
61
+ start: tokens?.[0]?.start ?? null,
62
+ fallback: declaration.span,
63
+ };
64
+ const expanded = expandValue(declaration.value, origin, graph, hooks);
65
+ if (rejectedRuntime || expanded === null)
66
+ return null;
67
+ if (expanded === declaration.value)
68
+ return declaration;
69
+ return declarationIr(declaration.property, expanded, declaration.span, declaration.important);
70
+ });
71
+ return { ir: mapped.ir, diagnostics };
72
+ }
73
+ //# sourceMappingURL=substitute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"substitute.js","sourceRoot":"","sources":["../../src/variables/substitute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAInD,OAAO,EAAE,KAAK,EAAE,MAAM,yBAAyB,CAAC;AAEhD,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAEnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC;AA6D9C;;;;;;;;;GASG;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,WAAW,CAAC,QAAQ,EAAE,QAAQ,EAAE,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAChG,CAAC,CAAC,CAAC;IAEH,OAAO,EAAE,EAAE,EAAE,MAAM,CAAC,EAAE,EAAE,WAAW,EAAE,CAAC;AACxC,CAAC"}
@@ -0,0 +1,37 @@
1
+ import type { DeclarationIr, RuleIr, StylesheetIr } from "../ir/types.js";
2
+ /**
3
+ * Shared IR rewriting for the variable passes.
4
+ *
5
+ * The IR is immutable and source-ordered, so both substitution and runtime
6
+ * emission rebuild it with frozen factories instead of editing nodes. Rewriting
7
+ * is structural: node identity is preserved when nothing changed, a rule or
8
+ * block at-rule is dropped only when **this pass** rejected its last
9
+ * declaration/child (an authored empty block such as `@layer a {}` is kept
10
+ * because it carries meaning), and statement at-rules (`@import`, `@charset`,
11
+ * `@layer a, b;`) are always kept because they carry no declarations (A05
12
+ * additive field).
13
+ */
14
+ /** Result of {@link mapIrDeclarations}. */
15
+ export interface MappedIr {
16
+ /** Rebuilt IR, or the input instance when nothing changed. */
17
+ readonly ir: StylesheetIr;
18
+ /** Whether at least one node or declaration was replaced or dropped. */
19
+ readonly changed: boolean;
20
+ }
21
+ /**
22
+ * Rewrite every {@link DeclarationIr} of a stylesheet.
23
+ *
24
+ * `transform` returns the declaration to keep or `null` to omit it. A rule or
25
+ * block at-rule is dropped when the last of its declarations/children was
26
+ * omitted by this pass, so a rejected reference cannot leave an empty selector
27
+ * behind; containers that were already empty on input are preserved untouched.
28
+ */
29
+ export declare function mapIrDeclarations(ir: StylesheetIr, transform: (declaration: DeclarationIr) => DeclarationIr | null): MappedIr;
30
+ /**
31
+ * Create the `:root` rule that carries runtime custom properties.
32
+ *
33
+ * The rule span is the first declaration's span, matching the lowering contract
34
+ * for runs; an empty list is defensive only and yields a zero-width span.
35
+ */
36
+ export declare function rootRule(declarations: readonly DeclarationIr[], condition: RuleIr["condition"]): RuleIr;
37
+ //# sourceMappingURL=walk.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.d.ts","sourceRoot":"","sources":["../../src/variables/walk.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,aAAa,EAGb,MAAM,EACN,YAAY,EACb,MAAM,gBAAgB,CAAC;AAExB;;;;;;;;;;;GAWG;AAEH,2CAA2C;AAC3C,MAAM,WAAW,QAAQ;IACvB,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,EAAE,YAAY,CAAC;IAC1B,wEAAwE;IACxE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAC/B,EAAE,EAAE,YAAY,EAChB,SAAS,EAAE,CAAC,WAAW,EAAE,aAAa,KAAK,aAAa,GAAG,IAAI,GAC9D,QAAQ,CAoFV;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CACtB,YAAY,EAAE,SAAS,aAAa,EAAE,EACtC,SAAS,EAAE,MAAM,CAAC,WAAW,CAAC,GAC7B,MAAM,CAUR"}
@@ -0,0 +1,113 @@
1
+ import { nativeBlockIr, ruleIr, stylesheetIr } from "../ir/factories.js";
2
+ /**
3
+ * Rewrite every {@link DeclarationIr} of a stylesheet.
4
+ *
5
+ * `transform` returns the declaration to keep or `null` to omit it. A rule or
6
+ * block at-rule is dropped when the last of its declarations/children was
7
+ * omitted by this pass, so a rejected reference cannot leave an empty selector
8
+ * behind; containers that were already empty on input are preserved untouched.
9
+ */
10
+ export function mapIrDeclarations(ir, transform) {
11
+ let changed = false;
12
+ const rewriteRule = (rule) => {
13
+ // An already-empty rule is authored input, not a rejection: preserve it so
14
+ // the pass never changes nodes it did not have to rewrite.
15
+ if (rule.declarations.length === 0)
16
+ return rule;
17
+ const declarations = [];
18
+ for (const declaration of rule.declarations) {
19
+ const rewritten = transform(declaration);
20
+ if (rewritten !== null)
21
+ declarations.push(rewritten);
22
+ }
23
+ if (declarations.length === 0) {
24
+ changed = true;
25
+ return null;
26
+ }
27
+ if (declarations.length === rule.declarations.length &&
28
+ declarations.every((entry, index) => entry === rule.declarations[index])) {
29
+ return rule;
30
+ }
31
+ changed = true;
32
+ return ruleIr({
33
+ selector: rule.selector,
34
+ ancestors: rule.ancestors,
35
+ declarations,
36
+ condition: rule.condition,
37
+ context: rule.context,
38
+ span: rule.span,
39
+ });
40
+ };
41
+ const rewriteBlock = (block) => {
42
+ if (block.statement === true)
43
+ return block;
44
+ // An authored empty block is preserved: `@layer a {}` still declares layer
45
+ // order and an empty `@media` is the author's structure, not our rejection.
46
+ if (block.children.length === 0)
47
+ return block;
48
+ const children = [];
49
+ for (const child of block.children) {
50
+ if ("declarations" in child) {
51
+ const rewritten = rewriteRule(child);
52
+ if (rewritten !== null)
53
+ children.push(rewritten);
54
+ continue;
55
+ }
56
+ if ("children" in child) {
57
+ const rewritten = rewriteBlock(child);
58
+ if (rewritten !== null)
59
+ children.push(rewritten);
60
+ continue;
61
+ }
62
+ const rewritten = transform(child);
63
+ if (rewritten !== null)
64
+ children.push(rewritten);
65
+ }
66
+ if (children.length === 0) {
67
+ changed = true;
68
+ return null;
69
+ }
70
+ if (children.length === block.children.length &&
71
+ children.every((entry, index) => entry === block.children[index])) {
72
+ return block;
73
+ }
74
+ changed = true;
75
+ return nativeBlockIr({
76
+ name: block.name,
77
+ prelude: block.prelude,
78
+ children,
79
+ span: block.span,
80
+ });
81
+ };
82
+ const rules = [];
83
+ for (const node of ir.rules) {
84
+ if ("declarations" in node) {
85
+ const rewritten = rewriteRule(node);
86
+ if (rewritten !== null)
87
+ rules.push(rewritten);
88
+ continue;
89
+ }
90
+ const rewritten = rewriteBlock(node);
91
+ if (rewritten !== null)
92
+ rules.push(rewritten);
93
+ }
94
+ return { ir: changed ? stylesheetIr(rules) : ir, changed };
95
+ }
96
+ /**
97
+ * Create the `:root` rule that carries runtime custom properties.
98
+ *
99
+ * The rule span is the first declaration's span, matching the lowering contract
100
+ * for runs; an empty list is defensive only and yields a zero-width span.
101
+ */
102
+ export function rootRule(declarations, condition) {
103
+ const first = declarations[0];
104
+ return ruleIr({
105
+ selector: ":root",
106
+ ancestors: [],
107
+ declarations,
108
+ condition,
109
+ context: [],
110
+ span: first?.span ?? { start: 0, end: 0, file: "" },
111
+ });
112
+ }
113
+ //# sourceMappingURL=walk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walk.js","sourceRoot":"","sources":["../../src/variables/walk.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AA8BzE;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAC/B,EAAgB,EAChB,SAA+D;IAE/D,IAAI,OAAO,GAAG,KAAK,CAAC;IAEpB,MAAM,WAAW,GAAG,CAAC,IAAY,EAAiB,EAAE;QAClD,2EAA2E;QAC3E,2DAA2D;QAC3D,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAChD,MAAM,YAAY,GAAoB,EAAE,CAAC;QACzC,KAAK,MAAM,WAAW,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YAC5C,MAAM,SAAS,GAAG,SAAS,CAAC,WAAW,CAAC,CAAC;YACzC,IAAI,SAAS,KAAK,IAAI;gBAAE,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACvD,CAAC;QACD,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,GAAG,IAAI,CAAC;YACf,OAAO,IAAI,CAAC;QACd,CAAC;QACD,IACE,YAAY,CAAC,MAAM,KAAK,IAAI,CAAC,YAAY,CAAC,MAAM;YAChD,YAAY,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,EACxE,CAAC;YACD,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,G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