@astryxdesign/cli 0.7.5-canary.fccd771 → 0.7.5

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 (117) hide show
  1. package/CHANGELOG.md +91 -3
  2. package/README.md +2 -2
  3. package/api/hook/detail/detail.test.mjs +11 -0
  4. package/api/integration/pack-check.lifecycle-output.test.mjs +24 -1
  5. package/api/integration/pack-check.mjs +10 -4
  6. package/api/integration/pack-check.test.mjs +5 -1
  7. package/api/swizzle/copy/copy.mjs +1 -1
  8. package/api/template/tokenizer-showcase.test.mjs +47 -0
  9. package/api/theme/build/build.adaptation-enrollment.test.mjs +4 -0
  10. package/api/theme/build/build.icon-entry-variants.test.mjs +134 -0
  11. package/api/theme/build/build.icon-function-entries.test.mjs +499 -0
  12. package/api/theme/build/build.icon-variants.test.mjs +105 -0
  13. package/api/theme/build/build.mjs +371 -113
  14. package/api/theme/build/build.project-core.test.mjs +63 -8
  15. package/api/theme/build/build.test.mjs +77 -0
  16. package/api/theme/build/icon-imports.d.mts +12 -2
  17. package/api/theme/build/icon-imports.mjs +36 -11
  18. package/api/upgrade/_adapter.d.mts +47 -2
  19. package/api/upgrade/_adapter.mjs +58 -2
  20. package/api/upgrade/run/failure-receipt.test.mjs +212 -0
  21. package/api/upgrade/run/outcomes.test.mjs +646 -0
  22. package/api/upgrade/run/run.d.mts +36 -0
  23. package/api/upgrade/run/run.mjs +670 -44
  24. package/api/upgrade/upgrade.doc.mjs +4 -4
  25. package/api/upgrade/upgrade.mjs +19 -4
  26. package/api/upgrade/upgrade.type.d.mts +36 -1
  27. package/api/upgrade/upgrade.type.mjs +29 -4
  28. package/assets/codemods/__tests__/registry.test.mjs +1 -0
  29. package/assets/codemods/__tests__/runner.symlinks.test.mjs +205 -0
  30. package/assets/codemods/__tests__/runner.unparseable.test.mjs +457 -0
  31. package/assets/codemods/integration-runner.mjs +8 -2
  32. package/assets/codemods/integration-runner.protection.test.mjs +53 -0
  33. package/assets/codemods/outcome.mjs +79 -0
  34. package/assets/codemods/outcome.test.mjs +40 -0
  35. package/assets/codemods/parse.mjs +94 -0
  36. package/assets/codemods/registry.mjs +1 -0
  37. package/assets/codemods/run-codemod.mjs +109 -5
  38. package/assets/codemods/runner.mjs +119 -13
  39. package/assets/codemods/transform-prop.mjs +99 -49
  40. package/assets/codemods/transform-prop.test.mjs +36 -1
  41. package/assets/codemods/transforms/v0.1.2/rename-text-color-active-to-accent.mjs +1 -1
  42. package/assets/codemods/transforms/v0.1.3/__tests__/migrate-layout-components-to-experimental.test.mjs +216 -2
  43. package/assets/codemods/transforms/v0.1.3/migrate-layout-components-to-experimental.mjs +88 -6
  44. package/assets/codemods/transforms/v0.1.5/rename-switch-label-spacing-default-to-hug.mjs +1 -1
  45. package/assets/codemods/transforms/v0.1.7/migrate-table-tableprops-to-direct-props.mjs +1 -1
  46. package/assets/codemods/transforms/v0.1.8/rename-avatar-size-scale.mjs +1 -1
  47. package/assets/codemods/transforms/v0.2.0/remove-tablist-orientation.mjs +1 -1
  48. package/assets/codemods/transforms/v0.2.1/migrate-dialog-position-to-logical.mjs +1 -1
  49. package/assets/codemods/transforms/v0.6.0/migrate-astryx-theme-selectors-to-data-attrs.mjs +4 -3
  50. package/assets/codemods/transforms/v0.7.0/__tests__/migrate-deprecated-theme-surface.test.mjs +207 -6
  51. package/assets/codemods/transforms/v0.7.0/__tests__/validate-data-token-ownership.test.mjs +143 -70
  52. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface-v0.6-data.mjs +6 -1
  53. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +244 -42
  54. package/assets/codemods/transforms/v0.7.0/validate-data-token-ownership.mjs +23 -78
  55. package/assets/codemods/transforms/v0.7.5/__tests__/migrate-codeblock-tokenizer-to-language.test.mjs +130 -0
  56. package/assets/codemods/transforms/v0.7.5/index.mjs +20 -0
  57. package/assets/codemods/transforms/v0.7.5/migrate-codeblock-tokenizer-to-language.mjs +315 -0
  58. package/assets/docs/README.md +13 -13
  59. package/assets/docs/author-a-theme.doc.mjs +5 -1
  60. package/assets/docs/getting-started.doc.mjs +5 -5
  61. package/assets/docs/icons.doc.mjs +82 -1
  62. package/assets/docs/motion.doc.mjs +1 -1
  63. package/assets/docs/theme.doc.dense.mjs +8 -0
  64. package/assets/docs/theme.doc.mjs +16 -0
  65. package/assets/docs/theme.doc.zh.mjs +11 -0
  66. package/assets/docs/tree/i18n-library-integration.doc.mjs +1 -1
  67. package/assets/docs/tree/styling-advanced.doc.mjs +1 -1
  68. package/assets/docs/tree/template-styles.doc.mjs +1 -1
  69. package/assets/docs/tree/tokens-color.doc.mjs +145 -1
  70. package/assets/docs/tree/tokens-ease.doc.mjs +4 -0
  71. package/assets/docs/tree/tokens.doc.mjs +1 -1
  72. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.doc.mjs +6 -4
  73. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherReviewFlow.tsx +61 -55
  74. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.doc.mjs +6 -4
  75. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.tsx +68 -37
  76. package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.doc.mjs +1 -1
  77. package/assets/templates/blocks/components/CodeBlock/CodeBlockBashCommand.tsx +10 -2
  78. package/assets/templates/blocks/components/Thumbnail/ThumbnailDisabled.doc.mjs +1 -1
  79. package/assets/templates/blocks/components/Tokenizer/TokenizerShowcase.tsx +13 -6
  80. package/assets/templates/pages/dashboard-alert-rail/page.tsx +3 -3
  81. package/assets/templates/pages/library/page.tsx +1 -2
  82. package/assets/templates/pages/table-comparison/page.tsx +12 -3
  83. package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +3 -2
  84. package/assets/templates/themes/butter/butterTheme.ts +47 -44
  85. package/assets/templates/themes/chocolate/chocolateTheme.ts +15 -2
  86. package/assets/templates/themes/gothic/gothicTheme.ts +21 -16
  87. package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +1 -1
  88. package/assets/templates/themes/matcha/matchaTheme.ts +15 -2
  89. package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +4 -4
  90. package/assets/templates/themes/neutral/neutralTheme.ts +24 -119
  91. package/assets/templates/themes/stone/stoneTheme.ts +46 -21
  92. package/assets/templates/themes/y2k/y2kTheme.ts +20 -16
  93. package/assets/theme.template.ts +19 -6
  94. package/authoring/codemod/type.ts +10 -0
  95. package/authoring/config/config.doc.mjs +1 -1
  96. package/clients/cli/commands/build-theme.adaptations.test.mjs +110 -0
  97. package/clients/cli/commands/build-theme.outside-project.test.mjs +140 -0
  98. package/clients/cli/commands/swizzle.routing.test.mjs +1 -1
  99. package/clients/cli/commands/upgrade.doc.mjs +47 -2
  100. package/clients/cli/commands/upgrade.file-protection.test.mjs +175 -0
  101. package/clients/cli/commands/upgrade.flag-help.test.mjs +37 -0
  102. package/clients/cli/commands/upgrade.integration-policy.test.mjs +103 -0
  103. package/clients/cli/commands/upgrade.mjs +9 -2
  104. package/clients/cli/commands/upgrade.outcomes.test.mjs +350 -0
  105. package/foundation/agent-docs/agent-docs.mjs +1 -0
  106. package/foundation/agent-docs/agent-docs.test.mjs +5 -0
  107. package/foundation/config/project.d.mts +1 -1
  108. package/foundation/config/project.mjs +1 -1
  109. package/foundation/debug/redact.test.mjs +1 -1
  110. package/foundation/doc-compiler/doc-loads.test.mjs +2 -6
  111. package/foundation/response/base.d.ts +7 -0
  112. package/foundation/response/error-codes.doc.mjs +3 -2
  113. package/foundation/response/error-codes.mjs +1 -1
  114. package/foundation/response/json.d.mts +10 -3
  115. package/foundation/response/json.mjs +16 -4
  116. package/foundation/response/response-types.doc.mjs +1 -1
  117. package/package.json +13 -12
