@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
package/dist/index.js ADDED
@@ -0,0 +1,792 @@
1
+ /**
2
+ * `@chikx/compiler` public entry point.
3
+ *
4
+ * The frozen contracts of task A02 live in `./types/*`. `parse`, `compile`,
5
+ * `compileProject` and `planOutputs` are wired here; the compiler core remains
6
+ * pure and filesystem-free through an injected {@link ModuleHost}. `compile` is
7
+ * the batteries-included single-source entry point and uses the default Node
8
+ * host from `./host/node.js`.
9
+ */
10
+ import { emitCss } from "./emitter/index.js";
11
+ import { createNodeHost } from "./host/node.js";
12
+ import { stylesheetIr } from "./ir/factories.js";
13
+ import { tokenize } from "./lexer/index.js";
14
+ import { expandGroupUseSites, groupRuns, lower } from "./lowering/index.js";
15
+ import { generateClassMap, mergeClassTables, renameClasses } from "./naming/index.js";
16
+ import { stylesheetImportSpecifier } from "./output/import-path.js";
17
+ import { planOutputs } from "./output/index.js";
18
+ import { basenamePath, dirnamePath, isAbsolutePath, joinPath, normalizePath, relativePath, } from "./output/paths.js";
19
+ import { defaultSetupFor } from "./output/resolve.js";
20
+ import { parseTokens } from "./parser/index.js";
21
+ import { buildModuleGraph, styleModuleOrder } from "./resolver/index.js";
22
+ import { flattenIr } from "./selectors/index.js";
23
+ import { decodeSetupList, decodeSetupScalar } from "./setup-list.js";
24
+ import { error } from "./types/diagnostics.js";
25
+ import { emitRuntimeTokens, substitute } from "./variables/index.js";
26
+ export { DIAGNOSTIC_CODES, error, warning } from "./types/diagnostics.js";
27
+ /** Tokenize Chikx source into a lossless token stream plus lexical diagnostics. */
28
+ export { tokenize };
29
+ /** Default Node/Bun filesystem host used by {@link compile}. */
30
+ export { createNodeHost };
31
+ /** Validate and return a complete output plan without writing. */
32
+ export { planOutputs };
33
+ /** Decode a comma-separated `@setup` value list and its per-item diagnostics. */
34
+ export { decodeSetupList, decodeSetupScalar } from "./setup-list.js";
35
+ /** Canonical source formatting (pure parse→print; errors return the input). */
36
+ export { format } from "./formatter/index.js";
37
+ /** Every `@setup` key understood by `compile` and `compileProject`. */
38
+ const SETUP_KEYS = new Set([
39
+ "prefix",
40
+ "css-dir",
41
+ "css-name",
42
+ "map-dir",
43
+ "map-name",
44
+ "emit-css",
45
+ "emit-map",
46
+ "map-types",
47
+ "emit-types",
48
+ "source-map",
49
+ "minify",
50
+ "module",
51
+ "map-inherit",
52
+ "import-css",
53
+ "media-grouping",
54
+ ]);
55
+ const BOOLEAN_SETUP_KEYS = new Set([
56
+ "emit-css",
57
+ "emit-map",
58
+ "map-types",
59
+ "emit-types",
60
+ "source-map",
61
+ "minify",
62
+ "map-inherit",
63
+ "import-css",
64
+ ]);
65
+ /**
66
+ * Tolerantly parse Chikx source into an immutable AST plus diagnostics,
67
+ * recovering at safe statement boundaries for editor use.
68
+ *
69
+ * Pipeline: {@link tokenize} then the A04 `parseTokens` stage. Diagnostics are
70
+ * merged in a stable order — **lexical diagnostics first** (emitted while
71
+ * tokenizing), then syntactic diagnostics — because both use the CHIKX1xxx
72
+ * family and each stage already reports in source order. Neither stage throws,
73
+ * so a partial `Stylesheet` is always returned.
74
+ */
75
+ export function parse(source, options) {
76
+ const lexed = tokenize(source, options.file);
77
+ const parsed = parseTokens(lexed.tokens, options.file);
78
+ return Object.freeze({
79
+ ast: parsed.ast,
80
+ diagnostics: Object.freeze([...lexed.diagnostics, ...parsed.diagnostics]),
81
+ });
82
+ }
83
+ /**
84
+ * Compile one Chikx source string to CSS, JS class map, declarations and/or a
85
+ * source map, returning artifacts and diagnostics without writing files.
86
+ *
87
+ * The pipeline is `parse` → optional module graph → (`expandGroupUseSites` →
88
+ * `lower` → `substitute` → `renameClasses` per style-imported module) →
89
+ * `expandGroupUseSites` → `lower` → `substitute` → `renameClasses` for the
90
+ * entry → `mergeClassTables` → `emitRuntimeTokens` → `groupRuns` → `flattenIr`
91
+ * → `emitCss` → `planOutputs` → `generateClassMap`.
92
+ * Rule-bearing `styles` imports (ADR-0014) contribute their rules first, in
93
+ * depth-first `@use` order, each renamed with the module's own `@setup` prefix;
94
+ * the importing build then has one merged class map and declaration file. A
95
+ * module that authors `@setup { map-inherit: false; }` (ADR-0020) still merges
96
+ * its rules but keeps its bindings out of the importer's map and declarations.
97
+ * A file that authors `@setup { import-css: true; }` (ADR-0021) gets one
98
+ * side-effect import of its planned stylesheet at the top of the JS map; without
99
+ * it (the default) the map stays import-free for plain Node.
100
+ * File `@setup` values are decoded here, then explicit `CompileOptions` output
101
+ * fields override them (API/CLI precedence). Prefix precedence is
102
+ * `options.prefix` → `options.naming.prefix` → file `@setup prefix` → `""`;
103
+ * strategy `none` disables prefixing. `@setup { module: true }` suppresses this
104
+ * file's own CSS, map, types and CSS import unless those keys are authored.
105
+ *
106
+ * `options.workspaceRoot` is the containment root and the Node host's root. It
107
+ * defaults to `process.cwd()` at call time inside {@link createNodeHost}. When
108
+ * the source has no `@use`, a memory-only single-file graph is built so runtime
109
+ * token and variable passes still see the entry; no filesystem is read for the
110
+ * source itself.
111
+ */
112
+ export async function compile(source, options) {
113
+ const parsed = parse(source, { file: options.file });
114
+ const nodeHost = createNodeHost(options.workspaceRoot);
115
+ const workspaceRoot = normalizePath(nodeHost.workspaceRoot());
116
+ const entryAbsolute = normalizePath(isAbsolutePath(options.file) ? options.file : joinPath(workspaceRoot, options.file));
117
+ const canonicalEntry = normalizePath(nodeHost.canonicalize(entryAbsolute));
118
+ const decoded = decodeSetup(parsed.ast);
119
+ const hasUse = parsed.ast.items.some((item) => item.kind === "use");
120
+ let graph;
121
+ let ast = parsed.ast;
122
+ let entryKey = canonicalEntry;
123
+ let baseDiagnostics;
124
+ let sourceTexts;
125
+ if (hasUse) {
126
+ const recorded = createRecordingHost(withEntrySource(nodeHost, entryAbsolute, source));
127
+ sourceTexts = recorded.sources;
128
+ graph = await buildModuleGraph([entryAbsolute], recorded.host, options);
129
+ const record = graph.modules.get(canonicalEntry);
130
+ if (record !== undefined)
131
+ ast = record.ast;
132
+ baseDiagnostics =
133
+ record === undefined
134
+ ? [...parsed.diagnostics, ...graph.diagnostics]
135
+ : diagnosticsForEntry(graph, canonicalEntry);
136
+ }
137
+ else {
138
+ const recorded = createRecordingHost(createMemoryHost(source, entryAbsolute, workspaceRoot));
139
+ sourceTexts = recorded.sources;
140
+ graph = await buildModuleGraph([entryAbsolute], recorded.host, options);
141
+ entryKey = entryAbsolute;
142
+ const record = graph.modules.get(entryKey);
143
+ if (record !== undefined)
144
+ ast = record.ast;
145
+ baseDiagnostics = singleFileDiagnostics(parsed.diagnostics, diagnosticsForEntry(graph, entryKey));
146
+ }
147
+ const setup = buildCompileSetup(options.file, options, decoded);
148
+ const sourceFile = sourcePathFor(entryAbsolute, workspaceRoot, options.file);
149
+ const passOptions = { ...options, file: sourceFile };
150
+ return compileEntry({
151
+ ast,
152
+ entryKey,
153
+ sourceFile,
154
+ graph,
155
+ setup,
156
+ prefix: setup.prefix,
157
+ options: passOptions,
158
+ mediaGrouping: decoded.mediaGrouping ?? options.mediaGrouping ?? "safe",
159
+ workspaceRoot,
160
+ host: nodeHost,
161
+ sourceTexts,
162
+ baseDiagnostics,
163
+ setupDiagnostics: decoded.diagnostics,
164
+ ...(options.mapInherit === undefined ? {} : { moduleMapInherit: options.mapInherit }),
165
+ });
166
+ }
167
+ /**
168
+ * Resolve modules for several entries through an injected host and compile each
169
+ * entry with one shared module graph.
170
+ *
171
+ * Project-level `options.output` and `options.naming` form the project layer;
172
+ * file `@setup` values override them. `options.configFile`/`projectRoot` are
173
+ * reserved for the P5 project loader and are not read here. The returned record
174
+ * is keyed by the entry strings exactly as supplied.
175
+ */
176
+ export async function compileProject(entries, host, options) {
177
+ const workspaceRoot = normalizePath(options.projectRoot ?? host.workspaceRoot());
178
+ const projectHost = options.projectRoot === undefined
179
+ ? host
180
+ : {
181
+ readFile: (path) => host.readFile(path),
182
+ fileExists: (path) => host.fileExists(path),
183
+ canonicalize: (path) => host.canonicalize(path),
184
+ resolvePath: (from, to) => host.resolvePath(from, to),
185
+ workspaceRoot: () => workspaceRoot,
186
+ };
187
+ const absoluteEntries = entries.map((entry) => normalizePath(isAbsolutePath(entry) ? entry : joinPath(workspaceRoot, entry)));
188
+ const graphOptions = {
189
+ file: absoluteEntries[0] ?? "",
190
+ ...(options.media === undefined ? {} : { media: options.media }),
191
+ };
192
+ const recorded = createRecordingHost(projectHost);
193
+ const graph = await buildModuleGraph(absoluteEntries, recorded.host, graphOptions);
194
+ const projectConfig = projectDirectoryConfig(options.output);
195
+ const results = {};
196
+ for (let index = 0; index < entries.length; index += 1) {
197
+ const entry = entries[index] ?? "";
198
+ const absolute = absoluteEntries[index] ?? entry;
199
+ const canonical = normalizePath(projectHost.canonicalize(absolute));
200
+ const record = graph.modules.get(canonical);
201
+ const sourceFile = sourcePathFor(absolute, workspaceRoot, entry);
202
+ if (record === undefined) {
203
+ results[entry] = emptyCompileResult(graph.diagnostics.filter((diagnostic) => normalizePath(diagnostic.span.file) === canonical));
204
+ continue;
205
+ }
206
+ const decoded = decodeSetup(record.ast);
207
+ const setup = buildProjectSetup(sourceFile, options, decoded);
208
+ const passOptions = { ...options, file: sourceFile };
209
+ results[entry] = compileEntry({
210
+ ast: record.ast,
211
+ entryKey: canonical,
212
+ sourceFile,
213
+ graph,
214
+ setup,
215
+ ...(projectConfig === undefined ? {} : { projectConfig }),
216
+ prefix: setup.prefix,
217
+ options: passOptions,
218
+ mediaGrouping: decoded.mediaGrouping ?? options.mediaGrouping ?? "safe",
219
+ workspaceRoot,
220
+ host: projectHost,
221
+ sourceTexts: recorded.sources,
222
+ baseDiagnostics: diagnosticsForEntry(graph, canonical),
223
+ setupDiagnostics: decoded.diagnostics,
224
+ ...(options.output?.mapInherit === undefined
225
+ ? {}
226
+ : { moduleMapInherit: options.output.mapInherit }),
227
+ });
228
+ }
229
+ return results;
230
+ }
231
+ /** No visible groups: used when the entry module record is unavailable. */
232
+ const NO_GROUPS = new Map();
233
+ /** Run every pure pass for one already-loaded entry and return frozen artifacts. */
234
+ function compileEntry(input) {
235
+ const styleModules = styleModuleOrder(input.graph, input.entryKey);
236
+ const moduleDiagnostics = [];
237
+ const moduleIr = [];
238
+ const tables = [];
239
+ // Rule-merge imports (ADR-0014): each module is expanded, lowered,
240
+ // substituted and renamed on its own, with its own setup prefix. Imported
241
+ // modules come first in the IR (depth-first in authored `@use` order), then
242
+ // the entry's rules.
243
+ for (const file of styleModules) {
244
+ const record = input.graph.modules.get(file);
245
+ if (record === undefined)
246
+ continue;
247
+ const decoded = decodeSetup(record.ast);
248
+ const prefix = modulePrefix(input.options, decoded);
249
+ const expanded = expandGroupUseSites(record.ast, { module: file, groups: record.groups });
250
+ const lowered = lower(expanded.ast, input.options);
251
+ const substituted = substitute(lowered.ir, input.graph, file, { prefix: input.prefix });
252
+ const renamed = renameClasses(substituted.ir, { prefix });
253
+ moduleDiagnostics.push(...decoded.diagnostics, ...expanded.diagnostics, ...lowered.diagnostics, ...substituted.diagnostics, ...renamed.diagnostics);
254
+ moduleIr.push(renamed.ir);
255
+ tables.push({
256
+ table: renamed.classes,
257
+ label: sourcePathFor(file, input.workspaceRoot, file),
258
+ mapInherit: decoded.values.mapInherit ?? input.moduleMapInherit ?? true,
259
+ });
260
+ }
261
+ const entryGroups = input.graph.modules.get(input.entryKey)?.groups ?? NO_GROUPS;
262
+ const expandedEntry = expandGroupUseSites(input.ast, {
263
+ module: input.entryKey,
264
+ groups: entryGroups,
265
+ });
266
+ const lowered = lower(expandedEntry.ast, input.options);
267
+ const substituted = substitute(lowered.ir, input.graph, input.entryKey, {
268
+ prefix: input.prefix,
269
+ });
270
+ const renamed = renameClasses(substituted.ir, { prefix: input.prefix });
271
+ tables.push({ table: renamed.classes, label: input.sourceFile });
272
+ const merged = mergeClassTables(tables);
273
+ const combinedIr = stylesheetIr([...moduleIr.flatMap((ir) => ir.rules), ...renamed.ir.rules]);
274
+ const runtime = emitRuntimeTokens(combinedIr, input.graph, input.entryKey, {
275
+ prefix: input.prefix,
276
+ ...(input.options.media === undefined ? {} : { media: input.options.media }),
277
+ ...(styleModules.length === 0 ? {} : { ruleModules: styleModules }),
278
+ });
279
+ const grouped = groupRuns(runtime.ir, input.mediaGrouping);
280
+ const flattened = flattenIr(grouped);
281
+ const planInput = {
282
+ sourceFile: input.sourceFile,
283
+ setup: input.setup,
284
+ workspaceRoot: input.workspaceRoot,
285
+ ...(input.projectConfig === undefined ? {} : { projectConfig: input.projectConfig }),
286
+ };
287
+ const plan = planOutputs(planInput, {
288
+ host: { canonicalize: (path) => input.host.canonicalize(path) },
289
+ });
290
+ const cssOutput = plan.plan.outputs.find((output) => output.kind === "css");
291
+ const emitted = emitCss(flattened.ir, {
292
+ minify: input.setup.minify,
293
+ ...(input.setup.sourceMap && cssOutput !== undefined
294
+ ? {
295
+ sourceMap: {
296
+ // The comment and `file` field must name the planned CSS basename.
297
+ file: basenamePath(cssOutput.workspacePath),
298
+ sourceText: (file) => input.sourceTexts.get(normalizePath(file)),
299
+ sourcePath: (file) => mapSourcePath(file, input.workspaceRoot, input.host),
300
+ includeSourcesContent: true,
301
+ },
302
+ }
303
+ : {}),
304
+ });
305
+ const map = input.setup.emitMap === true ? generateClassMap(merged.table, classMapOptions()) : undefined;
306
+ /** Opt-in CSS side-effect import for the class map (ADR-0021). */
307
+ function classMapOptions() {
308
+ if (input.setup.importCss !== true)
309
+ return {};
310
+ const jsOutput = plan.plan.outputs.find((output) => output.kind === "js");
311
+ // Authored destinations keep their order, so the first `css` plan entry is
312
+ // the canonical artifact for a `css-dir` list (ADR-0019, ADR-0021).
313
+ const cssOutput = plan.plan.outputs.find((output) => output.kind === "css");
314
+ if (jsOutput === undefined || cssOutput === undefined)
315
+ return {};
316
+ return {
317
+ cssImport: stylesheetImportSpecifier(jsOutput.absolutePath, cssOutput.absolutePath),
318
+ };
319
+ }
320
+ const diagnostics = mergeDiagnostics(input.setupDiagnostics, input.baseDiagnostics, moduleDiagnostics, expandedEntry.diagnostics, lowered.diagnostics, substituted.diagnostics, runtime.diagnostics, renamed.diagnostics, merged.diagnostics, flattened.diagnostics, emitted.diagnostics, map === undefined ? [] : map.diagnostics, plan.diagnostics);
321
+ return Object.freeze({
322
+ ...(input.setup.emitCss ? { css: emitted.css } : {}),
323
+ ...(emitted.map === undefined ? {} : { sourceMap: emitted.map }),
324
+ ...(map === undefined ? {} : { js: map.js }),
325
+ ...(map === undefined || !input.setup.emitTypes ? {} : { types: map.types }),
326
+ classes: classMapOf(merged.table.entries),
327
+ diagnostics: Object.freeze(diagnostics),
328
+ dependencies: Object.freeze(dependenciesForEntry(input.graph, input.entryKey, input.workspaceRoot)),
329
+ });
330
+ }
331
+ /** Decode every `@setup` block in document order, rejecting unknown/duplicate keys. */
332
+ function decodeSetup(ast) {
333
+ const values = {};
334
+ const diagnostics = [];
335
+ const seen = new Set();
336
+ let moduleOnly = false;
337
+ let mediaGrouping;
338
+ // `map-types` is the canonical declaration flag; `emit-types` is its legacy
339
+ // alias. Resolve them together so the canonical key wins regardless of order.
340
+ let mapTypes;
341
+ let emitTypes;
342
+ for (const item of ast.items) {
343
+ if (item.kind !== "setup")
344
+ continue;
345
+ for (const setting of item.settings) {
346
+ if (!SETUP_KEYS.has(setting.key)) {
347
+ diagnostics.push(error("CHIKX1001", `Unknown @setup setting "${setting.key}".`, setting.span));
348
+ continue;
349
+ }
350
+ if (seen.has(setting.key)) {
351
+ diagnostics.push(error("CHIKX1001", `Duplicate @setup setting "${setting.key}".`, setting.span));
352
+ continue;
353
+ }
354
+ seen.add(setting.key);
355
+ // `module` is a marker, not a resolved output setting: it only removes
356
+ // defaults for this file's own build (ADR-0014).
357
+ if (setting.key === "module") {
358
+ const moduleValue = decodeBoolean(setting);
359
+ if (moduleValue === undefined) {
360
+ diagnostics.push(error("CHIKX1001", `@setup setting "module" expects true or false, not ${setting.value.trim()}.`, setting.span));
361
+ continue;
362
+ }
363
+ moduleOnly = moduleValue;
364
+ continue;
365
+ }
366
+ if (setting.key === "media-grouping") {
367
+ const value = decodeSetupScalar(setting.value);
368
+ if (value === "merge" || value === "safe" || value === "none") {
369
+ mediaGrouping = value;
370
+ }
371
+ else {
372
+ diagnostics.push(error("CHIKX1001", `@setup setting "media-grouping" expects "merge", "safe" or "none", not ${setting.value.trim()}.`, setting.valueSpan));
373
+ }
374
+ continue;
375
+ }
376
+ if (BOOLEAN_SETUP_KEYS.has(setting.key)) {
377
+ const value = decodeBoolean(setting);
378
+ if (value === undefined) {
379
+ diagnostics.push(error("CHIKX1001", `@setup setting "${setting.key}" expects true or false, not ${setting.value.trim()}.`, setting.span));
380
+ continue;
381
+ }
382
+ if (setting.key === "map-types") {
383
+ mapTypes = value;
384
+ continue;
385
+ }
386
+ if (setting.key === "emit-types") {
387
+ emitTypes = value;
388
+ continue;
389
+ }
390
+ assignBooleanSetting(values, setting.key, value);
391
+ }
392
+ else if (setting.key === "css-dir") {
393
+ const decoded = decodeSetupList(setting.value, setting.valueSpan);
394
+ diagnostics.push(...decoded.diagnostics);
395
+ if (decoded.diagnostics.length > 0)
396
+ continue;
397
+ const first = decoded.entries[0];
398
+ if (first === undefined)
399
+ continue;
400
+ values.cssDir = first.value;
401
+ if (decoded.entries.length > 1) {
402
+ values.cssDirs = decoded.entries.map((entry) => entry.value);
403
+ }
404
+ }
405
+ else {
406
+ assignStringSetting(values, setting.key, decodeSetupScalar(setting.value));
407
+ }
408
+ }
409
+ }
410
+ const types = mapTypes ?? emitTypes;
411
+ if (types !== undefined)
412
+ values.emitTypes = types;
413
+ return {
414
+ values,
415
+ moduleOnly,
416
+ ...(mediaGrouping === undefined ? {} : { mediaGrouping }),
417
+ diagnostics,
418
+ };
419
+ }
420
+ function decodeBoolean(setting) {
421
+ const raw = setting.value.trim();
422
+ if (raw === "true")
423
+ return true;
424
+ if (raw === "false")
425
+ return false;
426
+ return undefined;
427
+ }
428
+ function assignStringSetting(target, key, value) {
429
+ switch (key) {
430
+ case "prefix":
431
+ target.prefix = value;
432
+ break;
433
+ case "css-name":
434
+ target.cssName = value;
435
+ break;
436
+ case "map-dir":
437
+ target.mapDir = value;
438
+ break;
439
+ case "map-name":
440
+ target.mapName = value;
441
+ break;
442
+ default:
443
+ break;
444
+ }
445
+ }
446
+ function assignBooleanSetting(target, key, value) {
447
+ switch (key) {
448
+ case "emit-css":
449
+ target.emitCss = value;
450
+ break;
451
+ case "emit-map":
452
+ target.emitMap = value;
453
+ break;
454
+ case "map-types":
455
+ case "emit-types":
456
+ target.emitTypes = value;
457
+ break;
458
+ case "source-map":
459
+ target.sourceMap = value;
460
+ break;
461
+ case "minify":
462
+ target.minify = value;
463
+ break;
464
+ case "map-inherit":
465
+ target.mapInherit = value;
466
+ break;
467
+ case "import-css":
468
+ target.importCss = value;
469
+ break;
470
+ default:
471
+ break;
472
+ }
473
+ }
474
+ function definedOutput(options) {
475
+ if (options === undefined)
476
+ return {};
477
+ const values = {};
478
+ if (options.cssDir !== undefined)
479
+ values.cssDir = options.cssDir;
480
+ if (options.cssName !== undefined)
481
+ values.cssName = options.cssName;
482
+ if (options.mapDir !== undefined)
483
+ values.mapDir = options.mapDir;
484
+ if (options.mapName !== undefined)
485
+ values.mapName = options.mapName;
486
+ if (options.emitCss !== undefined)
487
+ values.emitCss = options.emitCss;
488
+ if (options.emitMap !== undefined)
489
+ values.emitMap = options.emitMap;
490
+ const types = options.mapTypes ?? options.emitTypes;
491
+ if (types !== undefined)
492
+ values.emitTypes = types;
493
+ if (options.sourceMap !== undefined)
494
+ values.sourceMap = options.sourceMap;
495
+ if (options.minify !== undefined)
496
+ values.minify = options.minify;
497
+ if (options.mapInherit !== undefined)
498
+ values.mapInherit = options.mapInherit;
499
+ if (options.importCss !== undefined)
500
+ values.importCss = options.importCss;
501
+ return values;
502
+ }
503
+ function buildCompileSetup(file, options, decoded) {
504
+ const defaults = defaultSetupFor(file);
505
+ const merged = {
506
+ ...defaults,
507
+ ...moduleSuppression(decoded.moduleOnly),
508
+ ...decoded.values,
509
+ ...definedOutput(options),
510
+ prefix: "",
511
+ };
512
+ // An explicit API/CLI `cssDir` is a single destination and replaces a file
513
+ // list (ADR-0019); only the file layer can author a list.
514
+ if (options.cssDir !== undefined)
515
+ merged.cssDirs = undefined;
516
+ return {
517
+ ...merged,
518
+ prefix: resolveCompilePrefix(options, decoded),
519
+ };
520
+ }
521
+ function buildProjectSetup(file, options, decoded) {
522
+ const defaults = defaultSetupFor(file);
523
+ const merged = {
524
+ ...defaults,
525
+ ...definedOutput(options.output),
526
+ ...moduleSuppression(decoded.moduleOnly),
527
+ ...decoded.values,
528
+ prefix: "",
529
+ };
530
+ if (options.output?.cssDir !== undefined)
531
+ merged.cssDirs = undefined;
532
+ return {
533
+ ...merged,
534
+ prefix: resolveProjectPrefix(options, decoded),
535
+ };
536
+ }
537
+ /**
538
+ * Output defaults removed by `@setup { module: true }` (ADR-0014).
539
+ *
540
+ * A module-only file writes no artifacts of its own, so CSS, map and types are
541
+ * suppressed. The suppression sits above built-in defaults and project
542
+ * configuration but below the file's own explicit `emit-*` keys and explicit
543
+ * API/CLI overrides, so authoring a key restores it.
544
+ */
545
+ function moduleSuppression(moduleOnly) {
546
+ if (!moduleOnly)
547
+ return {};
548
+ return { emitCss: false, emitMap: false, emitTypes: false };
549
+ }
550
+ function resolveCompilePrefix(options, decoded) {
551
+ if ((options.naming?.strategy ?? "prefix") === "none")
552
+ return "";
553
+ return options.prefix ?? options.naming?.prefix ?? decoded.values.prefix ?? "";
554
+ }
555
+ function resolveProjectPrefix(options, decoded) {
556
+ if ((options.naming?.strategy ?? "prefix") === "none")
557
+ return "";
558
+ return decoded.values.prefix ?? options.naming?.prefix ?? "";
559
+ }
560
+ /**
561
+ * Effective class prefix of a style-imported module (ADR-0014).
562
+ *
563
+ * Only the module's own `@setup prefix` applies: the importing file's prefix,
564
+ * the API `prefix` and the project `classes.prefix` never reach merged rules. A
565
+ * module without `@setup` therefore merges unprefixed. `naming.strategy: "none"`
566
+ * still disables prefixing for the whole build.
567
+ */
568
+ function modulePrefix(options, decoded) {
569
+ if ((options.naming?.strategy ?? "prefix") === "none")
570
+ return "";
571
+ return decoded.values.prefix ?? "";
572
+ }
573
+ /** Build the null-prototype class map required by the frozen `ClassMap` type. */
574
+ function classMapOf(entries) {
575
+ const map = Object.create(null);
576
+ for (const entry of entries) {
577
+ if (!entry.global)
578
+ map[entry.name] = entry.emitted;
579
+ }
580
+ return Object.freeze(map);
581
+ }
582
+ /** Workspace-relative POSIX dependencies of one entry, excluding the entry itself. */
583
+ function dependenciesForEntry(graph, entryKey, workspaceRoot) {
584
+ const reachable = new Set();
585
+ const queue = [entryKey];
586
+ while (queue.length > 0) {
587
+ const file = queue.shift();
588
+ if (reachable.has(file))
589
+ continue;
590
+ reachable.add(file);
591
+ const record = graph.modules.get(file);
592
+ if (record === undefined)
593
+ continue;
594
+ for (const dependency of record.dependencies)
595
+ queue.push(dependency);
596
+ }
597
+ reachable.delete(entryKey);
598
+ const relative = [...reachable].map((file) => relativePath(workspaceRoot, file));
599
+ return relative
600
+ .filter((path) => !isAbsolutePath(path) && path !== ".." && !path.startsWith("../"))
601
+ .sort();
602
+ }
603
+ /**
604
+ * Caller-facing diagnostics for a source compiled without `@use`.
605
+ *
606
+ * The memory graph reparses the same source, so its syntax (`CHIKX1xxx`)
607
+ * diagnostics duplicate `parsed.diagnostics`; the caller's parse keeps the
608
+ * authored file label and stays authoritative. Resolver diagnostics
609
+ * (`CHIKX2xxx`+) exist only in the graph and must survive, so a duplicate
610
+ * `@variables` key reports one `CHIKX2001` with the resolver's message and span
611
+ * just as the module-graph path does (CSSX-U04).
612
+ */
613
+ function singleFileDiagnostics(parsed, graphDiagnostics) {
614
+ const resolverDiagnostics = graphDiagnostics.filter((diagnostic) => !diagnostic.code.startsWith("CHIKX1"));
615
+ return mergeDiagnostics(parsed, resolverDiagnostics);
616
+ }
617
+ /** Graph diagnostics whose file belongs to the entry's reachable module set. */
618
+ function diagnosticsForEntry(graph, entryKey) {
619
+ const reachable = new Set();
620
+ const queue = [entryKey];
621
+ while (queue.length > 0) {
622
+ const file = queue.shift();
623
+ if (reachable.has(file))
624
+ continue;
625
+ reachable.add(file);
626
+ const record = graph.modules.get(file);
627
+ if (record === undefined)
628
+ continue;
629
+ for (const dependency of record.dependencies)
630
+ queue.push(dependency);
631
+ }
632
+ return graph.diagnostics.filter((diagnostic) => reachable.has(normalizePath(diagnostic.span.file)));
633
+ }
634
+ function projectDirectoryConfig(output) {
635
+ if (output === undefined)
636
+ return undefined;
637
+ const directories = {
638
+ ...(output.cssDir === undefined ? {} : { cssDir: output.cssDir }),
639
+ ...(output.mapDir === undefined ? {} : { mapDir: output.mapDir }),
640
+ };
641
+ if (directories.cssDir === undefined && directories.mapDir === undefined)
642
+ return undefined;
643
+ return { output: directories };
644
+ }
645
+ function emptyCompileResult(diagnostics = []) {
646
+ const classes = Object.freeze(Object.create(null));
647
+ return Object.freeze({
648
+ classes,
649
+ diagnostics: Object.freeze([...diagnostics]),
650
+ dependencies: Object.freeze([]),
651
+ });
652
+ }
653
+ /** Resolve an authored source path to a workspace-relative POSIX path when possible. */
654
+ function sourcePathFor(absolute, workspaceRoot, authored) {
655
+ const relative = relativePath(workspaceRoot, absolute);
656
+ if (relative === "." ||
657
+ isAbsolutePath(relative) ||
658
+ relative === ".." ||
659
+ relative.startsWith("../")) {
660
+ return normalizePath(authored);
661
+ }
662
+ return relative;
663
+ }
664
+ /** Project a span file path into the workspace-relative POSIX form maps require. */
665
+ function mapSourcePath(file, workspaceRoot, host) {
666
+ const roots = new Set([workspaceRoot]);
667
+ try {
668
+ roots.add(normalizePath(host.canonicalize(workspaceRoot)));
669
+ }
670
+ catch {
671
+ // A host that cannot canonicalize the root keeps the lexical form only.
672
+ }
673
+ for (const root of roots) {
674
+ const relative = relativePath(root, file);
675
+ if (relative !== "." &&
676
+ !isAbsolutePath(relative) &&
677
+ relative !== ".." &&
678
+ !relative.startsWith("../")) {
679
+ return relative;
680
+ }
681
+ }
682
+ return normalizePath(file);
683
+ }
684
+ /**
685
+ * Wrap a host so every authored source read by the module graph is retained for
686
+ * source-map generation.
687
+ *
688
+ * `ModuleRecord` intentionally stores no source text (A07 contract), so the
689
+ * index pipeline records it here without changing frozen or resolver types.
690
+ * Keys are normalized module paths, exactly the paths that end up in IR spans.
691
+ */
692
+ function createRecordingHost(host) {
693
+ const sources = new Map();
694
+ return {
695
+ sources,
696
+ host: {
697
+ async readFile(path) {
698
+ const source = await host.readFile(path);
699
+ sources.set(normalizePath(path), source);
700
+ return source;
701
+ },
702
+ fileExists: (path) => host.fileExists(path),
703
+ canonicalize: (path) => host.canonicalize(path),
704
+ resolvePath: (from, to) => host.resolvePath(from, to),
705
+ workspaceRoot: () => host.workspaceRoot(),
706
+ },
707
+ };
708
+ }
709
+ /**
710
+ * Memory host for a source with no `@use`: it makes the parsed entry available
711
+ * to the graph passes without reading the filesystem.
712
+ */
713
+ function createMemoryHost(source, entry, workspaceRoot) {
714
+ const normalizedEntry = normalizePath(entry);
715
+ const same = (path) => normalizePath(path) === normalizedEntry;
716
+ return {
717
+ async readFile(path) {
718
+ if (same(path))
719
+ return source;
720
+ throw new Error(`ENOENT: ${path}`);
721
+ },
722
+ async fileExists(path) {
723
+ return same(path);
724
+ },
725
+ canonicalize(path) {
726
+ return normalizePath(path);
727
+ },
728
+ resolvePath(from, to) {
729
+ return isAbsolutePath(to) ? normalizePath(to) : joinPath(dirnamePath(from), to);
730
+ },
731
+ workspaceRoot() {
732
+ return workspaceRoot;
733
+ },
734
+ };
735
+ }
736
+ /**
737
+ * Wrap the default host so the in-memory source overrides the entry file while
738
+ * imports still resolve through the real Node host.
739
+ */
740
+ function withEntrySource(host, entry, source) {
741
+ const normalizedEntry = normalizePath(entry);
742
+ const canonicalEntry = normalizePath(host.canonicalize(normalizedEntry));
743
+ const same = (path) => {
744
+ const normalized = normalizePath(path);
745
+ return normalized === normalizedEntry || normalized === canonicalEntry;
746
+ };
747
+ return {
748
+ async readFile(path) {
749
+ if (same(path))
750
+ return source;
751
+ return host.readFile(path);
752
+ },
753
+ async fileExists(path) {
754
+ if (same(path))
755
+ return true;
756
+ return host.fileExists(path);
757
+ },
758
+ canonicalize(path) {
759
+ if (same(path))
760
+ return canonicalEntry;
761
+ return host.canonicalize(path);
762
+ },
763
+ resolvePath(from, to) {
764
+ return host.resolvePath(from, to);
765
+ },
766
+ workspaceRoot() {
767
+ return host.workspaceRoot();
768
+ },
769
+ };
770
+ }
771
+ function mergeDiagnostics(...groups) {
772
+ const merged = [];
773
+ const seen = new Set();
774
+ for (const group of groups) {
775
+ for (const diagnostic of group) {
776
+ const key = [
777
+ diagnostic.code,
778
+ diagnostic.severity,
779
+ diagnostic.span.file,
780
+ diagnostic.span.start,
781
+ diagnostic.span.end,
782
+ diagnostic.message,
783
+ ].join("\u0000");
784
+ if (seen.has(key))
785
+ continue;
786
+ seen.add(key);
787
+ merged.push(diagnostic);
788
+ }
789
+ }
790
+ return merged;
791
+ }
792
+ //# sourceMappingURL=index.js.map