zyzz 0.0.28 → 0.0.29

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 (302) hide show
  1. package/binding/wasi/wasi-worker.mjs +186 -0
  2. package/binding/wasi/zyzz.wasi.cjs +2302 -0
  3. package/binding/wasi/zyzz.wasm32-wasi.wasm +0 -0
  4. package/binding/zyzz.linux-x64-gnu.node +0 -0
  5. package/dist/babel/index.d.ts.map +1 -1
  6. package/dist/babel/index.js +2 -4
  7. package/dist/babel/index.js.map +1 -1
  8. package/dist/babel/internal/Compilation.d.ts +0 -1
  9. package/dist/babel/internal/Compilation.d.ts.map +1 -1
  10. package/dist/compiler/Graph.d.ts +14 -11
  11. package/dist/compiler/Graph.d.ts.map +1 -1
  12. package/dist/compiler/Graph.js +57 -1442
  13. package/dist/compiler/Graph.js.map +1 -1
  14. package/dist/compiler/Native.d.ts +7 -9
  15. package/dist/compiler/Native.d.ts.map +1 -1
  16. package/dist/compiler/Native.js +12 -716
  17. package/dist/compiler/Native.js.map +1 -1
  18. package/dist/compiler/Source.d.ts +122 -24
  19. package/dist/compiler/Source.d.ts.map +1 -1
  20. package/dist/compiler/Source.js +10 -1267
  21. package/dist/compiler/Source.js.map +1 -1
  22. package/dist/compiler/Transform.d.ts +4 -3
  23. package/dist/compiler/Transform.d.ts.map +1 -1
  24. package/dist/compiler/Transform.js +13 -961
  25. package/dist/compiler/Transform.js.map +1 -1
  26. package/dist/compiler/internal/AtRules.d.ts +5 -1
  27. package/dist/compiler/internal/AtRules.d.ts.map +1 -1
  28. package/dist/compiler/internal/AtRules.js +12 -106
  29. package/dist/compiler/internal/AtRules.js.map +1 -1
  30. package/dist/compiler/internal/Context.d.ts +11 -0
  31. package/dist/compiler/internal/Context.d.ts.map +1 -0
  32. package/dist/compiler/internal/Context.js +11 -0
  33. package/dist/compiler/internal/Context.js.map +1 -0
  34. package/dist/compiler/internal/Results.d.ts +148 -0
  35. package/dist/compiler/internal/Results.d.ts.map +1 -0
  36. package/dist/compiler/internal/Results.js +259 -0
  37. package/dist/compiler/internal/Results.js.map +1 -0
  38. package/dist/compiler/internal/Syntax.d.ts +0 -2
  39. package/dist/compiler/internal/Syntax.d.ts.map +1 -1
  40. package/dist/compiler/internal/Syntax.js +0 -2
  41. package/dist/compiler/internal/Syntax.js.map +1 -1
  42. package/dist/compiler/internal/Wire.d.ts +17 -0
  43. package/dist/compiler/internal/Wire.d.ts.map +1 -0
  44. package/dist/compiler/internal/Wire.js +795 -0
  45. package/dist/compiler/internal/Wire.js.map +1 -0
  46. package/dist/internal/Addon.d.ts +28 -0
  47. package/dist/internal/Addon.d.ts.map +1 -0
  48. package/dist/internal/Addon.js +87 -0
  49. package/dist/internal/Addon.js.map +1 -0
  50. package/dist/internal/Literal.d.ts +4 -1
  51. package/dist/internal/Literal.d.ts.map +1 -1
  52. package/dist/internal/Literal.js +4 -2
  53. package/dist/internal/Literal.js.map +1 -1
  54. package/dist/internal/Token.d.ts +2 -1
  55. package/dist/internal/Token.d.ts.map +1 -1
  56. package/dist/internal/Token.js +2 -2
  57. package/dist/internal/Token.js.map +1 -1
  58. package/dist/internal/Vlq.d.ts +14 -0
  59. package/dist/internal/Vlq.d.ts.map +1 -0
  60. package/dist/internal/Vlq.js +47 -0
  61. package/dist/internal/Vlq.js.map +1 -0
  62. package/dist/internal/Wasm.d.ts +22 -0
  63. package/dist/internal/Wasm.d.ts.map +1 -0
  64. package/dist/internal/Wasm.js +83 -0
  65. package/dist/internal/Wasm.js.map +1 -0
  66. package/dist/metro/Graph.d.ts +0 -1
  67. package/dist/metro/Graph.d.ts.map +1 -1
  68. package/dist/metro/transformer.d.ts.map +1 -1
  69. package/dist/metro/transformer.js +0 -1
  70. package/dist/metro/transformer.js.map +1 -1
  71. package/dist/next/internal/Project.d.ts.map +1 -1
  72. package/dist/next/internal/Project.js +2 -5
  73. package/dist/next/internal/Project.js.map +1 -1
  74. package/dist/node/Host.d.ts.map +1 -1
  75. package/dist/node/Host.js +0 -2
  76. package/dist/node/Host.js.map +1 -1
  77. package/dist/node/internal/Contracts.d.ts.map +1 -1
  78. package/dist/node/internal/Contracts.js +3 -7
  79. package/dist/node/internal/Contracts.js.map +1 -1
  80. package/dist/node/internal/Snapshot.d.ts +0 -1
  81. package/dist/node/internal/Snapshot.d.ts.map +1 -1
  82. package/dist/node/internal/Snapshot.js +0 -6
  83. package/dist/node/internal/Snapshot.js.map +1 -1
  84. package/dist/runtime/Composition.d.ts +2 -3
  85. package/dist/runtime/Composition.d.ts.map +1 -1
  86. package/dist/runtime/Composition.js +51 -30
  87. package/dist/runtime/Composition.js.map +1 -1
  88. package/dist/runtime/Recipe.d.ts.map +1 -1
  89. package/dist/runtime/Recipe.js +16 -12
  90. package/dist/runtime/Recipe.js.map +1 -1
  91. package/dist/unplugin/index.d.ts.map +1 -1
  92. package/dist/unplugin/index.js +3 -6
  93. package/dist/unplugin/index.js.map +1 -1
  94. package/dist/vite/index.d.ts.map +1 -1
  95. package/dist/vite/index.js +696 -201
  96. package/dist/vite/index.js.map +1 -1
  97. package/dist/web/Css.d.ts +8 -3
  98. package/dist/web/Css.d.ts.map +1 -1
  99. package/dist/web/Css.js +5 -721
  100. package/dist/web/Css.js.map +1 -1
  101. package/dist/web/internal/Block.d.ts +0 -2
  102. package/dist/web/internal/Block.d.ts.map +1 -1
  103. package/dist/web/internal/Block.js +1 -12
  104. package/dist/web/internal/Block.js.map +1 -1
  105. package/dist/web/internal/ClassName.d.ts +23 -18
  106. package/dist/web/internal/ClassName.d.ts.map +1 -1
  107. package/dist/web/internal/ClassName.js +26 -55
  108. package/dist/web/internal/ClassName.js.map +1 -1
  109. package/dist/web/internal/Contributions.d.ts +3 -5
  110. package/dist/web/internal/Contributions.d.ts.map +1 -1
  111. package/dist/web/internal/Contributions.js +0 -45
  112. package/dist/web/internal/Contributions.js.map +1 -1
  113. package/dist/web/internal/Engine.browser.d.ts +8 -0
  114. package/dist/web/internal/Engine.browser.d.ts.map +1 -0
  115. package/dist/web/internal/Engine.browser.js +42 -0
  116. package/dist/web/internal/Engine.browser.js.map +1 -0
  117. package/dist/web/internal/Engine.d.ts +9 -0
  118. package/dist/web/internal/Engine.d.ts.map +1 -0
  119. package/dist/web/internal/Engine.js +14 -0
  120. package/dist/web/internal/Engine.js.map +1 -0
  121. package/dist/web/internal/Themes.d.ts +1 -9
  122. package/dist/web/internal/Themes.d.ts.map +1 -1
  123. package/dist/web/internal/Themes.js +3 -221
  124. package/dist/web/internal/Themes.js.map +1 -1
  125. package/dist/web/internal/Wasm.generated.d.ts +4 -0
  126. package/dist/web/internal/Wasm.generated.d.ts.map +1 -0
  127. package/dist/web/internal/Wasm.generated.js +4 -0
  128. package/dist/web/internal/Wasm.generated.js.map +1 -0
  129. package/dist/web/internal/Wire.d.ts +19 -0
  130. package/dist/web/internal/Wire.d.ts.map +1 -0
  131. package/dist/web/internal/Wire.js +387 -0
  132. package/dist/web/internal/Wire.js.map +1 -0
  133. package/package.json +20 -1
  134. package/src/babel/index.ts +2 -5
  135. package/src/babel/internal/Compilation.ts +0 -1
  136. package/src/compiler/Graph.ts +89 -1930
  137. package/src/compiler/Native.ts +18 -871
  138. package/src/compiler/Source.ts +144 -1803
  139. package/src/compiler/Transform.ts +16 -1315
  140. package/src/compiler/internal/AtRules.ts +27 -115
  141. package/src/compiler/internal/Context.ts +11 -0
  142. package/src/compiler/internal/Results.ts +406 -0
  143. package/src/compiler/internal/Syntax.ts +0 -3
  144. package/src/compiler/internal/Wire.ts +1045 -0
  145. package/src/internal/Addon.ts +118 -0
  146. package/src/internal/Literal.ts +4 -2
  147. package/src/internal/Token.ts +2 -2
  148. package/src/internal/Vlq.ts +59 -0
  149. package/src/internal/Wasm.ts +111 -0
  150. package/src/metro/transformer.ts +0 -1
  151. package/src/next/internal/Project.ts +2 -9
  152. package/src/node/Host.ts +0 -2
  153. package/src/node/internal/Contracts.ts +3 -7
  154. package/src/node/internal/Snapshot.ts +0 -8
  155. package/src/runtime/Composition.ts +48 -25
  156. package/src/runtime/Recipe.ts +21 -14
  157. package/src/unplugin/index.ts +6 -6
  158. package/src/vite/index.ts +922 -221
  159. package/src/web/Css.ts +12 -908
  160. package/src/web/internal/Block.ts +0 -14
  161. package/src/web/internal/ClassName.ts +52 -85
  162. package/src/web/internal/Contributions.ts +3 -59
  163. package/src/web/internal/Engine.browser.ts +51 -0
  164. package/src/web/internal/Engine.ts +17 -0
  165. package/src/web/internal/Themes.ts +3 -279
  166. package/src/web/internal/Wasm.generated.ts +3 -0
  167. package/src/web/internal/Wire.ts +466 -0
  168. package/src/web/internal/wasm.d.ts +9 -0
  169. package/dist/compiler/internal/Applications.d.ts +0 -55
  170. package/dist/compiler/internal/Applications.d.ts.map +0 -1
  171. package/dist/compiler/internal/Applications.js +0 -393
  172. package/dist/compiler/internal/Applications.js.map +0 -1
  173. package/dist/compiler/internal/ColorProfiles.d.ts +0 -3
  174. package/dist/compiler/internal/ColorProfiles.d.ts.map +0 -1
  175. package/dist/compiler/internal/ColorProfiles.js +0 -84
  176. package/dist/compiler/internal/ColorProfiles.js.map +0 -1
  177. package/dist/compiler/internal/CompositionBindings.d.ts +0 -16
  178. package/dist/compiler/internal/CompositionBindings.d.ts.map +0 -1
  179. package/dist/compiler/internal/CompositionBindings.js +0 -89
  180. package/dist/compiler/internal/CompositionBindings.js.map +0 -1
  181. package/dist/compiler/internal/Compositions.d.ts +0 -37
  182. package/dist/compiler/internal/Compositions.d.ts.map +0 -1
  183. package/dist/compiler/internal/Compositions.js +0 -494
  184. package/dist/compiler/internal/Compositions.js.map +0 -1
  185. package/dist/compiler/internal/Configurations.d.ts +0 -26
  186. package/dist/compiler/internal/Configurations.d.ts.map +0 -1
  187. package/dist/compiler/internal/Configurations.js +0 -259
  188. package/dist/compiler/internal/Configurations.js.map +0 -1
  189. package/dist/compiler/internal/Contract.d.ts +0 -30
  190. package/dist/compiler/internal/Contract.d.ts.map +0 -1
  191. package/dist/compiler/internal/Contract.js +0 -753
  192. package/dist/compiler/internal/Contract.js.map +0 -1
  193. package/dist/compiler/internal/Contributions.d.ts +0 -54
  194. package/dist/compiler/internal/Contributions.d.ts.map +0 -1
  195. package/dist/compiler/internal/Contributions.js +0 -1151
  196. package/dist/compiler/internal/Contributions.js.map +0 -1
  197. package/dist/compiler/internal/Descriptors.d.ts +0 -15
  198. package/dist/compiler/internal/Descriptors.d.ts.map +0 -1
  199. package/dist/compiler/internal/Descriptors.js +0 -192
  200. package/dist/compiler/internal/Descriptors.js.map +0 -1
  201. package/dist/compiler/internal/Dynamic.d.ts +0 -14
  202. package/dist/compiler/internal/Dynamic.d.ts.map +0 -1
  203. package/dist/compiler/internal/Dynamic.js +0 -175
  204. package/dist/compiler/internal/Dynamic.js.map +0 -1
  205. package/dist/compiler/internal/Expression.d.ts +0 -19
  206. package/dist/compiler/internal/Expression.d.ts.map +0 -1
  207. package/dist/compiler/internal/Expression.js +0 -164
  208. package/dist/compiler/internal/Expression.js.map +0 -1
  209. package/dist/compiler/internal/Functions.d.ts +0 -5
  210. package/dist/compiler/internal/Functions.d.ts.map +0 -1
  211. package/dist/compiler/internal/Functions.js +0 -191
  212. package/dist/compiler/internal/Functions.js.map +0 -1
  213. package/dist/compiler/internal/Keyframes.d.ts +0 -5
  214. package/dist/compiler/internal/Keyframes.d.ts.map +0 -1
  215. package/dist/compiler/internal/Keyframes.js +0 -51
  216. package/dist/compiler/internal/Keyframes.js.map +0 -1
  217. package/dist/compiler/internal/Names.d.ts +0 -5
  218. package/dist/compiler/internal/Names.d.ts.map +0 -1
  219. package/dist/compiler/internal/Names.js +0 -37
  220. package/dist/compiler/internal/Names.js.map +0 -1
  221. package/dist/compiler/internal/NativeBindings.d.ts +0 -72
  222. package/dist/compiler/internal/NativeBindings.d.ts.map +0 -1
  223. package/dist/compiler/internal/NativeBindings.js +0 -171
  224. package/dist/compiler/internal/NativeBindings.js.map +0 -1
  225. package/dist/compiler/internal/NativeMedia.d.ts +0 -14
  226. package/dist/compiler/internal/NativeMedia.d.ts.map +0 -1
  227. package/dist/compiler/internal/NativeMedia.js +0 -287
  228. package/dist/compiler/internal/NativeMedia.js.map +0 -1
  229. package/dist/compiler/internal/NativeVars.d.ts +0 -28
  230. package/dist/compiler/internal/NativeVars.d.ts.map +0 -1
  231. package/dist/compiler/internal/NativeVars.js +0 -160
  232. package/dist/compiler/internal/NativeVars.js.map +0 -1
  233. package/dist/compiler/internal/PackedStyles.d.ts +0 -37
  234. package/dist/compiler/internal/PackedStyles.d.ts.map +0 -1
  235. package/dist/compiler/internal/PackedStyles.js +0 -344
  236. package/dist/compiler/internal/PackedStyles.js.map +0 -1
  237. package/dist/compiler/internal/Properties.d.ts +0 -5
  238. package/dist/compiler/internal/Properties.d.ts.map +0 -1
  239. package/dist/compiler/internal/Properties.js +0 -82
  240. package/dist/compiler/internal/Properties.js.map +0 -1
  241. package/dist/compiler/internal/RecipeConditions.d.ts +0 -41
  242. package/dist/compiler/internal/RecipeConditions.d.ts.map +0 -1
  243. package/dist/compiler/internal/RecipeConditions.js +0 -82
  244. package/dist/compiler/internal/RecipeConditions.js.map +0 -1
  245. package/dist/compiler/internal/RecipePayloads.d.ts +0 -31
  246. package/dist/compiler/internal/RecipePayloads.d.ts.map +0 -1
  247. package/dist/compiler/internal/RecipePayloads.js +0 -99
  248. package/dist/compiler/internal/RecipePayloads.js.map +0 -1
  249. package/dist/compiler/internal/Recipes.d.ts +0 -32
  250. package/dist/compiler/internal/Recipes.d.ts.map +0 -1
  251. package/dist/compiler/internal/Recipes.js +0 -339
  252. package/dist/compiler/internal/Recipes.js.map +0 -1
  253. package/dist/compiler/internal/Selectors.d.ts +0 -13
  254. package/dist/compiler/internal/Selectors.d.ts.map +0 -1
  255. package/dist/compiler/internal/Selectors.js +0 -280
  256. package/dist/compiler/internal/Selectors.js.map +0 -1
  257. package/dist/compiler/internal/Static.d.ts +0 -39
  258. package/dist/compiler/internal/Static.d.ts.map +0 -1
  259. package/dist/compiler/internal/Static.js +0 -534
  260. package/dist/compiler/internal/Static.js.map +0 -1
  261. package/dist/compiler/internal/Themes.d.ts +0 -190
  262. package/dist/compiler/internal/Themes.d.ts.map +0 -1
  263. package/dist/compiler/internal/Themes.js +0 -1577
  264. package/dist/compiler/internal/Themes.js.map +0 -1
  265. package/dist/compiler/internal/Vars.d.ts +0 -33
  266. package/dist/compiler/internal/Vars.d.ts.map +0 -1
  267. package/dist/compiler/internal/Vars.js +0 -387
  268. package/dist/compiler/internal/Vars.js.map +0 -1
  269. package/dist/compiler/internal/ViewTransitions.d.ts +0 -3
  270. package/dist/compiler/internal/ViewTransitions.d.ts.map +0 -1
  271. package/dist/compiler/internal/ViewTransitions.js +0 -68
  272. package/dist/compiler/internal/ViewTransitions.js.map +0 -1
  273. package/dist/internal/Cascade.d.ts +0 -74
  274. package/dist/internal/Cascade.d.ts.map +0 -1
  275. package/dist/internal/Cascade.js +0 -210
  276. package/dist/internal/Cascade.js.map +0 -1
  277. package/src/compiler/internal/Applications.ts +0 -610
  278. package/src/compiler/internal/ColorProfiles.ts +0 -93
  279. package/src/compiler/internal/CompositionBindings.ts +0 -120
  280. package/src/compiler/internal/Compositions.ts +0 -678
  281. package/src/compiler/internal/Configurations.ts +0 -349
  282. package/src/compiler/internal/Contract.ts +0 -1028
  283. package/src/compiler/internal/Contributions.ts +0 -1561
  284. package/src/compiler/internal/Descriptors.ts +0 -242
  285. package/src/compiler/internal/Dynamic.ts +0 -275
  286. package/src/compiler/internal/Expression.ts +0 -218
  287. package/src/compiler/internal/Functions.ts +0 -206
  288. package/src/compiler/internal/Keyframes.ts +0 -62
  289. package/src/compiler/internal/Names.ts +0 -48
  290. package/src/compiler/internal/NativeBindings.ts +0 -235
  291. package/src/compiler/internal/NativeMedia.ts +0 -342
  292. package/src/compiler/internal/NativeVars.ts +0 -231
  293. package/src/compiler/internal/PackedStyles.ts +0 -473
  294. package/src/compiler/internal/Properties.ts +0 -121
  295. package/src/compiler/internal/RecipeConditions.ts +0 -139
  296. package/src/compiler/internal/RecipePayloads.ts +0 -132
  297. package/src/compiler/internal/Recipes.ts +0 -531
  298. package/src/compiler/internal/Selectors.ts +0 -353
  299. package/src/compiler/internal/Static.ts +0 -784
  300. package/src/compiler/internal/Themes.ts +0 -2398
  301. package/src/compiler/internal/Vars.ts +0 -516
  302. package/src/compiler/internal/ViewTransitions.ts +0 -82