@@ -0,0 +1,499 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Built themes carry function icon entries and resolve them as runtime.
5
+ * @input Real themeBuild and themeBuildFamily calls on source themes whose icon
6
+ * registries hold functions of `{size, variant}` and that list icon variants:
7
+ * a child under a replaced list, a theme extending a built module, a theme
8
+ * extending a package theme, and a family.
9
+ * @output Native ESM loads that render `<Icon>` for every request a theme's
10
+ * entries can receive under the built and the runtime theme, read the public
11
+ * icon views, and a TypeScript program that extends a built theme.
12
+ * @position `architecture:theme-compilation` INV15 and `spec:AST-087` FR3,
13
+ * FR18, and FR19 for theme build. The node project's global setup supplies
14
+ * the compiled core package.
15
+ */
16
+
17
+ import {afterEach, beforeEach, describe, expect, it} from 'vitest';
18
+ import {execFileSync} from 'node:child_process';
19
+ import * as fs from 'node:fs';
20
+ import * as path from 'node:path';
21
+ import ts from 'typescript';
22
+ import {themeBuild, themeBuildFamily} from './build.mjs';
23
+
24
+ const SIZES = ['xsm', 'sm', 'md', 'lg'];
25
+
26
+ let tmpDir;
27
+ beforeEach(() => {
28
+ // Under the CLI package, so core, React, and TypeScript resolve both in the
29
+ // theme loader and in the native artifact checks.
30
+ tmpDir = fs.mkdtempSync(
31
+ path.resolve(import.meta.dirname, '../../..', '.tmp-icon-functions-'),
32
+ );
33
+ });
34
+ afterEach(() => {
35
+ fs.rmSync(tmpDir, {recursive: true, force: true});
36
+ });
37
+
38
+ /** @param {string} file @param {string} source */
39
+ function write(file, source) {
40
+ fs.mkdirSync(path.dirname(path.join(tmpDir, file)), {recursive: true});
41
+ fs.writeFileSync(path.join(tmpDir, file), source);
42
+ }
43
+
44
+ /** A registry module whose function entries label what they were asked for. */
45
+ function writeRegistry(file, exportName, entries) {
46
+ write(
47
+ file,
48
+ `import {createElement} from 'react';
49
+ const draw = label => ({size, variant}) =>
50
+ createElement('svg', {'data-icon': label + ':' + size + ':' + (variant ?? 'normal')});
51
+ export const ${exportName} = {
52
+ ${Object.entries(entries)
53
+ .map(([key, label]) =>
54
+ label.startsWith('node:')
55
+ ? ` ${JSON.stringify(key)}: createElement('svg', {'data-icon': ${JSON.stringify(label.slice(5))}}),`
56
+ : ` ${JSON.stringify(key)}: draw(${JSON.stringify(label)}),`,
57
+ )
58
+ .join('\n')}
59
+ };
60
+ `,
61
+ );
62
+ }
63
+
64
+ /** The base, its child under a replaced list, and their registries. */
65
+ function writeLineage() {
66
+ writeRegistry('base/icons.mjs', 'baseIcons', {
67
+ info: 'base-info',
68
+ close: 'base-close',
69
+ check: 'node:base-check',
70
+ 'parity:mark': 'base-mark',
71
+ });
72
+ write(
73
+ 'base/source.mjs',
74
+ `import {defineTheme} from '@astryxdesign/core/theme';
75
+ import {baseIcons} from './icons.mjs';
76
+ export const baseTheme = defineTheme({
77
+ name: 'parity-base', tokens: {}, iconVariants: ['outline', 'filled'], icons: baseIcons,
78
+ });
79
+ export default baseTheme;
80
+ `,
81
+ );
82
+ writeRegistry('child/icons.mjs', 'childIcons', {
83
+ close: 'child-close',
84
+ menu: 'child-menu',
85
+ });
86
+ write(
87
+ 'child/source.mjs',
88
+ `import {defineTheme} from '@astryxdesign/core/theme';
89
+ import {baseTheme} from '../base/source.mjs';
90
+ import {childIcons} from './icons.mjs';
91
+ export const childTheme = defineTheme({
92
+ name: 'parity-child', extends: baseTheme, tokens: {},
93
+ iconVariants: ['filled', 'bold'], icons: childIcons,
94
+ });
95
+ export default childTheme;
96
+ `,
97
+ );
98
+ }
99
+
100
+ /** @param {string} file */
101
+ async function buildAndCheck(file) {
102
+ const built = await themeBuild(file, {}, {cwd: tmpDir});
103
+ expect(built?.type).toBe('theme.build');
104
+ const checked = await themeBuild(file, {check: true}, {cwd: tmpDir});
105
+ expect(checked?.data.upToDate).toBe(true);
106
+ return built;
107
+ }
108
+
109
+ /**
110
+ * Load built and runtime themes with native Node and report, for each pair,
111
+ * what `<Icon>` renders for every request and what the public reads return.
112
+ * The requests are every key a theme draws × the four sizes × no variant and
113
+ * each name any theme here lists, so unlisted names are covered too.
114
+ *
115
+ * @param {Array<{label: string, built: string, runtime: string}>} pairs -
116
+ * Module specifiers with `#export` suffixes, relative to the fixture root.
117
+ */
118
+ function inspect(pairs) {
119
+ const load = specifier => {
120
+ const [file, name] = specifier.split('#');
121
+ return `(await import(${JSON.stringify(file)}))[${JSON.stringify(name)}]`;
122
+ };
123
+ const output = execFileSync(
124
+ process.execPath,
125
+ [
126
+ '--input-type=module',
127
+ '--eval',
128
+ `import {createElement, isValidElement} from 'react';
129
+ import {renderToStaticMarkup} from 'react-dom/server';
130
+ import {Icon, getIcon, getIconRegistry} from '@astryxdesign/core/Icon';
131
+ import {Theme} from '@astryxdesign/core/theme';
132
+ const SIZES = ${JSON.stringify(SIZES)};
133
+ const VARIANTS = [undefined, 'outline', 'filled', 'bold'];
134
+ const label = node => {
135
+ if (node == null) return null;
136
+ if (!isValidElement(node)) return 'not-a-node:' + typeof node;
137
+ return node.props['data-icon'];
138
+ };
139
+ const rendered = (theme, icon, size, variant) => {
140
+ const html = renderToStaticMarkup(
141
+ createElement(Theme, {theme}, createElement(Icon, {icon, size, variant})),
142
+ );
143
+ return /data-icon="([^"]+)"/.exec(html)?.[1] ?? null;
144
+ };
145
+ const describe = theme => {
146
+ const keys = Object.keys(theme.icons ?? {}).sort();
147
+ const requests = {};
148
+ for (const key of keys) {
149
+ for (const size of SIZES) {
150
+ for (const variant of VARIANTS) {
151
+ requests[key + '|' + size + '|' + (variant ?? '-')] = rendered(theme, key, size, variant);
152
+ }
153
+ }
154
+ }
155
+ return {
156
+ keys,
157
+ requests,
158
+ icons: Object.fromEntries(keys.map(key => [key, label(theme.icons[key])])),
159
+ getIcon: Object.fromEntries(keys.map(key => [key, label(getIcon(key, theme))])),
160
+ registry: Object.fromEntries(
161
+ Object.entries(getIconRegistry(theme))
162
+ .filter(([key]) => keys.includes(key))
163
+ .map(([key, node]) => [key, label(node)]),
164
+ ),
165
+ iconVariants: theme.iconVariants ?? null,
166
+ };
167
+ };
168
+ const result = {};
169
+ ${pairs
170
+ .map(
171
+ ({label, built, runtime}) => `{
172
+ const built = ${load(built)};
173
+ const runtime = ${load(runtime)};
174
+ const mod = await import(${JSON.stringify(built.split('#')[0])});
175
+ const reexports = Object.entries(mod).filter(([name]) => name !== ${JSON.stringify(built.split('#')[1])});
176
+ result[${JSON.stringify(label)}] = {
177
+ built: describe(built),
178
+ runtime: describe(runtime),
179
+ reexportIsNodeView: reexports.length === 0 ? null : reexports.every(([, value]) => value === built.icons),
180
+ };
181
+ }`,
182
+ )
183
+ .join('\n')}
184
+ process.stdout.write(JSON.stringify(result));`,
185
+ ],
186
+ {cwd: tmpDir, encoding: 'utf8', timeout: 60000},
187
+ );
188
+ return JSON.parse(output);
189
+ }
190
+
191
+ describe('built themes keep function icon entries', () => {
192
+ it('resolve every request as the runtime theme does, including an inherited entry under a replaced list', async () => {
193
+ writeLineage();
194
+ await buildAndCheck('base/source.mjs');
195
+ await buildAndCheck('child/source.mjs');
196
+ // A grandchild extends the BUILT child module.
197
+ write(
198
+ 'grandchild/source.mjs',
199
+ `import {defineTheme} from '@astryxdesign/core/theme';
200
+ import {parityChildTheme} from '../child/parity-child.js';
201
+ export default defineTheme({name: 'parity-grandchild', extends: parityChildTheme, tokens: {}});
202
+ `,
203
+ );
204
+ await buildAndCheck('grandchild/source.mjs');
205
+
206
+ const result = inspect([
207
+ {
208
+ label: 'base',
209
+ built: './base/parity-base.js#parityBaseTheme',
210
+ runtime: './base/source.mjs#default',
211
+ },
212
+ {
213
+ label: 'child',
214
+ built: './child/parity-child.js#parityChildTheme',
215
+ runtime: './child/source.mjs#default',
216
+ },
217
+ {
218
+ label: 'grandchild',
219
+ built: './grandchild/parity-grandchild.js#parityGrandchildTheme',
220
+ runtime: './grandchild/source.mjs#default',
221
+ },
222
+ ]);
223
+
224
+ for (const {built, runtime, reexportIsNodeView} of Object.values(result)) {
225
+ // INV15: built resolution and public views match runtime.
226
+ expect(built).toEqual(runtime);
227
+ expect(reexportIsNodeView).not.toBe(false);
228
+ }
229
+
230
+ const child = result.child.built;
231
+ expect(child.keys).toEqual([
232
+ 'check',
233
+ 'close',
234
+ 'info',
235
+ 'menu',
236
+ 'parity:mark',
237
+ ]);
238
+ for (const size of SIZES) {
239
+ // The child's own entry receives the names the child lists.
240
+ expect(child.requests[`close|${size}|bold`]).toBe(
241
+ `child-close:${size}:bold`,
242
+ );
243
+ expect(child.requests[`close|${size}|filled`]).toBe(
244
+ `child-close:${size}:filled`,
245
+ );
246
+ expect(child.requests[`close|${size}|outline`]).toBe(
247
+ `child-close:${size}:normal`,
248
+ );
249
+ // The inherited entry keeps its defining theme's list: 'outline' is no
250
+ // longer listed by the child, and the base never listed 'bold'.
251
+ expect(child.requests[`info|${size}|filled`]).toBe(
252
+ `base-info:${size}:filled`,
253
+ );
254
+ expect(child.requests[`info|${size}|outline`]).toBe(
255
+ `base-info:${size}:normal`,
256
+ );
257
+ expect(child.requests[`info|${size}|bold`]).toBe(
258
+ `base-info:${size}:normal`,
259
+ );
260
+ expect(child.requests[`info|${size}|-`]).toBe(`base-info:${size}:normal`);
261
+ // A node entry renders the same node for every request.
262
+ expect(child.requests[`check|${size}|filled`]).toBe('base-check');
263
+ }
264
+ expect(result.grandchild.built.requests).toEqual(child.requests);
265
+
266
+ // FR18: public reads are React nodes, each function read at md.
267
+ expect(child.icons).toEqual({
268
+ check: 'base-check',
269
+ close: 'child-close:md:normal',
270
+ info: 'base-info:md:normal',
271
+ menu: 'child-menu:md:normal',
272
+ 'parity:mark': 'base-mark:md:normal',
273
+ });
274
+ expect(child.getIcon).toEqual(child.icons);
275
+ expect(child.registry).toEqual({
276
+ check: 'base-check',
277
+ close: 'child-close:md:normal',
278
+ info: 'base-info:md:normal',
279
+ menu: 'child-menu:md:normal',
280
+ });
281
+ expect(result.base.reexportIsNodeView).toBe(true);
282
+ expect(result.child.reexportIsNodeView).toBe(true);
283
+ });
284
+
285
+ it('keeps the function entries of a package theme it extends', async () => {
286
+ const pkg = 'node_modules/@fixture/function-icons';
287
+ write(
288
+ `${pkg}/package.json`,
289
+ JSON.stringify({
290
+ name: '@fixture/function-icons',
291
+ type: 'module',
292
+ exports: './index.mjs',
293
+ }),
294
+ );
295
+ writeRegistry(`${pkg}/icons.mjs`, 'packageIcons', {info: 'package-info'});
296
+ write(
297
+ `${pkg}/index.mjs`,
298
+ `import {defineTheme} from '@astryxdesign/core/theme';
299
+ import {packageIcons} from './icons.mjs';
300
+ export const packageTheme = defineTheme({
301
+ name: 'parity-package', tokens: {}, iconVariants: ['filled'], icons: packageIcons,
302
+ });
303
+ `,
304
+ );
305
+ write(
306
+ 'package-child.mjs',
307
+ `import {defineTheme} from '@astryxdesign/core/theme';
308
+ import {packageTheme} from '@fixture/function-icons';
309
+ export default defineTheme({name: 'parity-package-child', extends: packageTheme, tokens: {}});
310
+ `,
311
+ );
312
+ await buildAndCheck('package-child.mjs');
313
+
314
+ const {packageChild} = inspect([
315
+ {
316
+ label: 'packageChild',
317
+ built: './parity-package-child.js#parityPackageChildTheme',
318
+ runtime: './package-child.mjs#default',
319
+ },
320
+ ]);
321
+
322
+ expect(packageChild.built).toEqual(packageChild.runtime);
323
+ for (const size of SIZES) {
324
+ expect(packageChild.built.requests[`info|${size}|filled`]).toBe(
325
+ `package-info:${size}:filled`,
326
+ );
327
+ }
328
+ expect(packageChild.built.icons).toEqual({info: 'package-info:md:normal'});
329
+ });
330
+
331
+ it('keeps function entries in a family build', async () => {
332
+ writeLineage();
333
+ // A member that sets no icons of its own inherits the child's entries.
334
+ write(
335
+ 'inherit/source.mjs',
336
+ `import {defineTheme} from '@astryxdesign/core/theme';
337
+ import {childTheme} from '../child/source.mjs';
338
+ export default defineTheme({name: 'parity-inherit', extends: childTheme, tokens: {}});
339
+ `,
340
+ );
341
+ const receipt = await themeBuildFamily(
342
+ ['base/source.mjs', 'child/source.mjs', 'inherit/source.mjs'],
343
+ {familyKey: 'parity-family'},
344
+ {cwd: tmpDir},
345
+ );
346
+ expect(receipt?.type).toBe('theme.build');
347
+ const familyModule = `./${receipt.data.outputs.js}`;
348
+
349
+ const result = inspect([
350
+ {
351
+ label: 'base',
352
+ built: `${familyModule}#parityBaseTheme`,
353
+ runtime: './base/source.mjs#default',
354
+ },
355
+ {
356
+ label: 'child',
357
+ built: `${familyModule}#parityChildTheme`,
358
+ runtime: './child/source.mjs#default',
359
+ },
360
+ {
361
+ label: 'inherit',
362
+ built: `${familyModule}#parityInheritTheme`,
363
+ runtime: './inherit/source.mjs#default',
364
+ },
365
+ ]);
366
+
367
+ expect(result.base.built).toEqual(result.base.runtime);
368
+ expect(result.child.built).toEqual(result.child.runtime);
369
+ expect(result.inherit.built).toEqual(result.inherit.runtime);
370
+ expect(result.inherit.built.requests).toEqual(result.child.built.requests);
371
+ expect(result.child.built.requests['info|sm|filled']).toBe(
372
+ 'base-info:sm:filled',
373
+ );
374
+ expect(result.child.built.requests['info|sm|outline']).toBe(
375
+ 'base-info:sm:normal',
376
+ );
377
+ });
378
+ });
379
+
380
+ describe('the node view never captures a registry binding', () => {
381
+ it.each(['entries', 'nodes', 'name', 'entry', 'Object'])(
382
+ 'loads a built theme whose registry is imported as `%s`',
383
+ async binding => {
384
+ writeRegistry('icons.mjs', 'registry', {info: 'bound-info'});
385
+ write(
386
+ 'bound.mjs',
387
+ `import {defineTheme} from '@astryxdesign/core/theme';
388
+ import {registry as ${binding}} from './icons.mjs';
389
+ export default defineTheme({
390
+ name: 'bound', tokens: {}, iconVariants: ['filled'], icons: ${binding},
391
+ });
392
+ `,
393
+ );
394
+ await buildAndCheck('bound.mjs');
395
+
396
+ const {bound} = inspect([
397
+ {
398
+ label: 'bound',
399
+ built: './bound.js#boundTheme',
400
+ runtime: './bound.mjs#default',
401
+ },
402
+ ]);
403
+ expect(bound.built).toEqual(bound.runtime);
404
+ expect(bound.built.icons).toEqual({info: 'bound-info:md:normal'});
405
+ expect(bound.built.requests['info|lg|filled']).toBe(
406
+ 'bound-info:lg:filled',
407
+ );
408
+ expect(bound.reexportIsNodeView).toBe(true);
409
+ },
410
+ );
411
+ });
412
+
413
+ describe('a built theme types its icon variants', () => {
414
+ it('lets a theme extending the built module type `variant` by its list', async () => {
415
+ writeLineage();
416
+ await buildAndCheck('base/source.mjs');
417
+
418
+ const variants = fs.readFileSync(
419
+ path.join(tmpDir, 'base/parity-base.variants.d.ts'),
420
+ 'utf8',
421
+ );
422
+ expect(variants).toContain("declare module '@astryxdesign/core/Icon'");
423
+ expect(variants).toContain("'outline': true;");
424
+ expect(variants).toContain("'filled': true;");
425
+
426
+ write(
427
+ 'consumer.ts',
428
+ `import {defineTheme} from '@astryxdesign/core/theme';
429
+ import type {IconVariant} from '@astryxdesign/core/Icon';
430
+ import {parityBaseTheme} from './base/parity-base.js';
431
+
432
+ type Equal<A, B> =
433
+ (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2 ? true : false;
434
+
435
+ defineTheme({
436
+ name: 'consumer',
437
+ extends: parityBaseTheme,
438
+ icons: {
439
+ info: ({variant}) => {
440
+ const exact: Equal<typeof variant, 'outline' | 'filled' | undefined> = true;
441
+ return exact ? null : null;
442
+ },
443
+ // @ts-expect-error -- 'bold' is not a name the built theme lists.
444
+ close: ({variant}) => (variant === 'bold' ? null : null),
445
+ },
446
+ });
447
+
448
+ // The built theme's augmentation adds its names to IconVariant.
449
+ export const listed: IconVariant = 'filled';
450
+ // @ts-expect-error -- no theme here lists 'bold'.
451
+ export const unlisted: IconVariant = 'bold';
452
+ `,
453
+ );
454
+ const program = ts.createProgram([path.join(tmpDir, 'consumer.ts')], {
455
+ strict: true,
456
+ noEmit: true,
457
+ skipLibCheck: true,
458
+ target: ts.ScriptTarget.ES2022,
459
+ module: ts.ModuleKind.ESNext,
460
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
461
+ types: [],
462
+ });
463
+ const diagnostics = ts
464
+ .getPreEmitDiagnostics(program)
465
+ .filter(diagnostic => diagnostic.file?.fileName.endsWith('consumer.ts'))
466
+ .map(diagnostic =>
467
+ ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'),
468
+ );
469
+ expect(diagnostics).toEqual([]);
470
+ });
471
+ });
472
+
473
+ describe('a function the build cannot carry fails the build', () => {
474
+ it.each([
475
+ [
476
+ 'an inline function entry',
477
+ `icons: {info: ({size}) => createElement('svg', {'data-size': size})}`,
478
+ ],
479
+ [
480
+ 'a function beside a spread registry',
481
+ `icons: {...baseIcons, info: () => null}`,
482
+ ],
483
+ ])('rejects %s before writing', async (_label, iconsField) => {
484
+ writeRegistry('icons.mjs', 'baseIcons', {close: 'base-close'});
485
+ write(
486
+ 'inline.mjs',
487
+ `import {createElement} from 'react';
488
+ import {defineTheme} from '@astryxdesign/core/theme';
489
+ import {baseIcons} from './icons.mjs';
490
+ export default defineTheme({name: 'inline-functions', tokens: {}, ${iconsField}});
491
+ `,
492
+ );
493
+
494
+ await expect(
495
+ themeBuild('inline.mjs', {}, {cwd: tmpDir}),
496
+ ).rejects.toMatchObject({code: 'ERR_THEME_INVALID'});
497
+ expect(fs.readdirSync(tmpDir).sort()).toEqual(['icons.mjs', 'inline.mjs']);
498
+ });
499
+ });
@@ -0,0 +1,105 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file A built theme keeps its `iconVariants` list.
5
+ *
6
+ * `architecture:theme-compilation` (System model item 7): a built theme carries
7
+ * its `iconVariants` list in its JavaScript, so the built theme, and a theme
8
+ * that `extends` it, read the same list as the runtime theme
9
+ * (`spec:AST-087` FR2, FR18).
10
+ *
11
+ * `themeBuild` compiles via @astryxdesign/core, so it needs a built core — the
12
+ * `node` project's globalSetup builds it once before workers fork.
13
+ */
14
+
15
+ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
16
+ import * as fs from 'node:fs';
17
+ import * as os from 'node:os';
18
+ import * as path from 'node:path';
19
+ import {pathToFileURL} from 'node:url';
20
+ import {defineTheme} from '@astryxdesign/core/theme';
21
+ import {themeBuild} from './build.mjs';
22
+
23
+ vi.setConfig({testTimeout: 30000});
24
+
25
+ let tmpDir;
26
+ beforeEach(() => {
27
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-icon-variants-'));
28
+ });
29
+ afterEach(() => {
30
+ fs.rmSync(tmpDir, {recursive: true, force: true});
31
+ });
32
+
33
+ /**
34
+ * Build a raw theme source and import the generated module's theme.
35
+ * @param {Record<string, unknown>} source
36
+ */
37
+ async function buildAndImport(source) {
38
+ const name = String(source.name);
39
+ fs.writeFileSync(
40
+ path.join(tmpDir, `${name}.mjs`),
41
+ `export default ${JSON.stringify({tokens: {}, ...source})};\n`,
42
+ );
43
+ await themeBuild(`${name}.mjs`, {}, {cwd: tmpDir});
44
+ const mod = await import(pathToFileURL(path.join(tmpDir, `${name}.js`)).href);
45
+ const [theme] = Object.values(mod);
46
+ return /** @type {any} */ (theme);
47
+ }
48
+
49
+ describe('theme build keeps the iconVariants list', () => {
50
+ it('writes the list a theme sets, as the runtime theme reads it', async () => {
51
+ const source = {name: 'listed-icons', iconVariants: ['outline', 'filled']};
52
+ const built = await buildAndImport(source);
53
+
54
+ expect(built.__built).toBe(true);
55
+ expect(built.iconVariants).toEqual(['outline', 'filled']);
56
+ expect(built.iconVariants).toEqual(defineTheme(source).iconVariants);
57
+ });
58
+
59
+ it('lets a child of the built theme inherit or replace the list, as at runtime', async () => {
60
+ const source = {name: 'base-icons', iconVariants: ['outline', 'filled']};
61
+ const built = await buildAndImport(source);
62
+ const runtime = defineTheme(source);
63
+
64
+ const inherits = defineTheme({name: 'inherits', extends: built});
65
+ const replaces = defineTheme({
66
+ name: 'replaces',
67
+ extends: built,
68
+ iconVariants: ['bold'],
69
+ });
70
+
71
+ expect(inherits.iconVariants).toEqual(['outline', 'filled']);
72
+ expect(replaces.iconVariants).toEqual(['bold']);
73
+ expect(inherits.iconVariants).toEqual(
74
+ defineTheme({name: 'inherits-runtime', extends: runtime}).iconVariants,
75
+ );
76
+ expect(replaces.iconVariants).toEqual(
77
+ defineTheme({
78
+ name: 'replaces-runtime',
79
+ extends: runtime,
80
+ iconVariants: ['bold'],
81
+ }).iconVariants,
82
+ );
83
+ });
84
+
85
+ it('keeps an explicit empty list, which replaces an inherited one', async () => {
86
+ const built = await buildAndImport({
87
+ name: 'emptied-icons',
88
+ iconVariants: [],
89
+ });
90
+
91
+ expect(built.iconVariants).toEqual([]);
92
+ expect(
93
+ defineTheme({name: 'emptied-child', extends: built}).iconVariants,
94
+ ).toEqual([]);
95
+ });
96
+
97
+ it('writes no list for a theme without one', async () => {
98
+ const built = await buildAndImport({name: 'unlisted-icons'});
99
+
100
+ expect('iconVariants' in built).toBe(false);
101
+ expect(
102
+ defineTheme({name: 'unlisted-child', extends: built}).iconVariants,
103
+ ).toBeUndefined();
104
+ });
105
+ });