@chikx/compiler 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (398) 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/index.d.ts +40 -0
  140. package/dist/output/index.d.ts.map +1 -0
  141. package/dist/output/index.js +138 -0
  142. package/dist/output/index.js.map +1 -0
  143. package/dist/output/manifest.d.ts +17 -0
  144. package/dist/output/manifest.d.ts.map +1 -0
  145. package/dist/output/manifest.js +22 -0
  146. package/dist/output/manifest.js.map +1 -0
  147. package/dist/output/paths.d.ts +54 -0
  148. package/dist/output/paths.d.ts.map +1 -0
  149. package/dist/output/paths.js +183 -0
  150. package/dist/output/paths.js.map +1 -0
  151. package/dist/output/resolve.d.ts +100 -0
  152. package/dist/output/resolve.d.ts.map +1 -0
  153. package/dist/output/resolve.js +313 -0
  154. package/dist/output/resolve.js.map +1 -0
  155. package/dist/output/validate.d.ts +82 -0
  156. package/dist/output/validate.d.ts.map +1 -0
  157. package/dist/output/validate.js +128 -0
  158. package/dist/output/validate.js.map +1 -0
  159. package/dist/parser/cursor.d.ts +55 -0
  160. package/dist/parser/cursor.d.ts.map +1 -0
  161. package/dist/parser/cursor.js +120 -0
  162. package/dist/parser/cursor.js.map +1 -0
  163. package/dist/parser/declaration.d.ts +21 -0
  164. package/dist/parser/declaration.d.ts.map +1 -0
  165. package/dist/parser/declaration.js +201 -0
  166. package/dist/parser/declaration.js.map +1 -0
  167. package/dist/parser/diagnostics.d.ts +52 -0
  168. package/dist/parser/diagnostics.d.ts.map +1 -0
  169. package/dist/parser/diagnostics.js +55 -0
  170. package/dist/parser/diagnostics.js.map +1 -0
  171. package/dist/parser/group.d.ts +26 -0
  172. package/dist/parser/group.d.ts.map +1 -0
  173. package/dist/parser/group.js +219 -0
  174. package/dist/parser/group.js.map +1 -0
  175. package/dist/parser/index.d.ts +30 -0
  176. package/dist/parser/index.d.ts.map +1 -0
  177. package/dist/parser/index.js +48 -0
  178. package/dist/parser/index.js.map +1 -0
  179. package/dist/parser/modifier.d.ts +25 -0
  180. package/dist/parser/modifier.d.ts.map +1 -0
  181. package/dist/parser/modifier.js +41 -0
  182. package/dist/parser/modifier.js.map +1 -0
  183. package/dist/parser/recovery.d.ts +23 -0
  184. package/dist/parser/recovery.d.ts.map +1 -0
  185. package/dist/parser/recovery.js +80 -0
  186. package/dist/parser/recovery.js.map +1 -0
  187. package/dist/parser/rule.d.ts +39 -0
  188. package/dist/parser/rule.d.ts.map +1 -0
  189. package/dist/parser/rule.js +337 -0
  190. package/dist/parser/rule.js.map +1 -0
  191. package/dist/parser/statement.d.ts +21 -0
  192. package/dist/parser/statement.d.ts.map +1 -0
  193. package/dist/parser/statement.js +48 -0
  194. package/dist/parser/statement.js.map +1 -0
  195. package/dist/parser/statements.d.ts +33 -0
  196. package/dist/parser/statements.d.ts.map +1 -0
  197. package/dist/parser/statements.js +605 -0
  198. package/dist/parser/statements.js.map +1 -0
  199. package/dist/parser/value.d.ts +57 -0
  200. package/dist/parser/value.d.ts.map +1 -0
  201. package/dist/parser/value.js +226 -0
  202. package/dist/parser/value.js.map +1 -0
  203. package/dist/resolver/graph.d.ts +20 -0
  204. package/dist/resolver/graph.d.ts.map +1 -0
  205. package/dist/resolver/graph.js +382 -0
  206. package/dist/resolver/graph.js.map +1 -0
  207. package/dist/resolver/groups.d.ts +70 -0
  208. package/dist/resolver/groups.d.ts.map +1 -0
  209. package/dist/resolver/groups.js +143 -0
  210. package/dist/resolver/groups.js.map +1 -0
  211. package/dist/resolver/index.d.ts +46 -0
  212. package/dist/resolver/index.d.ts.map +1 -0
  213. package/dist/resolver/index.js +46 -0
  214. package/dist/resolver/index.js.map +1 -0
  215. package/dist/resolver/keyframes.d.ts +56 -0
  216. package/dist/resolver/keyframes.d.ts.map +1 -0
  217. package/dist/resolver/keyframes.js +102 -0
  218. package/dist/resolver/keyframes.js.map +1 -0
  219. package/dist/resolver/paths.d.ts +37 -0
  220. package/dist/resolver/paths.d.ts.map +1 -0
  221. package/dist/resolver/paths.js +135 -0
  222. package/dist/resolver/paths.js.map +1 -0
  223. package/dist/resolver/rules.d.ts +34 -0
  224. package/dist/resolver/rules.d.ts.map +1 -0
  225. package/dist/resolver/rules.js +52 -0
  226. package/dist/resolver/rules.js.map +1 -0
  227. package/dist/resolver/scopes.d.ts +44 -0
  228. package/dist/resolver/scopes.d.ts.map +1 -0
  229. package/dist/resolver/scopes.js +213 -0
  230. package/dist/resolver/scopes.js.map +1 -0
  231. package/dist/resolver/types.d.ts +238 -0
  232. package/dist/resolver/types.d.ts.map +1 -0
  233. package/dist/resolver/types.js +11 -0
  234. package/dist/resolver/types.js.map +1 -0
  235. package/dist/selectors/flatten.d.ts +95 -0
  236. package/dist/selectors/flatten.d.ts.map +1 -0
  237. package/dist/selectors/flatten.js +384 -0
  238. package/dist/selectors/flatten.js.map +1 -0
  239. package/dist/selectors/index.d.ts +15 -0
  240. package/dist/selectors/index.d.ts.map +1 -0
  241. package/dist/selectors/index.js +14 -0
  242. package/dist/selectors/index.js.map +1 -0
  243. package/dist/selectors/scan.d.ts +54 -0
  244. package/dist/selectors/scan.d.ts.map +1 -0
  245. package/dist/selectors/scan.js +191 -0
  246. package/dist/selectors/scan.js.map +1 -0
  247. package/dist/selectors/specificity.d.ts +28 -0
  248. package/dist/selectors/specificity.d.ts.map +1 -0
  249. package/dist/selectors/specificity.js +297 -0
  250. package/dist/selectors/specificity.js.map +1 -0
  251. package/dist/setup-list.d.ts +41 -0
  252. package/dist/setup-list.d.ts.map +1 -0
  253. package/dist/setup-list.js +122 -0
  254. package/dist/setup-list.js.map +1 -0
  255. package/dist/sourcemaps/index.d.ts +14 -0
  256. package/dist/sourcemaps/index.d.ts.map +1 -0
  257. package/dist/sourcemaps/index.js +13 -0
  258. package/dist/sourcemaps/index.js.map +1 -0
  259. package/dist/sourcemaps/mappings.d.ts +77 -0
  260. package/dist/sourcemaps/mappings.d.ts.map +1 -0
  261. package/dist/sourcemaps/mappings.js +124 -0
  262. package/dist/sourcemaps/mappings.js.map +1 -0
  263. package/dist/sourcemaps/vlq.d.ts +12 -0
  264. package/dist/sourcemaps/vlq.d.ts.map +1 -0
  265. package/dist/sourcemaps/vlq.js +29 -0
  266. package/dist/sourcemaps/vlq.js.map +1 -0
  267. package/dist/types/ast.d.ts +291 -0
  268. package/dist/types/ast.d.ts.map +1 -0
  269. package/dist/types/ast.js +2 -0
  270. package/dist/types/ast.js.map +1 -0
  271. package/dist/types/diagnostics.d.ts +55 -0
  272. package/dist/types/diagnostics.d.ts.map +1 -0
  273. package/dist/types/diagnostics.js +61 -0
  274. package/dist/types/diagnostics.js.map +1 -0
  275. package/dist/types/options.d.ts +274 -0
  276. package/dist/types/options.d.ts.map +1 -0
  277. package/dist/types/options.js +11 -0
  278. package/dist/types/options.js.map +1 -0
  279. package/dist/types/result.d.ts +112 -0
  280. package/dist/types/result.d.ts.map +1 -0
  281. package/dist/types/result.js +2 -0
  282. package/dist/types/result.js.map +1 -0
  283. package/dist/types/span.d.ts +19 -0
  284. package/dist/types/span.d.ts.map +1 -0
  285. package/dist/types/span.js +2 -0
  286. package/dist/types/span.js.map +1 -0
  287. package/dist/types/token.d.ts +68 -0
  288. package/dist/types/token.d.ts.map +1 -0
  289. package/dist/types/token.js +2 -0
  290. package/dist/types/token.js.map +1 -0
  291. package/dist/variables/ast.d.ts +56 -0
  292. package/dist/variables/ast.d.ts.map +1 -0
  293. package/dist/variables/ast.js +167 -0
  294. package/dist/variables/ast.js.map +1 -0
  295. package/dist/variables/expand.d.ts +109 -0
  296. package/dist/variables/expand.d.ts.map +1 -0
  297. package/dist/variables/expand.js +213 -0
  298. package/dist/variables/expand.js.map +1 -0
  299. package/dist/variables/index.d.ts +36 -0
  300. package/dist/variables/index.d.ts.map +1 -0
  301. package/dist/variables/index.js +33 -0
  302. package/dist/variables/index.js.map +1 -0
  303. package/dist/variables/naming.d.ts +39 -0
  304. package/dist/variables/naming.d.ts.map +1 -0
  305. package/dist/variables/naming.js +14 -0
  306. package/dist/variables/naming.js.map +1 -0
  307. package/dist/variables/runtime.d.ts +81 -0
  308. package/dist/variables/runtime.d.ts.map +1 -0
  309. package/dist/variables/runtime.js +228 -0
  310. package/dist/variables/runtime.js.map +1 -0
  311. package/dist/variables/substitute.d.ts +65 -0
  312. package/dist/variables/substitute.d.ts.map +1 -0
  313. package/dist/variables/substitute.js +73 -0
  314. package/dist/variables/substitute.js.map +1 -0
  315. package/dist/variables/walk.d.ts +37 -0
  316. package/dist/variables/walk.d.ts.map +1 -0
  317. package/dist/variables/walk.js +113 -0
  318. package/dist/variables/walk.js.map +1 -0
  319. package/package.json +35 -4
  320. package/src/emitter/index.ts +658 -0
  321. package/src/formatter/index.ts +544 -0
  322. package/src/host/node.ts +85 -0
  323. package/src/index.ts +1021 -0
  324. package/src/ir/factories.ts +85 -0
  325. package/src/ir/index.ts +28 -0
  326. package/src/ir/types.ts +145 -0
  327. package/src/lexer/at-keywords.ts +35 -0
  328. package/src/lexer/characters.ts +82 -0
  329. package/src/lexer/comments.ts +27 -0
  330. package/src/lexer/diagnostics.ts +42 -0
  331. package/src/lexer/dollar-refs.ts +23 -0
  332. package/src/lexer/index.ts +37 -0
  333. package/src/lexer/names.ts +49 -0
  334. package/src/lexer/numbers.ts +59 -0
  335. package/src/lexer/scanner.ts +199 -0
  336. package/src/lexer/strings.ts +52 -0
  337. package/src/lexer/urls.ts +63 -0
  338. package/src/lowering/expand-groups.ts +206 -0
  339. package/src/lowering/group.ts +119 -0
  340. package/src/lowering/index.ts +43 -0
  341. package/src/lowering/lower.ts +441 -0
  342. package/src/lowering/media.ts +314 -0
  343. package/src/lowering/merge.ts +163 -0
  344. package/src/lowering/property-families.ts +194 -0
  345. package/src/lowering/spans.ts +118 -0
  346. package/src/naming/index.ts +44 -0
  347. package/src/naming/mapgen.ts +252 -0
  348. package/src/naming/merge.ts +114 -0
  349. package/src/naming/rewrite.ts +333 -0
  350. package/src/naming/scan.ts +267 -0
  351. package/src/naming/table.ts +153 -0
  352. package/src/naming/types.ts +100 -0
  353. package/src/output/import-path.ts +30 -0
  354. package/src/output/index.ts +213 -0
  355. package/src/output/manifest.ts +28 -0
  356. package/src/output/paths.ts +200 -0
  357. package/src/output/resolve.ts +401 -0
  358. package/src/output/validate.ts +251 -0
  359. package/src/parser/cursor.ts +138 -0
  360. package/src/parser/declaration.ts +247 -0
  361. package/src/parser/diagnostics.ts +75 -0
  362. package/src/parser/group.ts +303 -0
  363. package/src/parser/index.ts +50 -0
  364. package/src/parser/modifier.ts +60 -0
  365. package/src/parser/recovery.ts +88 -0
  366. package/src/parser/rule.ts +370 -0
  367. package/src/parser/statement.ts +47 -0
  368. package/src/parser/statements.ts +667 -0
  369. package/src/parser/value.ts +265 -0
  370. package/src/resolver/graph.ts +467 -0
  371. package/src/resolver/groups.ts +177 -0
  372. package/src/resolver/index.ts +68 -0
  373. package/src/resolver/keyframes.ts +131 -0
  374. package/src/resolver/paths.ts +142 -0
  375. package/src/resolver/rules.ts +54 -0
  376. package/src/resolver/scopes.ts +342 -0
  377. package/src/resolver/types.ts +259 -0
  378. package/src/selectors/flatten.ts +448 -0
  379. package/src/selectors/index.ts +14 -0
  380. package/src/selectors/scan.ts +227 -0
  381. package/src/selectors/specificity.ts +319 -0
  382. package/src/setup-list.ts +172 -0
  383. package/src/sourcemaps/index.ts +25 -0
  384. package/src/sourcemaps/mappings.ts +190 -0
  385. package/src/sourcemaps/vlq.ts +29 -0
  386. package/src/types/ast.ts +318 -0
  387. package/src/types/diagnostics.ts +124 -0
  388. package/src/types/options.ts +286 -0
  389. package/src/types/result.ts +121 -0
  390. package/src/types/span.ts +18 -0
  391. package/src/types/token.ts +77 -0
  392. package/src/variables/ast.ts +182 -0
  393. package/src/variables/expand.ts +319 -0
  394. package/src/variables/index.ts +38 -0
  395. package/src/variables/naming.ts +46 -0
  396. package/src/variables/runtime.ts +336 -0
  397. package/src/variables/substitute.ts +148 -0
  398. package/src/variables/walk.ts +147 -0