@@ -1,1577 +0,0 @@
1
- import * as Identity from '../../internal/Identity.js';
2
- import * as Identifiers from './Identifiers.js';
3
- import * as Config from '../../internal/Configuration.js';
4
- import * as Expression from './Expression.js';
5
- import * as Configurations from './Configurations.js';
6
- import * as Token from '../../internal/Token.js';
7
- import * as Theme from '../../internal/Theme.js';
8
- import * as VariableSets from '../../internal/VariableSets.js';
9
- import * as Vars from '../../Vars.js';
10
- /** Internal context passed between graph extraction and module rewriting. */
11
- export const context = Symbol('zyzz.source.graph');
12
- /** Resolves the variable alternatives a linked configuration or variable set exposes to native hooks. */
13
- export function varsRead(link) {
14
- const named = link.call.options?.themes !== undefined;
15
- const vars = (() => {
16
- if (!named)
17
- return { default: link.definition };
18
- return Object.fromEntries(Object.entries(link.members ?? {}).flatMap((entry) => {
19
- const [key, member] = entry;
20
- const path = JSON.parse(key);
21
- if (path.length === 1 && link.call.selection)
22
- return [[path[0], member.definition]];
23
- if (path.length === 2 && path[0] === 'themes')
24
- return [[path[1], member.definition]];
25
- return [];
26
- }));
27
- })();
28
- return {
29
- defaultVars: named ? String(link.call.options.defaultTheme) : 'default',
30
- owner: link.call.name,
31
- unnamed: !named,
32
- vars,
33
- };
34
- }
35
- /** Collects immutable module-level themes without evaluating source. */
36
- export function collect(program, options) {
37
- const staticTokens = [];
38
- const aliases = [];
39
- const aliasBindings = new Map();
40
- const aliasReferences = new Set();
41
- const appearances = new Set();
42
- const scopeApplications = new Map();
43
- const selections = new Set();
44
- const selectionUses = new Set();
45
- const calls = [];
46
- const scripts = new Set();
47
- const definitions = new Map();
48
- const factories = new Set();
49
- const imports = new Set();
50
- const variableImports = new Set();
51
- const configImports = new Set();
52
- const factoryImports = new Map();
53
- const nativeFactories = new Set();
54
- const configs = new Map();
55
- const configBindings = new Map();
56
- const factoryReferences = new Set();
57
- const references = [];
58
- const varsReads = [];
59
- const styles = new Map();
60
- const themes = Object.create(null);
61
- const tokens = new Map();
62
- for (const node of program.body) {
63
- if (node.type !== 'ImportDeclaration' ||
64
- !['zyzz', 'zyzz/react-native', 'zyzz/react-native/react'].includes(node.source.value) ||
65
- node.importKind === 'type')
66
- continue;
67
- for (const specifier of node.specifiers) {
68
- if (node.source.value !== 'zyzz') {
69
- if (specifier.type === 'ImportSpecifier' &&
70
- specifier.importKind !== 'type' &&
71
- (specifier.imported.type === 'Identifier'
72
- ? specifier.imported.name
73
- : specifier.imported.value) === 'defineConfig') {
74
- factoryImports.set(specifier.start, 'create');
75
- nativeFactories.add(specifier.local.name);
76
- }
77
- continue;
78
- }
79
- if (specifier.type === 'ImportSpecifier' &&
80
- specifier.importKind !== 'type' &&
81
- (specifier.imported.type === 'Identifier'
82
- ? specifier.imported.name
83
- : specifier.imported.value) === 'Vars') {
84
- imports.add(specifier.start);
85
- variableImports.add(specifier.start);
86
- }
87
- else if (specifier.type === 'ImportSpecifier' &&
88
- specifier.importKind !== 'type' &&
89
- (specifier.imported.type === 'Identifier'
90
- ? specifier.imported.name
91
- : specifier.imported.value) === 'Config')
92
- configImports.add(specifier.start);
93
- else if (specifier.type === 'ImportSpecifier' &&
94
- specifier.importKind !== 'type' &&
95
- ['defineConfig', 'defineVars', 'extendVars'].includes(specifier.imported.type === 'Identifier'
96
- ? specifier.imported.name
97
- : specifier.imported.value)) {
98
- const name = specifier.imported.type === 'Identifier'
99
- ? specifier.imported.name
100
- : specifier.imported.value;
101
- if (name === 'defineConfig')
102
- factoryImports.set(specifier.start, 'create');
103
- else if (name === 'defineVars')
104
- factoryImports.set(specifier.start, 'define');
105
- else
106
- factoryImports.set(specifier.start, 'extend');
107
- }
108
- }
109
- }
110
- if (!imports.size &&
111
- !configImports.size &&
112
- !factoryImports.size &&
113
- !Object.keys(options.links ?? {}).length)
114
- return undefined;
115
- const names = new Map();
116
- const aliasNames = new Map();
117
- const exports = Object.create(null);
118
- for (const node of program.body) {
119
- if (node.type !== 'ImportDeclaration' || node.importKind === 'type')
120
- continue;
121
- for (const specifier of node.specifiers) {
122
- const link = options.links?.[specifier.local.name];
123
- if (!link ||
124
- link.kind === 'style-reference' ||
125
- link.kind === 'animation' ||
126
- link.kind === 'rule-reference' ||
127
- link.kind === 'variables')
128
- continue;
129
- const call = { ...link.call, start: -1, end: -1 };
130
- themes[call.name] = link.definition;
131
- if (link.kind === 'config') {
132
- const imported = {
133
- ...link,
134
- call,
135
- members: Object.fromEntries(Object.entries(link.members ?? {}).map(([key, member]) => [
136
- key,
137
- { ...member, call: { ...member.call, start: -1, end: -1 } },
138
- ])),
139
- };
140
- configs.set(specifier.local.name, imported);
141
- configBindings.set(specifier.start, imported);
142
- for (const member of Object.values(link.members ?? {}))
143
- themes[member.call.name] = member.definition;
144
- }
145
- else if (link.kind === 'theme') {
146
- definitions.set(specifier.start, call);
147
- names.set(specifier.local.name, call);
148
- }
149
- else {
150
- const alias = { ...call, destructured: false };
151
- aliasBindings.set(specifier.start, alias);
152
- aliasNames.set(specifier.local.name, alias);
153
- }
154
- }
155
- }
156
- const namespaces = new Set();
157
- const configNamespaces = new Set();
158
- const factoryFunctions = new Map();
159
- const variableNamespaces = new Set();
160
- for (const node of program.body)
161
- if (node.type === 'ImportDeclaration')
162
- for (const specifier of node.specifiers)
163
- if (variableImports.has(specifier.start))
164
- variableNamespaces.add(specifier.local.name);
165
- for (const node of program.body)
166
- if (node.type === 'ImportDeclaration')
167
- for (const specifier of node.specifiers)
168
- if (imports.has(specifier.start))
169
- namespaces.add(specifier.local.name);
170
- else if (configImports.has(specifier.start))
171
- configNamespaces.add(specifier.local.name);
172
- else if (factoryImports.has(specifier.start))
173
- factoryFunctions.set(specifier.local.name, factoryImports.get(specifier.start));
174
- function resolve(node) {
175
- if (node.type === 'Identifier') {
176
- const config = configs.get(node.name);
177
- if (config)
178
- return config;
179
- const call = names.get(node.name);
180
- if (call)
181
- return {
182
- binding: call.name,
183
- call,
184
- definition: themes[call.name],
185
- kind: 'theme',
186
- };
187
- return undefined;
188
- }
189
- if (node.type !== 'MemberExpression' || node.optional)
190
- return undefined;
191
- const path = [];
192
- let root = node;
193
- while (root.type === 'MemberExpression') {
194
- const key = (() => {
195
- if (root.property.type === 'Identifier' && !root.computed) {
196
- return root.property.name;
197
- }
198
- if (root.property.type === 'Literal' &&
199
- root.computed &&
200
- (typeof root.property.value === 'string' ||
201
- typeof root.property.value === 'number')) {
202
- return String(root.property.value);
203
- }
204
- return undefined;
205
- })();
206
- if (root.optional || key === undefined)
207
- return undefined;
208
- path.unshift(key);
209
- root = root.object;
210
- }
211
- const config = root.type === 'Identifier' ? configs.get(root.name) : undefined;
212
- if (config &&
213
- !config.call.selection &&
214
- !config.call.initialization &&
215
- !config.call.root &&
216
- path.length === 1 &&
217
- ['themes', 'vars', 'script', 'appearance'].includes(path[0])) {
218
- const key = path[0];
219
- if (config.call.variableConfig && key === 'themes')
220
- return undefined;
221
- if (key === 'script')
222
- scripts.add(config.call.name);
223
- if (key === 'appearance')
224
- appearances.add(config.call.name);
225
- if (key === 'themes' && !config.call.options?.themes)
226
- return undefined;
227
- if (key === 'themes' || key === 'vars')
228
- selections.add(config.call.name);
229
- if (key === 'script' && !config.call.script)
230
- fail('This packed configuration does not provide script(); rebuild its library with initialization support.', node);
231
- if (key === 'appearance' && !config.call.appearance)
232
- fail('This packed configuration does not provide appearance; rebuild its library with root control support.', node);
233
- const members = Object.fromEntries(Object.entries(config.members ?? {}).flatMap(([name, member]) => {
234
- const parts = JSON.parse(name);
235
- return (key === 'themes' || key === 'vars') && parts[0] === 'themes'
236
- ? [[JSON.stringify(parts.slice(1)), member]]
237
- : [];
238
- }));
239
- return {
240
- ...config,
241
- members,
242
- binding: `${config.binding}:${key}`,
243
- call: {
244
- ...config.call,
245
- members: Object.fromEntries(Object.entries(members).map(([name, member]) => [
246
- name,
247
- member.call.name,
248
- ])),
249
- ...helper(key),
250
- type: `${config.call.type}['${key}']`,
251
- },
252
- };
253
- }
254
- return config?.members?.[JSON.stringify(path)];
255
- }
256
- /** Marks which configuration helper a destructured or member binding represents. */
257
- function helper(key) {
258
- if (key === 'script')
259
- return { initialization: true };
260
- if (key === 'appearance')
261
- return { root: true };
262
- return { selection: true };
263
- }
264
- function readReference(node, derived) {
265
- if (node.type !== 'MemberExpression')
266
- return undefined;
267
- const path = [];
268
- let root = node;
269
- while (root.type === 'MemberExpression' && !root.optional) {
270
- const key = !root.computed && root.property.type === 'Identifier'
271
- ? root.property.name
272
- : root.property.type === 'Literal'
273
- ? String(root.property.value)
274
- : undefined;
275
- if (key === undefined)
276
- return undefined;
277
- path.unshift(key);
278
- root = root.object;
279
- }
280
- if (root.type !== 'Identifier')
281
- return undefined;
282
- const name = root.name;
283
- let value = (() => {
284
- if (derived?.name === name)
285
- return derived.vars;
286
- const call = names.get(name);
287
- if (call) {
288
- if (!call.variableSet &&
289
- !call.directVariables &&
290
- !['vars', 'tokens'].includes(path.shift()))
291
- return undefined;
292
- return themes[call.name]?.tokens;
293
- }
294
- // Config-bound vars read the configured variables, so they follow set scoping.
295
- const config = configs.get(name);
296
- if (!config?.call.variableConfig ||
297
- (!config.call.selection && path.shift() !== 'vars'))
298
- return undefined;
299
- return themes[config.call.name]?.tokens;
300
- })();
301
- if (value === undefined)
302
- return undefined;
303
- for (const key of path)
304
- value =
305
- value && typeof value === 'object'
306
- ? Object.getOwnPropertyDescriptor(value, key)?.value
307
- : undefined;
308
- return Token.is(value) ? value : undefined;
309
- }
310
- function data(node, derived) {
311
- const reference = readReference(node, derived);
312
- if (reference) {
313
- factoryReferences.add(node.start);
314
- return reference;
315
- }
316
- if (node.type === 'TSAsExpression' || node.type === 'TSSatisfiesExpression')
317
- return data(node.expression, derived);
318
- if (node.type === 'CallExpression' && !node.optional) {
319
- const callee = node.callee;
320
- if (callee.type === 'MemberExpression' &&
321
- !callee.computed &&
322
- !callee.optional &&
323
- callee.object.type === 'Identifier' &&
324
- variableNamespaces.has(callee.object.name) &&
325
- derived?.name !== callee.object.name &&
326
- callee.property.type === 'Identifier' &&
327
- callee.property.name === 'compose') {
328
- if (node.arguments.length !== 2)
329
- return fail('Vars.compose requires a literal domain and parts array.', node);
330
- const group = data(node.arguments[0], derived);
331
- const parts = data(node.arguments[1], derived);
332
- if ((group !== 'color' && group !== 'spacing') || !Array.isArray(parts))
333
- return fail('Vars.compose requires a literal domain and parts array.', node);
334
- factoryReferences.add(callee.start);
335
- return Vars.compose(group, parts);
336
- }
337
- }
338
- if (node.type === 'Literal' &&
339
- (typeof node.value === 'string' || typeof node.value === 'number'))
340
- return node.value;
341
- if (node.type === 'UnaryExpression' &&
342
- (node.operator === '+' || node.operator === '-') &&
343
- node.argument.type === 'Literal' &&
344
- typeof node.argument.value === 'number')
345
- return node.operator === '-' ? -node.argument.value : node.argument.value;
346
- if (node.type === 'ArrayExpression')
347
- return node.elements.map((element) => {
348
- if (!element || element.type === 'SpreadElement')
349
- return fail('Theme arrays require dense literal elements.', node);
350
- return data(element, derived);
351
- });
352
- if (node.type !== 'ObjectExpression')
353
- return fail('Theme values must be literal data; expressions are not evaluated.', node);
354
- const result = Object.create(null);
355
- for (const property of node.properties) {
356
- if (property.type !== 'Property' ||
357
- property.computed ||
358
- property.method ||
359
- property.shorthand ||
360
- property.kind !== 'init')
361
- return fail('Theme data requires explicit properties without spreads, methods, or computed keys.', property);
362
- const key = (() => {
363
- if (property.key.type === 'Identifier') {
364
- return property.key.name;
365
- }
366
- if (property.key.type === 'Literal' &&
367
- (typeof property.key.value === 'string' ||
368
- typeof property.key.value === 'number')) {
369
- return String(property.key.value);
370
- }
371
- return undefined;
372
- })();
373
- if (key === undefined || Object.hasOwn(result, key))
374
- return fail('Theme data requires unique literal keys.', property);
375
- result[key] = data(property.value, derived);
376
- }
377
- return result;
378
- }
379
- /** Reads a derived-variables callback as one parameter name and a literal body. */
380
- function derivation(argument) {
381
- const callback = argument && Expression.unwrap(argument);
382
- if (callback?.type !== 'ArrowFunctionExpression' &&
383
- callback?.type !== 'FunctionExpression')
384
- return undefined;
385
- const parameter = callback.params[0];
386
- const body = (() => {
387
- if (callback.body?.type !== 'BlockStatement')
388
- return callback.body;
389
- if (callback.body.body.length === 1 &&
390
- callback.body.body[0]?.type === 'ReturnStatement')
391
- return callback.body.body[0].argument;
392
- return undefined;
393
- })();
394
- if (callback.async ||
395
- callback.generator ||
396
- callback.params.length !== 1 ||
397
- parameter?.type !== 'Identifier' ||
398
- !body)
399
- fail('Derived variables require one named parameter and a literal return value.', callback);
400
- return { body, name: parameter.name };
401
- }
402
- /** Authored values of an extension, keeping base values at existing paths as `Vars.extend` types do. */
403
- function extendedValues(base, extension) {
404
- const metadata = extension[Token.definition];
405
- const known = base[Token.definition].values;
406
- const tree = Object.create(null);
407
- for (const path of Object.keys(metadata.values)) {
408
- const parts = metadata.paths?.[path] ?? path.split('.');
409
- const source = Object.hasOwn(known, path) ? base : extension;
410
- let target = tree;
411
- let token = source.tokens;
412
- for (const part of parts.slice(0, -1)) {
413
- target = (target[part] ??= Object.create(null));
414
- token = token[part];
415
- }
416
- const name = parts.at(-1);
417
- // Every metadata path has a reference in its own token tree.
418
- target[name] = token[name].value;
419
- }
420
- return tree;
421
- }
422
- function fail(message, node) {
423
- throw new InvalidError(message, node);
424
- }
425
- function type(node) {
426
- if (node.type === 'TSAsExpression' || node.type === 'TSSatisfiesExpression')
427
- return type(node.expression);
428
- if (node.type !== 'ObjectExpression')
429
- return Configurations.type(data(node));
430
- return `{${node.properties
431
- .map((property) => {
432
- if (property.type !== 'Property')
433
- return fail('Expected validated theme properties.', property);
434
- const key = (() => {
435
- if (property.key.type === 'Identifier') {
436
- return JSON.stringify(property.key.name);
437
- }
438
- if (property.key.type === 'Literal') {
439
- return JSON.stringify(property.key.value);
440
- }
441
- return fail('Expected a literal theme key.', property.key);
442
- })();
443
- return `readonly ${key}:${type(property.value)}`;
444
- })
445
- .join(';')}}`;
446
- }
447
- for (const statement of program.body) {
448
- const declaration = statement.type === 'ExportNamedDeclaration'
449
- ? statement.declaration
450
- : statement;
451
- if (declaration?.type !== 'VariableDeclaration')
452
- continue;
453
- for (const variable of declaration.declarations) {
454
- registerAlias({ declaration, statement, variable });
455
- const expression = variable.init;
456
- if (expression?.type !== 'CallExpression' || expression.optional)
457
- continue;
458
- const member = expression.callee;
459
- const directFactory = member.type === 'Identifier'
460
- ? factoryFunctions.get(member.name)
461
- : undefined;
462
- if (!directFactory &&
463
- (member.type !== 'MemberExpression' ||
464
- member.object.type !== 'Identifier' ||
465
- (!namespaces.has(member.object.name) &&
466
- !configNamespaces.has(member.object.name)) ||
467
- member.computed ||
468
- member.optional ||
469
- member.property.type !== 'Identifier' ||
470
- !(configNamespaces.has(member.object.name)
471
- ? ['create']
472
- : ['define', 'extend']).includes(member.property.name)))
473
- continue;
474
- const config = directFactory === 'create' ||
475
- (member.type === 'MemberExpression' &&
476
- member.object.type === 'Identifier' &&
477
- configNamespaces.has(member.object.name));
478
- const method = member.type === 'MemberExpression' &&
479
- member.property.type === 'Identifier'
480
- ? member.property.name
481
- : directFactory;
482
- const variableSet = directFactory === 'define' ||
483
- directFactory === 'extend' ||
484
- (member.type === 'MemberExpression' &&
485
- member.object.type === 'Identifier' &&
486
- variableNamespaces.has(member.object.name));
487
- if ((statement.type === 'ExportNamedDeclaration' && !options.linked) ||
488
- declaration.kind !== 'const' ||
489
- (variable.id.type !== 'Identifier' &&
490
- !(config && variable.id.type === 'ObjectPattern')))
491
- fail('Define local themes with a module-level const; exported themes require source linking.', variable);
492
- const bindings = (() => {
493
- if (variable.id.type !== 'ObjectPattern')
494
- return [];
495
- return variable.id.properties.map((property) => {
496
- if (property.type !== 'Property' ||
497
- property.computed ||
498
- property.key.type !== 'Identifier' ||
499
- property.value.type !== 'Identifier')
500
- fail('Configuration destructuring requires named bindings without defaults, rest, or nested patterns.', property);
501
- return { key: property.key.name, id: property.value };
502
- });
503
- })();
504
- const binding = variable.id.type === 'Identifier'
505
- ? variable.id.name
506
- : bindings[0]?.id.name;
507
- if (!binding)
508
- fail('Configuration destructuring requires a binding.', variable);
509
- let name = `${options.namespace}-${binding}`;
510
- if (!config && method === 'define') {
511
- const argument = expression.arguments[1];
512
- const callback = argument && Expression.unwrap(argument);
513
- const id = Identifiers.explicit(expression, callback?.type === 'ArrowFunctionExpression' ||
514
- callback?.type === 'FunctionExpression'
515
- ? 2
516
- : 1);
517
- if (id !== undefined)
518
- name = Identity.requireId(id, 'Vars.define');
519
- }
520
- if (!config && method === 'extend') {
521
- const argument = expression.arguments[2];
522
- const callback = argument && Expression.unwrap(argument);
523
- const id = Identifiers.explicit(expression, callback?.type === 'ArrowFunctionExpression' ||
524
- callback?.type === 'FunctionExpression'
525
- ? 3
526
- : 2);
527
- if (id !== undefined)
528
- name = Identity.requireId(id, 'Vars.extend');
529
- }
530
- if (config) {
531
- const nativeProvider = member.type === 'Identifier' && nativeFactories.has(member.name);
532
- if (nativeProvider && !options.native)
533
- fail('Native defineConfig requires a native compilation target.', expression);
534
- try {
535
- const link = Configurations.collect({
536
- data,
537
- expression,
538
- name,
539
- nativeProvider,
540
- resolve: (node) => {
541
- const link = resolve(node);
542
- if (link)
543
- factoryReferences.add(node.start);
544
- return link;
545
- },
546
- });
547
- if (variable.id.type === 'Identifier') {
548
- configs.set(variable.id.name, link);
549
- configBindings.set(variable.id.start, link);
550
- if (statement.type === 'ExportNamedDeclaration')
551
- exports[variable.id.name] = link;
552
- }
553
- calls.push(link.call);
554
- factories.add(expression.start);
555
- for (const member of Object.values(link.members ?? {}))
556
- themes[member.call.name] = member.definition;
557
- themes[link.call.name] = link.definition;
558
- for (const { key, id } of bindings) {
559
- if (key === 'Provider' && link.call.nativeProvider)
560
- continue;
561
- if ((key === 'style' || key === 'variants') &&
562
- !link.call.selection &&
563
- !link.call.initialization) {
564
- const alias = {
565
- ...link.call,
566
- recipe: key === 'variants',
567
- destructured: false,
568
- };
569
- aliasBindings.set(id.start, alias);
570
- aliasNames.set(id.name, alias);
571
- if (statement.type === 'ExportNamedDeclaration')
572
- exports[id.name] = {
573
- ...link,
574
- binding: `${options.namespace}-${id.name}`,
575
- call: alias,
576
- kind: 'style',
577
- };
578
- continue;
579
- }
580
- if (!link.call.selection &&
581
- !link.call.initialization &&
582
- !link.call.root &&
583
- (key === 'script' ||
584
- key === 'appearance' ||
585
- (key === 'themes' && link.call.options?.themes) ||
586
- (key === 'vars' && link.call.variableConfig))) {
587
- if ((key === 'script' && !link.call.script) ||
588
- (key === 'appearance' && !link.call.appearance) ||
589
- link.call.selection ||
590
- link.call.initialization)
591
- fail('This configuration helper is not available on the linked contract.', id);
592
- if (key === 'script')
593
- scripts.add(link.call.name);
594
- if (key === 'appearance')
595
- appearances.add(link.call.name);
596
- if (key === 'themes' || key === 'vars')
597
- selections.add(link.call.name);
598
- const members = Object.fromEntries(Object.entries(link.members ?? {}).flatMap(([pathKey, member]) => {
599
- const path = JSON.parse(pathKey);
600
- return (key === 'themes' || key === 'vars') &&
601
- (path[0] === 'themes' || path[0] === 'vars')
602
- ? [[JSON.stringify(path.slice(1)), member]]
603
- : [];
604
- }));
605
- const selection = {
606
- ...link,
607
- members,
608
- call: {
609
- ...link.call,
610
- members: Object.fromEntries(Object.entries(members).map(([key, member]) => [
611
- key,
612
- member.call.name,
613
- ])),
614
- ...helper(key),
615
- type: `${link.call.type}['${key}']`,
616
- },
617
- };
618
- configs.set(id.name, selection);
619
- configBindings.set(id.start, selection);
620
- if (statement.type === 'ExportNamedDeclaration')
621
- exports[id.name] = selection;
622
- continue;
623
- }
624
- if (link.call.variableConfig && ['theme', 'themes'].includes(key))
625
- fail('Use vars for references and scope selection.', id);
626
- const member = link.members?.[JSON.stringify([key])];
627
- if (!member)
628
- fail('Destructure only style and the configured single theme; other helpers remain unsupported.', id);
629
- definitions.set(id.start, member.call);
630
- names.set(id.name, member.call);
631
- if (statement.type === 'ExportNamedDeclaration')
632
- exports[id.name] = member;
633
- }
634
- }
635
- catch (error) {
636
- if (!(error instanceof Config.InvalidError) &&
637
- !(error instanceof Vars.InvalidError))
638
- throw error;
639
- fail(error.message, expression);
640
- }
641
- continue;
642
- }
643
- let definition;
644
- let tokenType;
645
- let output;
646
- try {
647
- if (method === 'define') {
648
- if (expression.arguments.length < 1 ||
649
- expression.arguments.length > 3)
650
- fail('Vars.define requires one literal variable object.', expression);
651
- let input = data(expression.arguments[0]);
652
- tokenType = type(expression.arguments[0]);
653
- const derive = variableSet
654
- ? derivation(expression.arguments[1])
655
- : undefined;
656
- if (derive) {
657
- const base = VariableSets.build(input, Object.freeze({ variableSet: true, [Token.identity]: name }));
658
- input = VariableSets.merge(input, data(derive.body, { name: derive.name, vars: base }));
659
- tokenType = Configurations.type(input);
660
- }
661
- else if (expression.arguments.length > 2)
662
- fail('Vars.define accepts a third argument only with a derived callback.', expression);
663
- const original = variableSet
664
- ? VariableSets.theme(VariableSets.build(input, Object.freeze({ variableSet: true, [Token.identity]: name })))
665
- : Theme.define(input);
666
- definition = Token.bind(original, Object.freeze({
667
- ...original[Token.definition].contract,
668
- [Token.identity]: name,
669
- }));
670
- }
671
- else {
672
- const base = expression.arguments[0];
673
- const parent = base ? resolve(base)?.call : undefined;
674
- const derive = variableSet
675
- ? derivation(expression.arguments[2])
676
- : undefined;
677
- const arity = derive ? 3 : 2;
678
- // A trailing options object names a new set over the base's variables.
679
- const id = expression.arguments.length === arity + 1
680
- ? Identifiers.explicit(expression, arity)
681
- : undefined;
682
- const named = id !== undefined;
683
- if ((expression.arguments.length !== arity && !named) ||
684
- !parent)
685
- fail('Vars.extend requires a preceding variable set and literal overrides.', expression);
686
- factoryReferences.add(base.start);
687
- tokenType = parent.tokenType;
688
- output = parent.output;
689
- if (variableSet) {
690
- const theme = themes[parent.name];
691
- const original = VariableSets.from(theme[Token.definition]);
692
- const extend = Vars.extend;
693
- const extended = extend(original, data(expression.arguments[1]), (vars) => derive
694
- ? data(derive.body, {
695
- name: derive.name,
696
- vars: vars,
697
- })
698
- : {}, { id });
699
- definition = named
700
- ? Token.bind(VariableSets.theme(extended), Object.freeze({
701
- ...extended[Token.definition].contract,
702
- [Token.identity]: name,
703
- }))
704
- : VariableSets.theme(extended);
705
- const metadata = definition[Token.definition];
706
- // Additions grow the token contract; overridden leaves keep their base types, as in `Vars.extend`.
707
- if (named ||
708
- Object.keys(metadata.values).length !==
709
- Object.keys(theme[Token.definition].values).length ||
710
- JSON.stringify(metadata.queries) !==
711
- JSON.stringify(theme[Token.definition].queries))
712
- tokenType = Configurations.type({
713
- ...extendedValues(theme, definition),
714
- ...metadata.queries,
715
- });
716
- }
717
- else
718
- definition = Theme.extend(themes[parent.name], data(expression.arguments[1]));
719
- // Extensions share their base identity, so the binding keeps sibling sets distinct.
720
- if (!named &&
721
- definition[Token.definition].contract[Token.identity]?.startsWith('id-'))
722
- name = `${definition.className.slice('z-theme-'.length)}-${binding}`;
723
- }
724
- }
725
- catch (error) {
726
- if (error instanceof InvalidError)
727
- throw error;
728
- if (!(error instanceof Theme.InvalidError) &&
729
- !(error instanceof Vars.InvalidError))
730
- throw error;
731
- fail(error.message, expression);
732
- }
733
- const call = Object.freeze({
734
- ...(variableSet
735
- ? {
736
- variableSet: true,
737
- directVariables: true,
738
- type: `import('zyzz').Vars.Definition<${tokenType}>`,
739
- }
740
- : {}),
741
- ...(output ? { output } : {}),
742
- end: expression.end,
743
- name,
744
- start: expression.start,
745
- tokenType,
746
- ...(definition[Token.definition].contract.shorthands ||
747
- output === 'html'
748
- ? {
749
- type: `import('zyzz').Config.create.ReturnType<{theme:${tokenType};${output === 'html' ? "output:'html';" : ''}shorthands:${Configurations.type(definition[Token.definition].contract.shorthands ?? {})}}>['theme']`,
750
- }
751
- : {}),
752
- });
753
- definition = Token.bind(definition, definition[Token.definition].contract, binding);
754
- calls.push(call);
755
- definitions.set(variable.id.start, call);
756
- factories.add(expression.start);
757
- names.set(binding, call);
758
- themes[name] = definition;
759
- if (statement.type === 'ExportNamedDeclaration')
760
- exports[binding] = {
761
- binding: name,
762
- call,
763
- definition,
764
- kind: 'theme',
765
- };
766
- }
767
- }
768
- function registerAlias(input) {
769
- const { declaration, statement, variable } = input;
770
- const expression = variable.init;
771
- if (!expression)
772
- return;
773
- const linked = resolve(expression);
774
- if (linked?.kind === 'config' && variable.id.type === 'ObjectPattern') {
775
- if (statement.type === 'ExportNamedDeclaration' && !options.linked)
776
- fail('Exported configuration destructuring requires source linking.', variable);
777
- if (declaration.kind !== 'const')
778
- fail('Configuration destructuring requires const bindings.', variable);
779
- const link = linked;
780
- const bindings = variable.id.properties.map((property) => {
781
- if (property.type !== 'Property' ||
782
- property.computed ||
783
- property.key.type !== 'Identifier' ||
784
- property.value.type !== 'Identifier')
785
- return fail('Configuration destructuring requires named bindings without defaults or rest.', property);
786
- return { key: property.key.name, id: property.value };
787
- });
788
- aliasReferences.add(expression.start);
789
- aliases.push({
790
- ...link.call,
791
- start: expression.start,
792
- end: expression.end,
793
- destructured: false,
794
- retained: true,
795
- });
796
- for (const { key, id } of bindings) {
797
- if (key === 'Provider' &&
798
- link.call.nativeProvider &&
799
- !link.call.initialization &&
800
- !link.call.root &&
801
- !link.call.selection)
802
- continue;
803
- if ((key === 'style' || key === 'variants') &&
804
- !link.call.selection &&
805
- !link.call.initialization) {
806
- const alias = {
807
- ...link.call,
808
- recipe: key === 'variants',
809
- destructured: false,
810
- };
811
- aliasBindings.set(id.start, alias);
812
- aliasNames.set(id.name, alias);
813
- if (statement.type === 'ExportNamedDeclaration')
814
- exports[id.name] = {
815
- ...link,
816
- binding: `${options.namespace}-${id.name}`,
817
- call: alias,
818
- kind: 'style',
819
- };
820
- continue;
821
- }
822
- if (!link.call.selection &&
823
- !link.call.initialization &&
824
- !link.call.root &&
825
- (key === 'script' ||
826
- key === 'appearance' ||
827
- (key === 'themes' && link.call.options?.themes) ||
828
- (key === 'vars' && link.call.variableConfig))) {
829
- if ((key === 'script' && !link.call.script) ||
830
- (key === 'appearance' && !link.call.appearance) ||
831
- link.call.selection ||
832
- link.call.initialization)
833
- fail('This configuration helper is not available on the linked contract.', id);
834
- if (key === 'script')
835
- scripts.add(link.call.name);
836
- if (key === 'appearance')
837
- appearances.add(link.call.name);
838
- if (key === 'themes' || key === 'vars')
839
- selections.add(link.call.name);
840
- const members = Object.fromEntries(Object.entries(link.members ?? {}).flatMap(([pathKey, member]) => {
841
- const path = JSON.parse(pathKey);
842
- return (key === 'themes' || key === 'vars') &&
843
- (path[0] === 'themes' || path[0] === 'vars')
844
- ? [[JSON.stringify(path.slice(1)), member]]
845
- : [];
846
- }));
847
- const selection = {
848
- ...link,
849
- members,
850
- call: {
851
- ...link.call,
852
- members: Object.fromEntries(Object.entries(members).map(([key, member]) => [
853
- key,
854
- member.call.name,
855
- ])),
856
- ...helper(key),
857
- type: `${link.call.type}['${key}']`,
858
- },
859
- };
860
- configs.set(id.name, selection);
861
- configBindings.set(id.start, selection);
862
- if (statement.type === 'ExportNamedDeclaration')
863
- exports[id.name] = selection;
864
- continue;
865
- }
866
- if (link.call.variableConfig && ['theme', 'themes'].includes(key))
867
- fail('Use vars for references and scope selection.', id);
868
- const member = link.members?.[JSON.stringify([key])];
869
- if (!member)
870
- fail('Destructure only style and the configured single theme; other helpers remain unsupported.', id);
871
- definitions.set(id.start, member.call);
872
- names.set(id.name, member.call);
873
- if (statement.type === 'ExportNamedDeclaration')
874
- exports[id.name] = member;
875
- }
876
- return;
877
- }
878
- if (linked && variable.id.type === 'Identifier') {
879
- if (expression.start < linked.call.end)
880
- fail('Authoring aliases must follow their definition.', expression);
881
- if (declaration.kind !== 'const' ||
882
- (statement.type === 'ExportNamedDeclaration' && !options.linked))
883
- fail('Authoring aliases require module-level const bindings.', variable);
884
- if (linked.kind === 'config') {
885
- configs.set(variable.id.name, linked);
886
- configBindings.set(variable.id.start, linked);
887
- }
888
- else {
889
- names.set(variable.id.name, linked.call);
890
- definitions.set(variable.id.start, linked.call);
891
- }
892
- aliases.push({
893
- ...linked.call,
894
- start: expression.start,
895
- end: expression.end,
896
- destructured: false,
897
- retained: true,
898
- });
899
- aliasReferences.add(expression.start);
900
- if (statement.type === 'ExportNamedDeclaration')
901
- exports[variable.id.name] = linked;
902
- return;
903
- }
904
- const member = (() => {
905
- if (expression.type === 'MemberExpression' &&
906
- !expression.computed &&
907
- !expression.optional &&
908
- expression.property.type === 'Identifier' &&
909
- ['style', 'variants'].includes(expression.property.name)) {
910
- return expression.object;
911
- }
912
- return undefined;
913
- })();
914
- const destructured = variable.id.type === 'ObjectPattern' && expression.type === 'Identifier';
915
- const source = member ?? (expression.type === 'Identifier' ? expression : undefined);
916
- if (!source)
917
- return;
918
- const theme = (() => {
919
- if (member || destructured) {
920
- return resolve(source)?.call;
921
- }
922
- if (source.type === 'Identifier') {
923
- return aliasNames.get(source.name);
924
- }
925
- return undefined;
926
- })();
927
- if (!theme)
928
- return;
929
- const bindings = (() => {
930
- if (destructured && variable.id.type === 'ObjectPattern') {
931
- if (!variable.id.properties.length)
932
- fail('Theme destructuring requires an authoring binding.', variable.id);
933
- return variable.id.properties.map((property) => {
934
- if (property.type !== 'Property' ||
935
- property.computed ||
936
- property.key.type !== 'Identifier' ||
937
- (property.key.name !== 'style' &&
938
- property.key.name !== 'variants') ||
939
- property.value.type !== 'Identifier')
940
- fail('Destructure only style or variants into const bindings without defaults or rest properties.', property);
941
- return { id: property.value, member: property.key.name };
942
- });
943
- }
944
- if (variable.id.type !== 'Identifier')
945
- fail('Theme authoring aliases require named const bindings.', variable);
946
- const recipe = expression.type === 'MemberExpression' &&
947
- expression.property.type === 'Identifier'
948
- ? expression.property.name === 'variants'
949
- : theme.recipe;
950
- return [
951
- {
952
- id: variable.id,
953
- member: recipe ? 'variants' : 'style',
954
- },
955
- ];
956
- })();
957
- if (declaration.kind !== 'const' ||
958
- (statement.type === 'ExportNamedDeclaration' && !options.linked))
959
- fail('Theme style aliases require a local module-level const binding.', variable);
960
- if (expression.start < theme.end)
961
- fail('Theme style aliases must follow their definition.', expression);
962
- for (const [index, { id, member }] of bindings.entries()) {
963
- const alias = Object.freeze({
964
- recipe: member === 'variants',
965
- destructured,
966
- end: expression.end,
967
- name: theme.name,
968
- start: expression.start,
969
- tokenType: theme.tokenType,
970
- type: theme.type,
971
- options: theme.options,
972
- variableConfig: theme.variableConfig,
973
- variableMappings: theme.variableMappings,
974
- output: theme.output,
975
- });
976
- // Each local/exported name keeps its own callable identity, while the
977
- // shared initializer is rewritten once with every destructured member.
978
- if (index === 0)
979
- aliases.push({
980
- ...alias,
981
- bindings: bindings.map(({ member }) => member),
982
- });
983
- aliasBindings.set(id.start, alias);
984
- aliasNames.set(id.name, alias);
985
- if (statement.type === 'ExportNamedDeclaration')
986
- exports[id.name] = {
987
- binding: `${options.namespace}-${id.name}`,
988
- call: alias,
989
- definition: themes[alias.name],
990
- kind: 'style',
991
- };
992
- }
993
- aliasReferences.add(source.start);
994
- }
995
- const exportReferences = new Set();
996
- if (options.linked)
997
- for (const statement of program.body) {
998
- if (statement.type !== 'ExportNamedDeclaration' ||
999
- statement.source ||
1000
- statement.exportKind === 'type')
1001
- continue;
1002
- for (const specifier of statement.specifiers) {
1003
- if (specifier.exportKind === 'type')
1004
- continue;
1005
- const name = specifier.local.type === 'Identifier'
1006
- ? specifier.local.name
1007
- : specifier.local.value;
1008
- const call = configs.get(name)?.call ?? names.get(name) ?? aliasNames.get(name);
1009
- if (!call)
1010
- continue;
1011
- const exported = specifier.exported.type === 'Identifier'
1012
- ? specifier.exported.name
1013
- : specifier.exported.value;
1014
- exports[exported] = configs.get(name) ??
1015
- options.links?.[name] ?? {
1016
- binding: `${options.namespace}-${name}`,
1017
- call,
1018
- definition: themes[call.name],
1019
- kind: names.has(name) ? 'theme' : 'style',
1020
- };
1021
- exportReferences.add(specifier.local.start);
1022
- }
1023
- }
1024
- function reference(node, parent, ancestors, binding, lookup) {
1025
- const grandparent = ancestors.at(-3);
1026
- if (binding?.type === 'Import' &&
1027
- (imports.has(binding.node.start) ||
1028
- configImports.has(binding.node.start) ||
1029
- factoryImports.has(binding.node.start))) {
1030
- if (factoryImports.has(binding.node.start) &&
1031
- parent.type === 'CallExpression' &&
1032
- factories.has(parent.start))
1033
- return true;
1034
- if (parent.type === 'MemberExpression' &&
1035
- grandparent?.type === 'CallExpression' &&
1036
- (factories.has(grandparent.start) ||
1037
- factoryReferences.has(parent.start)))
1038
- return true;
1039
- fail('Theme factories require direct module-level const declarations; aliases and indirect references require source linking.', node);
1040
- }
1041
- const alias = binding?.type === 'Variable' || binding?.type === 'Import'
1042
- ? aliasBindings.get(binding.node.start)
1043
- : undefined;
1044
- if (alias) {
1045
- if (node.start === binding.node.start ||
1046
- exportReferences.has(node.start))
1047
- return true;
1048
- if (node.start < alias.end)
1049
- fail('Theme style alias references must follow their definition.', node);
1050
- if (aliasReferences.has(node.start))
1051
- return true;
1052
- if (parent.type !== 'CallExpression' ||
1053
- parent.callee !== node ||
1054
- parent.optional)
1055
- fail('Theme style aliases support direct calls only; exporting or escaping them requires source linking.', node);
1056
- styles.set(parent.start, {
1057
- call: parent,
1058
- recipe: alias.recipe,
1059
- theme: themes[alias.name],
1060
- output: alias.output ??
1061
- (alias.options?.output === 'html' ? 'html' : undefined),
1062
- });
1063
- return true;
1064
- }
1065
- const config = binding?.type === 'Variable' || binding?.type === 'Import'
1066
- ? configBindings.get(binding.node.start)
1067
- : undefined;
1068
- if (config) {
1069
- if (node.start === binding.node.start)
1070
- return true;
1071
- if (exportReferences.has(node.start) || aliasReferences.has(node.start))
1072
- return true;
1073
- if (node.start < config.call.end &&
1074
- config.call.start >= 0 &&
1075
- binding?.type !== 'Import')
1076
- fail('Configuration references must follow their definition.', node);
1077
- if (config.call.variableConfig &&
1078
- config.call.selection &&
1079
- readVars(node, parent, config, lookup))
1080
- return true;
1081
- // Root controls are an ordinary runtime object; any read or call is valid.
1082
- if (config.call.root) {
1083
- appearances.add(config.call.name);
1084
- return true;
1085
- }
1086
- if ((config.call.selection || config.call.initialization) &&
1087
- parent.type === 'CallExpression' &&
1088
- parent.callee === node &&
1089
- !parent.optional) {
1090
- if (config.call.selection && config.call.catalogOnly)
1091
- fail('This legacy catalog is not callable; rebuild its library.', node);
1092
- if (config.call.selection) {
1093
- selections.add(config.call.name);
1094
- selectionUses.add(config.call.name);
1095
- scopeApplications.set(parent.start, {
1096
- end: parent.end,
1097
- output: config.call.options?.output === 'html' ? 'html' : undefined,
1098
- });
1099
- }
1100
- if (config.call.initialization)
1101
- scripts.add(config.call.name);
1102
- return true;
1103
- }
1104
- let target = node;
1105
- const path = [];
1106
- for (let index = ancestors.length - 2; index >= 0; index--) {
1107
- const member = ancestors[index];
1108
- if (member.type !== 'MemberExpression' ||
1109
- member.object !== target ||
1110
- member.optional)
1111
- break;
1112
- const key = (() => {
1113
- if (member.property.type === 'Identifier' && !member.computed) {
1114
- return member.property.name;
1115
- }
1116
- if (member.property.type === 'Literal' &&
1117
- member.computed &&
1118
- (typeof member.property.value === 'string' ||
1119
- typeof member.property.value === 'number')) {
1120
- return String(member.property.value);
1121
- }
1122
- return undefined;
1123
- })();
1124
- if (key === undefined)
1125
- break;
1126
- path.push(key);
1127
- target = member;
1128
- if (path.length === 1 &&
1129
- path[0] === 'Provider' &&
1130
- config.call.nativeProvider &&
1131
- !config.call.initialization &&
1132
- !config.call.root &&
1133
- !config.call.selection)
1134
- return true;
1135
- if (config.call.variableConfig &&
1136
- path.length === 1 &&
1137
- path[0] === 'vars' &&
1138
- readVars(target, ancestors[index - 1], config, lookup))
1139
- return true;
1140
- if (aliasReferences.has(target.start) ||
1141
- factoryReferences.has(target.start))
1142
- return true;
1143
- if (path.length === 1 &&
1144
- path[0] === 'appearance' &&
1145
- !config.call.selection &&
1146
- !config.call.initialization) {
1147
- if (!config.call.appearance)
1148
- fail('This packed configuration does not provide appearance; rebuild its library with root control support.', target);
1149
- appearances.add(config.call.name);
1150
- return true;
1151
- }
1152
- if (path.length === 1 &&
1153
- !config.call.selection &&
1154
- !config.call.initialization &&
1155
- ['script', 'themes', 'vars'].includes(path[0]) &&
1156
- ancestors[index - 1]?.type === 'CallExpression' &&
1157
- ancestors[index - 1].callee === target &&
1158
- !ancestors[index - 1].optional) {
1159
- if (path[0] === 'vars' && !config.call.variableConfig)
1160
- fail('This configuration has no vars.', target);
1161
- if (path[0] === 'themes' && !config.call.options?.themes)
1162
- fail('Theme selection requires a named catalog.', target);
1163
- if ((path[0] === 'themes' || path[0] === 'vars') &&
1164
- config.call.catalogOnly)
1165
- fail('This legacy catalog is not callable; rebuild its library.', target);
1166
- if (path[0] === 'script') {
1167
- if (!config.call.script)
1168
- fail('This packed configuration does not provide script(); rebuild its library with initialization support.', target);
1169
- scripts.add(config.call.name);
1170
- }
1171
- if (path[0] === 'themes' || path[0] === 'vars') {
1172
- selections.add(config.call.name);
1173
- selectionUses.add(config.call.name);
1174
- const application = ancestors[index - 1];
1175
- scopeApplications.set(application.start, {
1176
- end: application.end,
1177
- output: config.call.options?.output === 'html' ? 'html' : undefined,
1178
- });
1179
- }
1180
- return true;
1181
- }
1182
- if (path.length === 1 &&
1183
- (path[0] === 'style' || path[0] === 'variants')) {
1184
- const call = ancestors[index - 1];
1185
- if (call?.type !== 'CallExpression' ||
1186
- call.callee !== target ||
1187
- call.optional)
1188
- break;
1189
- styles.set(call.start, {
1190
- call,
1191
- recipe: path[0] === 'variants',
1192
- theme: config.definition,
1193
- output: config.call.options?.output === 'html' ? 'html' : undefined,
1194
- });
1195
- return true;
1196
- }
1197
- if (config.call.variableConfig &&
1198
- ['theme', 'themes'].includes(path[0]))
1199
- fail('Use vars for references and scope selection.', target);
1200
- const linked = config.call.variableConfig && config.call.selection
1201
- ? undefined
1202
- : config.members?.[JSON.stringify(path)];
1203
- if (linked)
1204
- return themeReference(target, ancestors[index - 1], ancestors.slice(0, index + 1), linked.call);
1205
- }
1206
- if (config.call.selection && config.call.variableConfig && path.length)
1207
- return themeReference(node, parent, ancestors, {
1208
- ...config.call,
1209
- directVariables: true,
1210
- });
1211
- // A compiled selector is an ordinary runtime function, so passing or storing it is safe.
1212
- if (config.call.selection && !config.call.catalogOnly && !path.length) {
1213
- selections.add(config.call.name);
1214
- selectionUses.add(config.call.name);
1215
- return true;
1216
- }
1217
- fail('Use direct configuration style calls or static theme members; configurations cannot escape or be mutated.', node);
1218
- }
1219
- const theme = binding?.type === 'Variable' || binding?.type === 'Import'
1220
- ? definitions.get(binding.node.start)
1221
- : undefined;
1222
- if (!theme)
1223
- return false;
1224
- if (node.start === binding.node.start)
1225
- return true;
1226
- if (theme.variableSet &&
1227
- readVars(node, parent, {
1228
- call: theme,
1229
- definition: themes[theme.name],
1230
- }, lookup))
1231
- return true;
1232
- return themeReference(node, parent, ancestors, theme);
1233
- }
1234
- function readVars(node, parent, link, lookup) {
1235
- if (!options.native ||
1236
- parent.type !== 'CallExpression' ||
1237
- parent.optional ||
1238
- parent.arguments[0] !== node)
1239
- return false;
1240
- const callee = parent.callee;
1241
- const name = callee.type === 'Identifier'
1242
- ? callee.name
1243
- : callee.type === 'MemberExpression' &&
1244
- !callee.computed &&
1245
- !callee.optional &&
1246
- callee.object.type === 'Identifier' &&
1247
- callee.property.type === 'Identifier' &&
1248
- ['useAnimatedVars', 'useVars'].includes(callee.property.name)
1249
- ? callee.object.name
1250
- : undefined;
1251
- const binding = name === undefined ? undefined : lookup(name);
1252
- if (binding?.type !== 'Import' ||
1253
- ![
1254
- 'zyzz/react-native',
1255
- 'zyzz/react-native/reanimated',
1256
- 'zyzz/react-native/react',
1257
- ].includes(binding.importNode.source.value) ||
1258
- binding.importNode.importKind === 'type')
1259
- return false;
1260
- const specifier = binding.node;
1261
- if (callee.type === 'Identifier'
1262
- ? specifier.type !== 'ImportSpecifier' ||
1263
- specifier.importKind === 'type' ||
1264
- !['useAnimatedVars', 'useVars'].includes(specifier.imported.type === 'Identifier'
1265
- ? specifier.imported.name
1266
- : specifier.imported.value)
1267
- : specifier.type !== 'ImportNamespaceSpecifier')
1268
- return false;
1269
- varsReads.push({ ...varsRead(link), end: node.end, start: node.start });
1270
- return true;
1271
- }
1272
- function themeReference(node, parent, ancestors, theme) {
1273
- const grandparent = ancestors.at(-3);
1274
- if (factoryReferences.has(node.start) ||
1275
- ancestors.some((ancestor) => factoryReferences.has(ancestor.start)))
1276
- return true;
1277
- if (exportReferences.has(node.start))
1278
- return true;
1279
- if (aliasReferences.has(node.start))
1280
- return true;
1281
- if (node.start < theme.end)
1282
- fail('Theme references must follow their local definition.', node);
1283
- if (parent.type === 'CallExpression' &&
1284
- factories.has(parent.start) &&
1285
- parent.arguments[0] === node)
1286
- return true;
1287
- if (parent.type === 'MemberExpression' &&
1288
- parent.object === node &&
1289
- (theme.directVariables ||
1290
- (parent.property.type === 'Identifier' &&
1291
- !parent.computed &&
1292
- ['tokens', 'vars'].includes(parent.property.name)) ||
1293
- (parent.property.type === 'Literal' &&
1294
- parent.computed &&
1295
- ['tokens', 'vars'].includes(String(parent.property.value))))) {
1296
- if (parent.optional)
1297
- fail('Token references cannot use optional access.', parent);
1298
- const variable = theme.directVariables ||
1299
- (parent.property.type === 'Identifier'
1300
- ? parent.property.name === 'vars'
1301
- : parent.property.type === 'Literal' &&
1302
- parent.property.value === 'vars');
1303
- let value = variable && !theme.directVariables
1304
- ? themes[theme.name].vars
1305
- : themes[theme.name].tokens;
1306
- let target = theme.directVariables ? node : parent;
1307
- let index = ancestors.length - (theme.directVariables ? 2 : 3);
1308
- for (; index >= 0; index--) {
1309
- const ancestor = ancestors[index];
1310
- if (ancestor.type !== 'MemberExpression' || ancestor.object !== target)
1311
- break;
1312
- const key = (() => {
1313
- if (ancestor.property.type === 'Identifier' && !ancestor.computed) {
1314
- return ancestor.property.name;
1315
- }
1316
- if (ancestor.property.type === 'Literal' &&
1317
- ancestor.computed &&
1318
- (typeof ancestor.property.value === 'string' ||
1319
- typeof ancestor.property.value === 'number')) {
1320
- return String(ancestor.property.value);
1321
- }
1322
- return undefined;
1323
- })();
1324
- if (ancestor.optional || key === undefined)
1325
- fail('Token paths require static property names without optional access.', ancestor);
1326
- value =
1327
- value && typeof value === 'object' && !Token.is(value)
1328
- ? Object.getOwnPropertyDescriptor(value, key)?.value
1329
- : undefined;
1330
- if (value === undefined)
1331
- fail('Unknown theme token path.', ancestor);
1332
- target = ancestor;
1333
- }
1334
- if (!Token.is(value))
1335
- fail('Expected a scalar theme token reference.', target);
1336
- const reference = value;
1337
- for (; index >= 0; index--) {
1338
- const ancestor = ancestors[index];
1339
- if ((ancestor.type === 'TSAsExpression' ||
1340
- ancestor.type === 'TSSatisfiesExpression' ||
1341
- ancestor.type === 'TSNonNullExpression' ||
1342
- ancestor.type === 'TSTypeAssertion') &&
1343
- ancestor.expression === target)
1344
- target = ancestor;
1345
- else
1346
- break;
1347
- }
1348
- const valueTarget = target;
1349
- if (variable) {
1350
- while (index >= 0) {
1351
- const ancestor = ancestors[index];
1352
- if (!((ancestor.type === 'TemplateLiteral' &&
1353
- ancestor.expressions.includes(target)) ||
1354
- ((ancestor.type === 'TSAsExpression' ||
1355
- ancestor.type === 'TSSatisfiesExpression' ||
1356
- ancestor.type === 'TSNonNullExpression' ||
1357
- ancestor.type === 'TSTypeAssertion') &&
1358
- ancestor.expression === target)))
1359
- break;
1360
- target = ancestor;
1361
- index--;
1362
- }
1363
- }
1364
- const array = ancestors[index];
1365
- if (array?.type === 'ArrayExpression' &&
1366
- array.elements.includes(target)) {
1367
- target = array;
1368
- index--;
1369
- for (; index >= 0; index--) {
1370
- const ancestor = ancestors[index];
1371
- if ((ancestor.type === 'TSAsExpression' ||
1372
- ancestor.type === 'TSSatisfiesExpression' ||
1373
- ancestor.type === 'TSNonNullExpression' ||
1374
- ancestor.type === 'TSTypeAssertion') &&
1375
- ancestor.expression === target)
1376
- target = ancestor;
1377
- else
1378
- break;
1379
- }
1380
- }
1381
- const property = ancestors[index];
1382
- const object = ancestors[index - 1];
1383
- let argument = object;
1384
- let callIndex = index - 2;
1385
- for (; callIndex >= 0; callIndex--) {
1386
- const ancestor = ancestors[callIndex];
1387
- if (ancestor.type === 'Property' &&
1388
- ancestor.value === argument &&
1389
- ancestors[callIndex - 1]?.type === 'ObjectExpression') {
1390
- argument = ancestors[--callIndex];
1391
- continue;
1392
- }
1393
- if ((ancestor.type === 'TSAsExpression' ||
1394
- ancestor.type === 'TSSatisfiesExpression' ||
1395
- ancestor.type === 'TSNonNullExpression' ||
1396
- ancestor.type === 'TSTypeAssertion') &&
1397
- ancestor.expression === argument)
1398
- argument = ancestor;
1399
- else
1400
- break;
1401
- }
1402
- if (ancestors[callIndex]?.type === 'ArrowFunctionExpression' &&
1403
- ancestors[callIndex].body === argument) {
1404
- argument = ancestors[callIndex];
1405
- callIndex--;
1406
- }
1407
- while (callIndex >= 0) {
1408
- const wrapper = ancestors[callIndex];
1409
- if ((wrapper.type === 'TSAsExpression' ||
1410
- wrapper.type === 'TSSatisfiesExpression' ||
1411
- wrapper.type === 'TSNonNullExpression' ||
1412
- wrapper.type === 'TSTypeAssertion') &&
1413
- wrapper.expression === argument) {
1414
- argument = wrapper;
1415
- callIndex--;
1416
- }
1417
- else
1418
- break;
1419
- }
1420
- const call = ancestors[callIndex];
1421
- if (property?.type !== 'Property' ||
1422
- property.value !== target ||
1423
- object?.type !== 'ObjectExpression' ||
1424
- call?.type !== 'CallExpression' ||
1425
- call.arguments[0] !== argument ||
1426
- (!styles.has(call.start) && !options.contributionCalls?.has(call.start))) {
1427
- if (ancestors.some((node) => node.type === 'VariableDeclarator' &&
1428
- options.staticBindings?.has(node.start)))
1429
- staticTokens.push(valueTarget);
1430
- else
1431
- fail('Token references must be direct property values in bound theme style calls.', target);
1432
- }
1433
- tokens.set(valueTarget.start, { end: valueTarget.end, reference });
1434
- const unwrapped = Expression.unwrap(valueTarget);
1435
- tokens.set(unwrapped.start, { end: unwrapped.end, reference });
1436
- return true;
1437
- }
1438
- if (parent.type !== 'MemberExpression' ||
1439
- parent.object !== node ||
1440
- parent.computed ||
1441
- parent.optional ||
1442
- parent.property.type !== 'Identifier')
1443
- fail('Use Config.create or Vars.extend with variable definitions; other references require source linking.', node);
1444
- if (['style', 'variants'].includes(parent.property.name) &&
1445
- grandparent?.type === 'CallExpression' &&
1446
- grandparent.callee === parent &&
1447
- !grandparent.optional) {
1448
- styles.set(grandparent.start, {
1449
- call: grandparent,
1450
- recipe: parent.property.name === 'variants',
1451
- theme: themes[theme.name],
1452
- output: theme.output,
1453
- });
1454
- return true;
1455
- }
1456
- if (parent.property.name !== 'className')
1457
- fail('Only direct theme.style calls and theme.className reads are supported here.', parent);
1458
- let target = parent;
1459
- for (let index = ancestors.length - 3; index >= 0; index--) {
1460
- const ancestor = ancestors[index];
1461
- if ((ancestor.type === 'AssignmentExpression' &&
1462
- ancestor.left === target) ||
1463
- (ancestor.type === 'UpdateExpression' &&
1464
- ancestor.argument === target) ||
1465
- (ancestor.type === 'UnaryExpression' &&
1466
- ancestor.operator === 'delete') ||
1467
- ((ancestor.type === 'ForInStatement' ||
1468
- ancestor.type === 'ForOfStatement') &&
1469
- ancestor.left === target))
1470
- fail('Theme scope properties cannot be reassigned.', ancestor);
1471
- if (((ancestor.type === 'TSAsExpression' ||
1472
- ancestor.type === 'TSNonNullExpression' ||
1473
- ancestor.type === 'TSSatisfiesExpression' ||
1474
- ancestor.type === 'TSTypeAssertion') &&
1475
- ancestor.expression === target) ||
1476
- (ancestor.type === 'Property' && ancestor.value === target) ||
1477
- ancestor.type === 'ObjectPattern' ||
1478
- ancestor.type === 'ArrayPattern' ||
1479
- (ancestor.type === 'RestElement' && ancestor.argument === target) ||
1480
- (ancestor.type === 'AssignmentPattern' && ancestor.left === target))
1481
- target = ancestor;
1482
- else
1483
- break;
1484
- }
1485
- references.push(Object.freeze({ end: parent.end, name: theme.name, start: parent.start }));
1486
- return true;
1487
- }
1488
- for (const link of Object.values(exports))
1489
- if (link.kind === 'config' &&
1490
- !link.call.selection &&
1491
- !link.call.initialization &&
1492
- !link.call.root) {
1493
- if (link.call.script)
1494
- scripts.add(link.call.name);
1495
- if (link.call.appearance)
1496
- appearances.add(link.call.name);
1497
- }
1498
- const nativeContexts = new Map();
1499
- return {
1500
- nativeContext(start, end) {
1501
- const theme = styles.get(start)?.theme;
1502
- const contract = theme?.[Token.definition].contract ??
1503
- [...tokens].find(([offset]) => offset >= start && offset < end)?.[1]
1504
- .reference.contract;
1505
- if (!contract)
1506
- return undefined;
1507
- const cached = nativeContexts.get(contract);
1508
- if (cached)
1509
- return cached;
1510
- const config = [...configs.values()].find((value) => value.definition[Token.definition].contract === contract);
1511
- if (!config?.call.options?.themes) {
1512
- const alias = [...aliasNames.values()].find((value) => themes[value.name]?.[Token.definition].contract === contract &&
1513
- value.options?.themes);
1514
- if (!alias?.options?.themes)
1515
- return undefined;
1516
- const context = {
1517
- defaultVars: String(alias.options.defaultTheme),
1518
- vars: Object.fromEntries(Object.entries(alias.options.themes).map(([name, values]) => [
1519
- name,
1520
- Token.bind(contract.variableSet
1521
- ? VariableSets.theme(Vars.define(values))
1522
- : Theme.define(values), contract),
1523
- ])),
1524
- };
1525
- Object.freeze(context.vars);
1526
- nativeContexts.set(contract, Object.freeze(context));
1527
- return context;
1528
- }
1529
- const entries = Object.entries(config.members ?? {}).flatMap(([key, value]) => {
1530
- const path = JSON.parse(key);
1531
- if (config.call.selection && path.length === 1)
1532
- return [[path[0], value.definition]];
1533
- return (path[0] === 'themes' || path[0] === 'vars') &&
1534
- path.length === 2
1535
- ? [[path[1], value.definition]]
1536
- : [];
1537
- });
1538
- const context = Object.freeze({
1539
- defaultVars: String(config.call.options.defaultTheme),
1540
- vars: Object.freeze(Object.fromEntries(entries)),
1541
- });
1542
- nativeContexts.set(contract, context);
1543
- return context;
1544
- },
1545
- aliases,
1546
- appearances,
1547
- calls,
1548
- exports: Object.freeze(exports),
1549
- reference,
1550
- references,
1551
- scopeApplications,
1552
- scripts,
1553
- selections,
1554
- selectionUses,
1555
- staticTokens,
1556
- styles,
1557
- themes: Object.freeze(themes),
1558
- tokens,
1559
- varsReads,
1560
- };
1561
- }
1562
- /** Located failure from unsupported or invalid local theme syntax. */
1563
- export class InvalidError extends Error {
1564
- /** Retains a source span for the public extractor diagnostic. */
1565
- constructor(message, node) {
1566
- super(message);
1567
- this.end = node.end;
1568
- this.start = node.start;
1569
- }
1570
- /** Exclusive source offset. */
1571
- end;
1572
- /** Namespaced diagnostic identity. */
1573
- name = 'Source.ThemeInvalidError';
1574
- /** Inclusive source offset. */
1575
- start;
1576
- }
1577
- //# sourceMappingURL=Themes.js.map