@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
@@ -15,6 +15,7 @@
15
15
  import path from 'node:path';
16
16
  import postcss from 'postcss';
17
17
  import selectorParser from 'postcss-selector-parser';
18
+ import {parseCss, processSelector} from '../../parse.mjs';
18
19
  import {
19
20
  V054_THEME_SELECTOR_DYNAMIC_AXES,
20
21
  V054_THEME_SELECTOR_TARGETS,
@@ -555,7 +556,7 @@ function migrateSelectorTextWithContext(
555
556
  ) {
556
557
  /** @type {TargetContext} */
557
558
  let context = {guaranteed: [], possible: []};
558
- const text = selectorParser(selectors => {
559
+ const text = processSelector(selector, selectors => {
559
560
  /** @type {TargetContext[]} */
560
561
  const branchContexts = [];
561
562
  selectors.each(selectorNode => {
@@ -593,7 +594,7 @@ function migrateSelectorTextWithContext(
593
594
  });
594
595
  });
595
596
  context = combineSelectorListContexts(branchContexts);
596
- }).processSync(selector, {lossless: true});
597
+ });
597
598
  return {text, context};
598
599
  }
599
600
 
@@ -677,7 +678,7 @@ export function migrateAstryxThemeSelectors(source, from = 'styles.css', data) {
677
678
 
678
679
  /** @param {string} source @param {string} from @returns {string} */
679
680
  function migrateWithActiveTables(source, from) {
680
- const root = postcss.parse(source, {from});
681
+ const root = parseCss(source, {from});
681
682
  const existingComments = new Set();
682
683
  root.walkComments(comment => {
683
684
  existingComments.add(comment.text.trim());
@@ -1,6 +1,9 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
- import {describe, expect, it} from 'vitest';
3
+ import * as fs from 'node:fs';
4
+ import * as os from 'node:os';
5
+ import * as path from 'node:path';
6
+ import {afterEach, describe, expect, it} from 'vitest';
4
7
  import jscodeshift from 'jscodeshift';
5
8
  import manifest from '../index.mjs';
6
9
  import transform, {
@@ -16,6 +19,38 @@ function apply(source, filePath = 'theme.ts') {
16
19
  return transform({source, path: filePath}, api) ?? source;
17
20
  }
18
21
 
22
+ /** A proven theme: an object passed to Astryx's imported defineTheme(). */
23
+ function defined(components) {
24
+ return `${IMPORT}export default defineTheme({name: 'brand', components: ${components}});`;
25
+ }
26
+
27
+ const THEME_DOC = `/** @type {import('@astryxdesign/cli/authoring').ThemeDoc} */
28
+ export default {
29
+ type: 'theme',
30
+ name: 'brand',
31
+ displayName: 'Brand',
32
+ description: 'A brand theme.',
33
+ maintained: false,
34
+ };
35
+ `;
36
+
37
+ const tempDirs = [];
38
+ afterEach(() => {
39
+ for (const dir of tempDirs.splice(0)) {
40
+ fs.rmSync(dir, {recursive: true, force: true});
41
+ }
42
+ });
43
+
44
+ /** Write `files` into a fresh directory and return its path. */
45
+ function folder(files) {
46
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-theme-surface-'));
47
+ tempDirs.push(dir);
48
+ for (const [name, content] of Object.entries(files)) {
49
+ fs.writeFileSync(path.join(dir, name), content);
50
+ }
51
+ return dir;
52
+ }
53
+
19
54
  const EXPECTED_TARGETS = {
20
55
  'base-table': 'table',
21
56
  checkbox: 'checkbox-indicator',
@@ -60,7 +95,7 @@ describe('migrate-deprecated-theme-surface', () => {
60
95
  'renames static target key %s to %s inside a components map',
61
96
  (oldKey, newKey) => {
62
97
  const output = apply(
63
- `export default {name: 'brand', components: {${JSON.stringify(oldKey)}: {base: {color: 'red'}}}};`,
98
+ defined(`{${JSON.stringify(oldKey)}: {base: {color: 'red'}}}`),
64
99
  );
65
100
 
66
101
  expect(output).not.toContain(`${JSON.stringify(oldKey)}:`);
@@ -103,12 +138,114 @@ export const theme = defineTheme({name: 'brand', components});`);
103
138
  }
104
139
  });
105
140
 
141
+ it('does not rewrite a look-alike theme object from another library', () => {
142
+ const input = `export const muiTheme = {
143
+ name: 'brand',
144
+ components: {checkbox: {root: {color: 'red'}}, radio: {root: {}}, textarea: {root: {}}},
145
+ };`;
146
+ expect(apply(input, 'src/muiTheme.ts')).toBe(input);
147
+ });
148
+
149
+ it('does not take a file-level Astryx import as proof for a look-alike object', () => {
150
+ const input = `import {Button} from '@astryxdesign/core';
151
+ export const editorTheme = {name: 'editor', components: {textarea: {root: {}}}};`;
152
+ expect(apply(input, 'src/editorTheme.ts')).toBe(input);
153
+ });
154
+
155
+ it('renames a direct theme object in a source its theme descriptor declares', () => {
156
+ const dir = folder({'brandTheme.doc.mjs': THEME_DOC});
157
+ const output = apply(
158
+ `export const brandTheme = {name: 'brand', components: {navicon: {base: {}}}};`,
159
+ path.join(dir, 'brandTheme.ts'),
160
+ );
161
+ expect(output).toContain("'nav-icon':");
162
+ expect(output).not.toMatch(/navicon\s*:/);
163
+ });
164
+
165
+ it('renames only the declared theme binding in a described theme source', () => {
166
+ const dir = folder({'brandTheme.doc.mjs': THEME_DOC});
167
+ const output = apply(
168
+ `export const brandTheme = {name: 'brand', components: {navicon: {base: {}}}};
169
+ export const otherLibraryTheme = {
170
+ name: 'other',
171
+ components: {checkbox: {root: {}}, textarea: {root: {}}},
172
+ };`,
173
+ path.join(dir, 'brandTheme.ts'),
174
+ );
175
+ expect(output).toContain("'nav-icon':");
176
+ expect(output).toContain(
177
+ 'components: {checkbox: {root: {}}, textarea: {root: {}}}',
178
+ );
179
+ expect(output).not.toContain('checkbox-indicator');
180
+ expect(output).not.toContain('text-area');
181
+ });
182
+
183
+ it('follows a local binding exported under the descriptor name', () => {
184
+ const dir = folder({'brandTheme.doc.mjs': THEME_DOC});
185
+ const output = apply(
186
+ `const theme = {name: 'brand', components: {navicon: {base: {}}}} as const;
187
+ export {theme as brandTheme};`,
188
+ path.join(dir, 'brandTheme.ts'),
189
+ );
190
+ expect(output).toContain("'nav-icon':");
191
+ });
192
+
193
+ it('does not treat an unexported or nested same-named object as the declared theme', () => {
194
+ const dir = folder({'brandTheme.doc.mjs': THEME_DOC});
195
+ for (const input of [
196
+ `const brandTheme = {name: 'brand', components: {navicon: {base: {}}}};`,
197
+ `export function make() {
198
+ const brandTheme = {name: 'brand', components: {navicon: {base: {}}}};
199
+ return brandTheme;
200
+ }`,
201
+ ]) {
202
+ expect(apply(input, path.join(dir, 'brandTheme.ts'))).toBe(input);
203
+ }
204
+ });
205
+
206
+ it('does not treat a descriptor for another stem or kind as theme proof', () => {
207
+ const input = `export const brandTheme = {name: 'brand', components: {navicon: {base: {}}}};`;
208
+ const otherStem = folder({'otherTheme.doc.mjs': THEME_DOC});
209
+ expect(apply(input, path.join(otherStem, 'brandTheme.ts'))).toBe(input);
210
+ const otherKind = folder({
211
+ 'brandTheme.doc.mjs': 'export default {name: "brand", props: []};\n',
212
+ });
213
+ expect(apply(input, path.join(otherKind, 'brandTheme.ts'))).toBe(input);
214
+ });
215
+
106
216
  it('does not treat a defineTheme from another library as an Astryx theme', () => {
107
217
  const input = `import {defineTheme} from 'other-ui';
108
218
  export default defineTheme({components: {checkbox: {base: {color: 'red'}}}});`;
109
219
  expect(apply(input)).toBe(input);
110
220
  });
111
221
 
222
+ it.each([
223
+ [
224
+ 'named defineTheme',
225
+ `import {defineTheme} from '@astryxdesign/core';
226
+ function unrelated(defineTheme) {
227
+ return defineTheme({name: 'fake', components: {checkbox: {root: {}}}});
228
+ }`,
229
+ ],
230
+ [
231
+ 'aliased defineTheme',
232
+ `import {defineTheme as makeTheme} from '@astryxdesign/core';
233
+ function unrelated() {
234
+ const makeTheme = otherLibrary.createTheme;
235
+ return makeTheme({name: 'fake', components: {checkbox: {root: {}}}});
236
+ }`,
237
+ ],
238
+ [
239
+ 'namespace defineTheme',
240
+ `import * as Astryx from '@astryxdesign/core';
241
+ function unrelated(Astryx) {
242
+ return Astryx.defineTheme({name: 'fake', components: {checkbox: {root: {}}}});
243
+ }`,
244
+ ],
245
+ ])('does not treat a shadowed %s as an Astryx call', (_name, input) => {
246
+ expect(apply(input)).toBe(input);
247
+ });
248
+
112
249
  it.each([
113
250
  [
114
251
  'an aliased defineTheme import',
@@ -141,9 +278,54 @@ const brand: DefineThemeInput = {name: 'brand', components: {navicon: {base: {}}
141
278
  expect(output).not.toMatch(/navicon\s*:/);
142
279
  });
143
280
 
281
+ it.each([
282
+ [
283
+ 'a locally redeclared named type',
284
+ `import type {DefineThemeInput} from '@astryxdesign/core';
285
+ function unrelated() {
286
+ type DefineThemeInput = OtherTheme;
287
+ return {name: 'fake', components: {checkbox: {root: {}}}} satisfies DefineThemeInput;
288
+ }`,
289
+ ],
290
+ [
291
+ 'an aliased type shadowed by a type parameter',
292
+ `import type {DefineThemeInput as Theme} from '@astryxdesign/core';
293
+ function unrelated<Theme>(value: unknown) {
294
+ const theme: Theme = {name: 'fake', components: {checkbox: {root: {}}}};
295
+ return theme;
296
+ }`,
297
+ ],
298
+ [
299
+ 'a namespace shadowed by a type parameter',
300
+ `import type * as Astryx from '@astryxdesign/core';
301
+ function unrelated<Astryx>() {
302
+ return {name: 'fake', components: {checkbox: {root: {}}}} satisfies Astryx.DefineThemeInput;
303
+ }`,
304
+ ],
305
+ [
306
+ 'a namespace shadowed by a local namespace',
307
+ `import * as Astryx from '@astryxdesign/core';
308
+ function unrelated() {
309
+ namespace Astryx { export type DefineThemeInput = OtherTheme; }
310
+ return ({name: 'fake', components: {checkbox: {root: {}}}}) as Astryx.DefineThemeInput;
311
+ }`,
312
+ ],
313
+ ])('does not treat %s as an Astryx theme type', (_name, input) => {
314
+ expect(apply(input)).toBe(input);
315
+ });
316
+
317
+ it('still recognizes an unshadowed theme type inside a function', () => {
318
+ const output =
319
+ apply(`import type {DefineThemeInput} from '@astryxdesign/core';
320
+ function makeTheme<T>() {
321
+ return {components: {navicon: {base: {}}}} satisfies DefineThemeInput;
322
+ }`);
323
+ expect(output).toContain("'nav-icon':");
324
+ });
325
+
144
326
  it('renames a clear-icon alias with a TODO naming the owner scope it loses', () => {
145
327
  const output = apply(
146
- `export default {name: 'brand', components: {'selector-clear-icon': {base: {color: 'red'}}}};`,
328
+ defined(`{'selector-clear-icon': {base: {color: 'red'}}}`),
147
329
  );
148
330
  expect(output).toContain("'input-clear-icon':");
149
331
  expect(output).toContain(
@@ -155,10 +337,12 @@ const brand: DefineThemeInput = {name: 'brand', components: {navicon: {base: {}}
155
337
  });
156
338
 
157
339
  it('flags two clear-icon aliases that collapse onto the shared target', () => {
158
- const output = apply(`export default {name: 'brand', components: {
340
+ const output = apply(
341
+ defined(`{
159
342
  'selector-clear-icon': {base: {color: 'red'}},
160
343
  'date-input-clear-icon': {base: {color: 'blue'}},
161
- }};`);
344
+ }`),
345
+ );
162
346
  expect(output).toContain("'input-clear-icon':");
163
347
  expect(output).toContain(
164
348
  'merge deprecated theme target `date-input-clear-icon` into `input-clear-icon`',
@@ -291,6 +475,21 @@ const theme = {name: 'brand', components: {[oldTarget]: {base: {color: 'red'}}}}
291
475
  expect(apply(output, 'theme.css')).toBe(output);
292
476
  });
293
477
 
478
+ it('maps only the column values MetadataList emitted, not app classes', () => {
479
+ const output = apply(
480
+ `.astryx-metadata-list.app-compact-list { gap: 4px; }
481
+ .astryx-metadata-list.columns-3, .astryx-metadata-list.multi { gap: 8px; }`,
482
+ 'theme.css',
483
+ );
484
+ expect(output).toContain('.astryx-metadata-list.app-compact-list');
485
+ expect(output).not.toContain('data-columns="app-compact-list"');
486
+ expect(output).toContain('.astryx-metadata-list:is([data-columns="3"])');
487
+ expect(output).not.toContain('data-columns="columns-3"');
488
+ expect(output).toContain(
489
+ '.astryx-metadata-list:is([data-columns="multi"])',
490
+ );
491
+ });
492
+
294
493
  it('leaves unqualified and unknown consumer classes unchanged', () => {
295
494
  const input = `.primary { color: red; }
296
495
  .astryx-button.brand { color: blue; }`;
@@ -312,7 +511,9 @@ const theme = {name: 'brand', components: {[oldTarget]: {base: {color: 'red'}}}}
312
511
  });
313
512
 
314
513
  it('is byte-idempotent after JavaScript and CSS migration', () => {
315
- const js = `export default {name: 'brand', components: {statusdot: {base: {color: 'red'}}, 'selector-clear-icon': {base: {color: 'red'}}}};`;
514
+ const js = defined(
515
+ `{statusdot: {base: {color: 'red'}}, 'selector-clear-icon': {base: {color: 'red'}}}`,
516
+ );
316
517
  const css = '.astryx-textarea.sm {}';
317
518
  const jsOnce = apply(js);
318
519
  const cssOnce = apply(css, 'theme.css');
@@ -1,95 +1,168 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
- import {describe, expect, it} from 'vitest';
4
- import transform from '../validate-data-token-ownership.mjs';
3
+ import * as fs from 'node:fs';
4
+ import * as os from 'node:os';
5
+ import * as path from 'node:path';
6
+ import {afterAll, describe, expect, it} from 'vitest';
7
+ import transform, {meta} from '../validate-data-token-ownership.mjs';
8
+ import {codemodOutcome} from '../../../outcome.mjs';
9
+ import {upgrade} from '../../../../../api/upgrade/upgrade.mjs';
5
10
 
6
- async function run(source, file = 'test.tsx') {
7
- const jscodeshift = (await import('jscodeshift')).default;
8
- const parser =
9
- file.endsWith('.ts') || file.endsWith('.tsx') ? 'tsx' : 'babel';
10
- const api = {
11
- jscodeshift: jscodeshift.withParser(parser),
12
- stats: () => {},
13
- report: () => {},
11
+ const SLOW = 30_000;
12
+ const tempRoots = [];
13
+
14
+ afterAll(() => {
15
+ for (const root of tempRoots) fs.rmSync(root, {recursive: true, force: true});
16
+ });
17
+
18
+ const STALE_THEME = `/* @generated by \`astryx theme build\` */
19
+ @layer astryx-base {
20
+ :root { --color-data-categorical-green: #008000; }
21
+ }`;
22
+
23
+ const RAW_USES = {
24
+ 'src/chart.tsx': `export const fill = 'var(--color-data-categorical-green)';
25
+ export const border = \`1px solid var(--color-data-blue-3)\`;
26
+ `,
27
+ 'src/chart.css': '.bar { fill: var(--color-data-categorical-green); }\n',
28
+ };
29
+
30
+ /**
31
+ * A project with Core 0.7.0 installed and the given files, as a repository.
32
+ * @param {Record<string, string | object>} files
33
+ */
34
+ function project(files) {
35
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-data-token-'));
36
+ tempRoots.push(root);
37
+ fs.mkdirSync(path.join(root, '.git'));
38
+ const all = {
39
+ 'node_modules/@astryxdesign/core/package.json': {
40
+ name: '@astryxdesign/core',
41
+ version: '0.7.0',
42
+ },
43
+ ...files,
14
44
  };
15
- return transform({source, path: file}, api);
45
+ for (const [rel, contents] of Object.entries(all)) {
46
+ const file = path.join(root, rel);
47
+ fs.mkdirSync(path.dirname(file), {recursive: true});
48
+ fs.writeFileSync(
49
+ file,
50
+ typeof contents === 'string' ? contents : JSON.stringify(contents),
51
+ );
52
+ }
53
+ return root;
16
54
  }
17
55
 
18
- describe('validate-data-token-ownership', () => {
19
- it('reports direct string and static-template consumers with both migration paths', async () => {
20
- for (const source of [
21
- `const fill = 'var(--color-data-categorical-green)';`,
22
- 'const fill = `var(--color-data-categorical-green)`;',
23
- `const border = '1px solid var(--color-data-categorical-green)';`,
24
- 'const border = `${width}px solid var(--color-data-categorical-green)`;',
25
- ]) {
26
- await expect(run(source)).rejects.toThrow(
27
- /CSS-capable code imports dataVars.*Canvas.*resolveThemeToken/s,
28
- );
29
- }
30
- });
56
+ const PREBUILT = {
57
+ 'package.json': {dependencies: {'@astryxdesign/core': '0.7.3'}},
58
+ 'src/main.tsx': "import '@astryxdesign/core/astryx.css';\n",
59
+ };
60
+ const SOURCE_BUILT = {
61
+ 'package.json': {
62
+ dependencies: {'@astryxdesign/core': '0.7.3'},
63
+ devDependencies: {'@astryxdesign/build': '0.7.3'},
64
+ },
65
+ 'vite.config.ts':
66
+ "import {astryxStylex} from '@astryxdesign/build/vite';\nexport default {plugins: [...astryxStylex()]};\n",
67
+ };
31
68
 
32
- it('reports prebuilt CSS availability without requiring a source compile step', async () => {
33
- await expect(
34
- run('const fill = "var(--color-data-categorical-blue)";'),
35
- ).rejects.toThrow(
36
- /in prebuilt mode, loads @astryxdesign\/core\/astryx\.css/,
37
- );
38
- await expect(
39
- run('.bar { fill: var(--color-data-categorical-blue); }', 'chart.css'),
40
- ).rejects.toThrow(/load @astryxdesign\/core\/astryx\.css in prebuilt mode/);
41
- });
69
+ const runUpgrade = root =>
70
+ upgrade(
71
+ {from: '0.6.9', codemod: 'validate-data-token-ownership', apply: true},
72
+ {cwd: root},
73
+ );
42
74
 
43
- it('leaves fallbacks, dataVars access, and comments unchanged', async () => {
44
- const source = `
45
- // var(--color-data-categorical-green)
46
- const fallback = 'var(--color-data-categorical-green, #008000)';
47
- const fill = dataVars['--color-data-categorical-green'];
48
- `;
49
- expect(await run(source)).toBeUndefined();
75
+ describe('validate-data-token-ownership', () => {
76
+ it('reads only stylesheets', () => {
77
+ expect(meta.fileExtensions).toEqual(['.css']);
50
78
  });
51
79
 
52
- it('reports stale generated theme CSS with root data defaults', async () => {
53
- const source = `/* @generated by \`astryx theme build\` */
54
- @layer astryx-base {
55
- :root { --color-data-categorical-green: #008000; }
56
- }`;
57
- await expect(run(source, 'theme.css')).rejects.toThrow(
80
+ it('reports stale generated theme CSS with root data defaults as a regeneration', () => {
81
+ expect(() => transform({source: STALE_THEME, path: 'theme.css'})).toThrow(
58
82
  /Rebuild it with `astryx theme build`/,
59
83
  );
84
+ let thrown;
85
+ try {
86
+ transform({source: STALE_THEME, path: 'theme.css'});
87
+ } catch (err) {
88
+ thrown = err;
89
+ }
90
+ expect(codemodOutcome(thrown)).toMatchObject({
91
+ kind: 'regenerate',
92
+ commands: ['astryx theme build'],
93
+ });
60
94
  });
61
95
 
62
- it('reports raw stylesheet uses without a fallback', async () => {
63
- await expect(
64
- run('.bar { fill: var(--color-data-categorical-green); }', 'chart.css'),
65
- ).rejects.toThrow(/Raw var\(--color-data-categorical-green\).*line 1/s);
66
- });
67
-
68
- it('leaves stylesheet uses with a fallback, and comments, unchanged', async () => {
69
- expect(
70
- await run(
71
- `/* var(--color-data-categorical-green) */
72
- .bar { fill: var(--color-data-categorical-green, #008000); }`,
73
- 'chart.css',
74
- ),
75
- ).toBeUndefined();
96
+ it('passes raw data-token uses with or without a fallback', () => {
97
+ for (const source of [
98
+ '.bar { fill: var(--color-data-categorical-green); }',
99
+ '.bar { fill: var(--color-data-categorical-green, #008000); }',
100
+ `/* @generated by \`astryx theme build\` */
101
+ .bar { fill: var(--color-data-blue-3); }`,
102
+ ]) {
103
+ expect(transform({source, path: 'chart.css'})).toBeUndefined();
104
+ }
76
105
  });
77
106
 
78
- it('does not claim ownership of hand-authored CSS or sparse theme overrides', async () => {
107
+ it('does not claim ownership of hand-authored CSS or sparse theme overrides', () => {
79
108
  expect(
80
- await run(
81
- ':root { --color-data-categorical-green: #008000; }',
82
- 'app.css',
83
- ),
109
+ transform({
110
+ source: ':root { --color-data-categorical-green: #008000; }',
111
+ path: 'app.css',
112
+ }),
84
113
  ).toBeUndefined();
85
114
  expect(
86
- await run(
87
- `/* @generated by \`astryx theme build\` */
115
+ transform({
116
+ source: `/* @generated by \`astryx theme build\` */
88
117
  @scope ([data-astryx-theme="ocean"]) {
89
118
  :scope { --color-data-categorical-green: #008000; }
90
119
  }`,
91
- 'theme.css',
92
- ),
120
+ path: 'theme.css',
121
+ }),
93
122
  ).toBeUndefined();
94
123
  });
124
+
125
+ it(
126
+ 'completes `astryx upgrade --apply` over raw uses in a prebuilt project',
127
+ async () => {
128
+ const root = project({...PREBUILT, ...RAW_USES});
129
+ const res = await runUpgrade(root);
130
+ expect(res.data.complete).toBe(true);
131
+ expect(res.data.errors ?? []).toEqual([]);
132
+ expect(res.data.modifiedFiles ?? []).toEqual([]);
133
+ },
134
+ SLOW,
135
+ );
136
+
137
+ it(
138
+ 'completes `astryx upgrade --apply` over raw uses in a source-built project',
139
+ async () => {
140
+ const root = project({...SOURCE_BUILT, ...RAW_USES});
141
+ const res = await runUpgrade(root);
142
+ expect(res.data.complete).toBe(true);
143
+ expect(res.data.errors ?? []).toEqual([]);
144
+ expect(res.data.modifiedFiles ?? []).toEqual([]);
145
+ },
146
+ SLOW,
147
+ );
148
+
149
+ it(
150
+ 'leaves `astryx upgrade --apply` incomplete on stale generated theme CSS, naming the rebuild',
151
+ async () => {
152
+ const root = project({...PREBUILT, 'src/theme.css': STALE_THEME});
153
+ const res = await runUpgrade(root);
154
+ expect(res.data).toMatchObject({
155
+ complete: false,
156
+ errorCode: 'ERR_CODEMOD_PROTECTED',
157
+ errors: [],
158
+ regenerate: [
159
+ expect.objectContaining({
160
+ file: 'src/theme.css',
161
+ commands: ['astryx theme build'],
162
+ }),
163
+ ],
164
+ });
165
+ },
166
+ SLOW,
167
+ );
95
168
  });
@@ -214,6 +214,11 @@ export const V06_THEME_SELECTOR_TARGETS = {
214
214
  "typeahead": {"error":["status","error"],"lg":["size","lg"],"md":["size","md"],"sm":["size","sm"],"success":["status","success"],"warning":["status","warning"]},
215
215
  };
216
216
 
217
+ // Only a prop typed as an open string emitted unbounded bare classes
218
+ // (CodeBlock `language`). MetadataList `columns` is `'single' | 'multi' |
219
+ // number`: its strings are in the table above and every number keeps the
220
+ // `columns-` prefix, so any other class beside `.astryx-metadata-list` is
221
+ // the app's own and stays unchanged.
217
222
  /** @type {Record<string, Array<{axis: string, token: 'class-value' | 'prefixed', prefix?: string}>>} */
218
223
  // prettier-ignore
219
224
  export const V06_THEME_SELECTOR_DYNAMIC_AXES = {
@@ -224,6 +229,6 @@ export const V06_THEME_SELECTOR_DYNAMIC_AXES = {
224
229
  "codeblock-header": [{"axis":"language","token":"class-value"}],
225
230
  "codeblock-title": [{"axis":"language","token":"class-value"}],
226
231
  "grid": [{"axis":"columns","token":"prefixed","prefix":"columns-"}],
227
- "metadata-list": [{"axis":"columns","token":"prefixed","prefix":"columns-"},{"axis":"columns","token":"class-value"}],
232
+ "metadata-list": [{"axis":"columns","token":"prefixed","prefix":"columns-"}],
228
233
  "outline-item": [{"axis":"level","token":"prefixed","prefix":"level-"}],
229
234
  };