@@ -0,0 +1,333 @@
1
+ import { nativeBlockIr, ruleIr, stylesheetIr } from "../ir/factories.js";
2
+ import type { NativeBlockChild, NativeBlockIr, RuleIr, StylesheetIr } from "../ir/types.js";
3
+ import { flattenSelector } from "../selectors/index.js";
4
+ import { scanSelector } from "../selectors/scan.js";
5
+ import type { Diagnostic } from "../types/diagnostics.js";
6
+ import { error } from "../types/diagnostics.js";
7
+ import type { Span } from "../types/span.js";
8
+ import { applySelectorEdits, scanSelectorClasses } from "./scan.js";
9
+ import type { SelectorEdit } from "./scan.js";
10
+ import { isValidCssIdentifier, renameClassName } from "./table.js";
11
+ import type { ClassEntry, ClassTable, RenameOptions, RenameResult } from "./types.js";
12
+
13
+ /**
14
+ * Class rewrite: flattened selectors in, prefixed selectors and a class table
15
+ * out (SPECIFICATION.md §7.2, ADR-0010).
16
+ *
17
+ * The pass runs after variable substitution and runtime emission and before
18
+ * `groupRuns`. It is pure, filesystem-free and deterministic, and it never
19
+ * mutates its input: rules are rebuilt with the frozen IR factories.
20
+ *
21
+ * Policy:
22
+ *
23
+ * - Every local class token in a flattened selector is looked up in one table,
24
+ * so a class used at top level and as a nested reference is renamed once:
25
+ * `.button { & > .icon { } }` with prefix `ui` emits `.ui-button > .ui-icon`.
26
+ * - `.` + identifier is the only rewritten construct. Ids, type selectors,
27
+ * attribute selectors, attribute values, strings, comments, pseudo names and
28
+ * pseudo-element names are copied byte for byte. Class tokens inside
29
+ * functional pseudos (`:is(.a)`, `:not(.a)`) are real class selectors and are
30
+ * renamed; the conservative boundary is "every class token except inside
31
+ * `:global(...)`, strings, comments and attribute values".
32
+ * - `:global(.external-icon)` escapes renaming and the `:global(...)` wrapper is
33
+ * **removed** from the emitted selector (§7.2); the inner class keeps its
34
+ * authored text. A global class is recorded in the table with `global: true`
35
+ * and is never exported by `generateClassMap` (§8). MVP unwraps only a simple
36
+ * single-class argument; a complex argument (comma list, combinator, several
37
+ * classes) stays wrapped and unrenamed so the selector's meaning cannot change.
38
+ * - Escaped class identifiers are decoded for their identity (`.\31 23` is
39
+ * class `123`) and, when a prefix applies, re-emitted as the valid bare
40
+ * identifier `ui-123`. When no prefix applies the authored bytes are kept,
41
+ * so escape style is never normalized by accident.
42
+ * - Naming table (MVP strategies): `prefix` with a non-empty, valid prefix
43
+ * produces `"<prefix>-<name>"`; `none` and an empty prefix leave names
44
+ * unchanged. An invalid prefix or an invalid prefixed result is CHIKX4001 and
45
+ * the class stays unprefixed (documented fallback). Only the prefix failure is
46
+ * reported once; a per-class result failure is reported once for that class.
47
+ *
48
+ * Selector flattening is idempotent, so running it here is equivalent to
49
+ * running A06's `flattenIr` afterwards; a selector that cannot flatten is left
50
+ * in its authored nested form (classes are still rewritten) and the emitter
51
+ * reports the same CHIKX1001 it would have reported anyway. Class diagnostics
52
+ * use the rule span because `RuleIr` records declaration-origin spans, not
53
+ * selector offsets.
54
+ */
55
+
56
+ interface Collector {
57
+ readonly entries: ClassEntry[];
58
+ readonly indexes: Map<string, number>;
59
+ }
60
+
61
+ function computeEmitted(
62
+ name: string,
63
+ prefix: string,
64
+ strategy: "none" | "prefix",
65
+ span: Span,
66
+ diagnostics: Diagnostic[],
67
+ reported: Set<string>,
68
+ ): { readonly emitted: string; readonly renamed: boolean } {
69
+ const result = renameClassName(name, prefix, strategy);
70
+ if (!result.renamed) {
71
+ const applies = strategy === "prefix" && prefix !== "" && isValidCssIdentifier(prefix);
72
+ if (applies && !reported.has(name)) {
73
+ reported.add(name);
74
+ diagnostics.push(
75
+ error(
76
+ "CHIKX4001",
77
+ `Class "${name}" cannot be prefixed with "${prefix}": the result "${prefix}-${name}" is not a valid CSS identifier (SPECIFICATION.md §7.2). The class stays unprefixed.`,
78
+ span,
79
+ ),
80
+ );
81
+ }
82
+ }
83
+ return result;
84
+ }
85
+
86
+ function record(
87
+ collector: Collector,
88
+ token: { readonly name: string; readonly global: boolean },
89
+ span: Span,
90
+ prefix: string,
91
+ strategy: "none" | "prefix",
92
+ diagnostics: Diagnostic[],
93
+ reported: Set<string>,
94
+ ): ClassEntry {
95
+ const existingIndex = collector.indexes.get(token.name);
96
+ if (existingIndex !== undefined) {
97
+ const existing = collector.entries[existingIndex];
98
+ if (existing === undefined) {
99
+ throw new Error("class table index out of range");
100
+ }
101
+ if (existing.global && !token.global) {
102
+ const computed = computeEmitted(token.name, prefix, strategy, span, diagnostics, reported);
103
+ const upgraded: ClassEntry = Object.freeze({
104
+ name: token.name,
105
+ emitted: computed.emitted,
106
+ global: false,
107
+ renamed: computed.renamed,
108
+ span: existing.span,
109
+ });
110
+ collector.entries[existingIndex] = upgraded;
111
+ return upgraded;
112
+ }
113
+ return existing;
114
+ }
115
+
116
+ const computed = token.global
117
+ ? { emitted: token.name, renamed: false }
118
+ : computeEmitted(token.name, prefix, strategy, span, diagnostics, reported);
119
+ const entry: ClassEntry = Object.freeze({
120
+ name: token.name,
121
+ emitted: computed.emitted,
122
+ global: token.global,
123
+ renamed: computed.renamed,
124
+ span: Object.freeze(span),
125
+ });
126
+ collector.indexes.set(token.name, collector.entries.length);
127
+ collector.entries.push(entry);
128
+ return entry;
129
+ }
130
+
131
+ /** Rewrite one flat selector text and record its classes. */
132
+ function rewriteFlatSelector(
133
+ text: string,
134
+ span: Span,
135
+ collector: Collector,
136
+ prefix: string,
137
+ strategy: "none" | "prefix",
138
+ diagnostics: Diagnostic[],
139
+ reported: Set<string>,
140
+ ): string {
141
+ if (!scanSelector(text).ok) return text;
142
+ const scan = scanSelectorClasses(text);
143
+ if (!scan.ok) return text;
144
+
145
+ const edits: SelectorEdit[] = [];
146
+ for (const global of scan.globals) {
147
+ const inner = text.slice(global.openEnd, global.closeStart).trim();
148
+ const contained = scan.classes.filter(
149
+ (token) => token.start >= global.openEnd && token.end <= global.closeStart,
150
+ );
151
+ // MVP supports a simple global class escape only (SPECIFICATION.md §7.2).
152
+ // A complex argument (comma list, combinator, several classes) is left
153
+ // wrapped and unrenamed rather than expanded into a selector whose meaning
154
+ // would change; its classes are still recorded as global.
155
+ const simple = contained.length === 1 && contained[0]?.raw === inner;
156
+ if (!simple) continue;
157
+ edits.push({ start: global.start, end: global.openEnd, text: "" });
158
+ edits.push({ start: global.closeStart, end: global.end, text: "" });
159
+ }
160
+
161
+ for (const token of scan.classes) {
162
+ const entry = record(collector, token, span, prefix, strategy, diagnostics, reported);
163
+ if (!token.global && entry.renamed) {
164
+ edits.push({ start: token.start, end: token.end, text: `.${entry.emitted}` });
165
+ }
166
+ }
167
+
168
+ return applySelectorEdits(text, edits);
169
+ }
170
+
171
+ function rewriteRule(
172
+ rule: RuleIr,
173
+ collector: Collector,
174
+ prefix: string,
175
+ strategy: "none" | "prefix",
176
+ diagnostics: Diagnostic[],
177
+ reported: Set<string>,
178
+ ): RuleIr {
179
+ const flattened = flattenSelector(rule.selector, rule.ancestors);
180
+
181
+ let selector: string;
182
+ let ancestors: readonly string[];
183
+ if (flattened.ok) {
184
+ selector = rewriteFlatSelector(
185
+ flattened.selector,
186
+ rule.span,
187
+ collector,
188
+ prefix,
189
+ strategy,
190
+ diagnostics,
191
+ reported,
192
+ );
193
+ ancestors = [];
194
+ } else {
195
+ selector = rewriteFlatSelector(
196
+ rule.selector,
197
+ rule.span,
198
+ collector,
199
+ prefix,
200
+ strategy,
201
+ diagnostics,
202
+ reported,
203
+ );
204
+ ancestors = rule.ancestors.map((ancestor) =>
205
+ rewriteFlatSelector(ancestor, rule.span, collector, prefix, strategy, diagnostics, reported),
206
+ );
207
+ }
208
+
209
+ const ancestorsUnchanged =
210
+ ancestors.length === rule.ancestors.length &&
211
+ ancestors.every((ancestor, index) => ancestor === rule.ancestors[index]);
212
+ if (selector === rule.selector && ancestorsUnchanged) return rule;
213
+
214
+ return ruleIr({
215
+ selector,
216
+ ancestors,
217
+ declarations: rule.declarations,
218
+ condition: rule.condition,
219
+ context: rule.context,
220
+ span: rule.span,
221
+ });
222
+ }
223
+
224
+ function rewriteChild(
225
+ child: NativeBlockChild,
226
+ collector: Collector,
227
+ prefix: string,
228
+ strategy: "none" | "prefix",
229
+ diagnostics: Diagnostic[],
230
+ reported: Set<string>,
231
+ ): NativeBlockChild {
232
+ if ("children" in child) {
233
+ return rewriteBlock(child, collector, prefix, strategy, diagnostics, reported);
234
+ }
235
+ if ("declarations" in child) {
236
+ return rewriteRule(child, collector, prefix, strategy, diagnostics, reported);
237
+ }
238
+ return child;
239
+ }
240
+
241
+ function rewriteBlock(
242
+ block: NativeBlockIr,
243
+ collector: Collector,
244
+ prefix: string,
245
+ strategy: "none" | "prefix",
246
+ diagnostics: Diagnostic[],
247
+ reported: Set<string>,
248
+ ): NativeBlockIr {
249
+ if (block.statement === true) return block;
250
+ const children = block.children.map((child) =>
251
+ rewriteChild(child, collector, prefix, strategy, diagnostics, reported),
252
+ );
253
+ if (children.every((child, index) => child === block.children[index])) return block;
254
+ return nativeBlockIr({
255
+ name: block.name,
256
+ prelude: block.prelude,
257
+ children,
258
+ span: block.span,
259
+ });
260
+ }
261
+
262
+ function tableOf(collector: Collector): ClassTable {
263
+ return Object.freeze({ entries: Object.freeze([...collector.entries]) });
264
+ }
265
+
266
+ /**
267
+ * Rename every local class token and unwrap `:global(...)` wrappers.
268
+ *
269
+ * @param ir Lowered (and normally substituted) stylesheet IR.
270
+ * @param options Effective prefix and strategy; `options.naming.prefix` wins
271
+ * over `options.prefix`, and the strategy defaults to `prefix`.
272
+ * @returns The rebuilt IR (the input instance when nothing changed), the
273
+ * first-appearance {@link ClassTable} and CHIKX4001 diagnostics for invalid
274
+ * prefix/result configurations.
275
+ */
276
+ export function renameClasses(ir: StylesheetIr, options: RenameOptions = {}): RenameResult {
277
+ const strategy: "none" | "prefix" = options.naming?.strategy ?? "prefix";
278
+ const prefix = options.naming?.prefix ?? options.prefix ?? "";
279
+ const collector: Collector = { entries: [], indexes: new Map() };
280
+ const diagnostics: Diagnostic[] = [];
281
+ const reported = new Set<string>();
282
+
283
+ if (strategy === "prefix" && prefix !== "" && !isValidCssIdentifier(prefix)) {
284
+ // Report once, at the first class we can point at; if the stylesheet has no
285
+ // classes the invalid prefix is inert and reporting would have no location.
286
+ const firstRule = findFirstRuleSpan(ir);
287
+ if (firstRule !== null) {
288
+ diagnostics.push(
289
+ error(
290
+ "CHIKX4001",
291
+ `Invalid class prefix "${prefix}": it is not a valid CSS identifier, so classes stay unprefixed (SPECIFICATION.md §7.2).`,
292
+ firstRule,
293
+ ),
294
+ );
295
+ }
296
+ }
297
+
298
+ const rules = ir.rules.map((node) =>
299
+ "declarations" in node
300
+ ? rewriteRule(node, collector, prefix, strategy, diagnostics, reported)
301
+ : rewriteBlock(node, collector, prefix, strategy, diagnostics, reported),
302
+ );
303
+
304
+ const changed = rules.every((node, index) => node === ir.rules[index]) === false;
305
+ return {
306
+ ir: changed ? stylesheetIr(rules) : ir,
307
+ classes: tableOf(collector),
308
+ diagnostics,
309
+ };
310
+ }
311
+
312
+ /** Span of the first rule node, or `null` for an empty stylesheet. */
313
+ function findFirstRuleSpan(ir: StylesheetIr): Span | null {
314
+ for (const node of ir.rules) {
315
+ if ("declarations" in node) return node.span;
316
+ if (node.statement !== true) {
317
+ const found = findFirstRuleInChildren(node.children);
318
+ if (found !== null) return found;
319
+ }
320
+ }
321
+ return null;
322
+ }
323
+
324
+ function findFirstRuleInChildren(children: readonly NativeBlockChild[]): Span | null {
325
+ for (const child of children) {
326
+ if ("declarations" in child) return child.span;
327
+ if ("children" in child) {
328
+ const found = findFirstRuleInChildren(child.children);
329
+ if (found !== null) return found;
330
+ }
331
+ }
332
+ return null;
333
+ }
@@ -0,0 +1,267 @@
1
+ import { readCssIdentifier } from "./table.js";
2
+
3
+ /**
4
+ * Class-token scanning of a flattened selector (SPECIFICATION.md §7.1, §7.2).
5
+ *
6
+ * The A06 selector module exposes structural scanning but not class ranges, so
7
+ * this module mirrors the same state machine — quoted strings, comments,
8
+ * backslash escapes and attribute selectors stay opaque — and adds exactly two
9
+ * observations:
10
+ *
11
+ * - `.` followed by a CSS identifier is a class token; the name is escape-decoded
12
+ * (`.foo\.bar` → `foo.bar`, `.\31 23` → `123`);
13
+ * - a `:global(...)` function around class tokens is recorded so the wrapper can
14
+ * be removed and its classes kept literal.
15
+ *
16
+ * Policy boundary (documented per the task): every class token is renamed
17
+ * **except** tokens inside `:global(...)` and inside strings, comments or
18
+ * attribute values. Class tokens inside functional pseudos such as `:is(.a)` or
19
+ * `:not(.a)` are real class selectors in the same selector and therefore are
20
+ * renamed; pseudo names, pseudo-element names, ids, type selectors and
21
+ * attribute text are never touched.
22
+ */
23
+
24
+ /** One class token in a flattened selector. */
25
+ export interface ScannedClass {
26
+ /** Offset of the leading dot. */
27
+ readonly start: number;
28
+ /** Exclusive end offset after the identifier. */
29
+ readonly end: number;
30
+ /** Exact source slice, dot included. */
31
+ readonly raw: string;
32
+ /** Decoded class name. */
33
+ readonly name: string;
34
+ /** `true` when the token is inside a `:global(...)` function. */
35
+ readonly global: boolean;
36
+ }
37
+
38
+ /** One `:global(...)` wrapper, kept as two removable ranges. */
39
+ export interface GlobalEscape {
40
+ /** Offset of the leading `:`. */
41
+ readonly start: number;
42
+ /** Exclusive end of `:global(`, where the inner content starts. */
43
+ readonly openEnd: number;
44
+ /** Offset of the closing `)`. */
45
+ readonly closeStart: number;
46
+ /** Exclusive end after the closing `)`. */
47
+ readonly end: number;
48
+ }
49
+
50
+ /** Result of scanning one flattened selector. */
51
+ export type SelectorClassScan =
52
+ | {
53
+ readonly ok: true;
54
+ /** Class tokens in source order, globals included. */
55
+ readonly classes: readonly ScannedClass[];
56
+ /** `:global(...)` wrappers in source order. */
57
+ readonly globals: readonly GlobalEscape[];
58
+ }
59
+ | { readonly ok: false; readonly reason: string };
60
+
61
+ /** Top of the opener stack, or `""` when no bracket/paren is open. */
62
+ function top(stack: readonly string[]): string {
63
+ return stack[stack.length - 1] ?? "";
64
+ }
65
+
66
+ /** Find the `)` matching the `(` at `open`, honouring strings and comments. */
67
+ function findClosingParen(text: string, open: number): number {
68
+ let depth = 0;
69
+ let quote: '"' | "'" | null = null;
70
+ let inComment = false;
71
+ let escaped = false;
72
+
73
+ for (let index = open; index < text.length; index += 1) {
74
+ const character = text.charAt(index);
75
+ if (inComment) {
76
+ if (character === "*" && text.charAt(index + 1) === "/") {
77
+ inComment = false;
78
+ index += 1;
79
+ }
80
+ continue;
81
+ }
82
+ if (quote !== null) {
83
+ if (escaped) {
84
+ escaped = false;
85
+ continue;
86
+ }
87
+ if (character === "\\") {
88
+ escaped = true;
89
+ continue;
90
+ }
91
+ if (character === quote) quote = null;
92
+ continue;
93
+ }
94
+ if (escaped) {
95
+ escaped = false;
96
+ continue;
97
+ }
98
+ if (character === "\\") {
99
+ escaped = true;
100
+ continue;
101
+ }
102
+ if (character === "/" && text.charAt(index + 1) === "*") {
103
+ inComment = true;
104
+ index += 1;
105
+ continue;
106
+ }
107
+ if (character === '"' || character === "'") {
108
+ quote = character;
109
+ continue;
110
+ }
111
+ if (character === "(") {
112
+ depth += 1;
113
+ continue;
114
+ }
115
+ if (character === ")") {
116
+ depth -= 1;
117
+ if (depth === 0) return index;
118
+ }
119
+ }
120
+ return -1;
121
+ }
122
+
123
+ /** True when `:global(` starts at `index` (ASCII case-insensitive). */
124
+ function isGlobalFunction(text: string, index: number): boolean {
125
+ return text.slice(index, index + 8).toLowerCase() === ":global(";
126
+ }
127
+
128
+ /**
129
+ * Scan one flattened selector for class tokens and `:global(...)` wrappers.
130
+ *
131
+ * Pure and deterministic. Structural failures (unbalanced brackets, an
132
+ * unterminated string or comment, a trailing escape) return `ok: false` with a
133
+ * reason; callers then leave the selector untouched so the emitter reports the
134
+ * same CHIKX1001 it already reports for malformed selectors.
135
+ */
136
+ export function scanSelectorClasses(text: string): SelectorClassScan {
137
+ const classes: ScannedClass[] = [];
138
+ const globals: GlobalEscape[] = [];
139
+ const closers: string[] = [];
140
+ let quote: '"' | "'" | null = null;
141
+ let inComment = false;
142
+ let escaped = false;
143
+
144
+ for (let index = 0; index < text.length; index += 1) {
145
+ const character = text.charAt(index);
146
+
147
+ if (inComment) {
148
+ if (character === "*" && text.charAt(index + 1) === "/") {
149
+ inComment = false;
150
+ index += 1;
151
+ }
152
+ continue;
153
+ }
154
+
155
+ if (quote !== null) {
156
+ if (escaped) {
157
+ escaped = false;
158
+ continue;
159
+ }
160
+ if (character === "\\") {
161
+ escaped = true;
162
+ continue;
163
+ }
164
+ if (character === quote) quote = null;
165
+ continue;
166
+ }
167
+
168
+ if (escaped) {
169
+ escaped = false;
170
+ continue;
171
+ }
172
+ if (character === "\\") {
173
+ escaped = true;
174
+ continue;
175
+ }
176
+ if (character === "/" && text.charAt(index + 1) === "*") {
177
+ inComment = true;
178
+ index += 1;
179
+ continue;
180
+ }
181
+ if (character === '"' || character === "'") {
182
+ quote = character;
183
+ continue;
184
+ }
185
+ if (character === "(") {
186
+ closers.push(")");
187
+ continue;
188
+ }
189
+ if (character === "[") {
190
+ closers.push("]");
191
+ continue;
192
+ }
193
+ if (character === ")" || character === "]") {
194
+ if (closers.pop() !== character) {
195
+ return { ok: false, reason: `unbalanced "${character}"` };
196
+ }
197
+ continue;
198
+ }
199
+
200
+ if (top(closers) === "]") continue;
201
+
202
+ if (character === ":" && isGlobalFunction(text, index)) {
203
+ const closeStart = findClosingParen(text, index + 7);
204
+ if (closeStart !== -1) {
205
+ globals.push({ start: index, openEnd: index + 8, closeStart, end: closeStart + 1 });
206
+ }
207
+ continue;
208
+ }
209
+
210
+ if (character === ".") {
211
+ const identifier = readCssIdentifier(text, index + 1);
212
+ if (identifier !== null) {
213
+ classes.push({
214
+ start: index,
215
+ end: identifier.end,
216
+ raw: text.slice(index, identifier.end),
217
+ name: identifier.name,
218
+ global: false,
219
+ });
220
+ index = identifier.end - 1;
221
+ }
222
+ }
223
+ }
224
+
225
+ if (quote !== null) return { ok: false, reason: "unterminated string" };
226
+ if (inComment) return { ok: false, reason: "unterminated comment" };
227
+ if (escaped) return { ok: false, reason: "trailing escape" };
228
+ if (closers.length > 0) {
229
+ return { ok: false, reason: `unbalanced "${top(closers)}"` };
230
+ }
231
+
232
+ const marked = classes.map((token) => ({
233
+ ...token,
234
+ global: globals.some((range) => token.start >= range.openEnd && token.end <= range.closeStart),
235
+ }));
236
+
237
+ return { ok: true, classes: Object.freeze(marked), globals: Object.freeze(globals) };
238
+ }
239
+
240
+ /** One text replacement over `[start, end)`. */
241
+ export interface SelectorEdit {
242
+ /** Inclusive start offset. */
243
+ readonly start: number;
244
+ /** Exclusive end offset. */
245
+ readonly end: number;
246
+ /** Replacement text; empty removes the range. */
247
+ readonly text: string;
248
+ }
249
+
250
+ /**
251
+ * Apply non-overlapping selector edits in ascending offset order.
252
+ *
253
+ * Edits are sorted defensively so callers cannot depend on insertion order.
254
+ */
255
+ export function applySelectorEdits(text: string, edits: readonly SelectorEdit[]): string {
256
+ if (edits.length === 0) return text;
257
+ const sorted = [...edits].sort((left, right) => left.start - right.start);
258
+ let output = "";
259
+ let cursor = 0;
260
+ for (const edit of sorted) {
261
+ if (edit.start < cursor) continue;
262
+ output += text.slice(cursor, edit.start);
263
+ output += edit.text;
264
+ cursor = edit.end;
265
+ }
266
+ return output + text.slice(cursor);
267
+ }