@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
@@ -1,27 +1,47 @@
1
1
  // Copyright (c) Meta Platforms, Inc. and affiliates.
2
2
 
3
3
  /**
4
- * Find imported components, resolve their local JSX names (including aliases),
5
- * and yield matching JSX prop paths to a visitor.
4
+ * Find imported components, resolve their local names (including aliases and
5
+ * namespace members) at a use site, and yield matching JSX prop paths to a
6
+ * visitor.
6
7
  *
7
- * This helper intentionally stops at the JSX boundary. It does not follow
8
- * variables, helper calls, object spreads, wrapper components, or types.
9
- *
10
- * @param {any} root jscodeshift collection for one source file
11
- * @param {any} j configured jscodeshift instance
12
- * @param {{
8
+ * `transformProp` intentionally stops at the JSX boundary. It does not follow
9
+ * variables, helper calls, object spreads, wrapper components, or types. The
10
+ * resolver it is built on (`importedComponents`, `resolveImportedComponent`,
11
+ * `isModuleBinding`) is exported for transforms that prove other use sites.
12
+ */
13
+
14
+ /**
15
+ * @typedef {{
13
16
  * matchesImport: (source: string) => boolean,
14
17
  * components: ReadonlyMap<string, ReadonlySet<string>>,
15
18
  * normalizeComponentName?: (name: string) => string,
16
- * }} options
17
- * @param {(propPath: any, context: {component: string, localName: string}) => void} visit
19
+ * }} ImportOptions
18
20
  */
19
- export function transformProp(root, j, options, visit) {
21
+
22
+ /**
23
+ * @typedef {{
24
+ * direct: Map<string, {component: string, props: ReadonlySet<string>}>,
25
+ * namespaces: Set<string>,
26
+ * normalize: (name: string) => string,
27
+ * components: ReadonlyMap<string, ReadonlySet<string>>,
28
+ * }} ImportedComponents
29
+ */
30
+
31
+ /**
32
+ * Local names bound by accepted runtime imports: named, aliased and default
33
+ * component imports, and namespace imports.
34
+ * @param {any} root jscodeshift collection for one source file
35
+ * @param {any} j configured jscodeshift instance
36
+ * @param {ImportOptions} options
37
+ * @returns {ImportedComponents}
38
+ */
39
+ export function importedComponents(root, j, options) {
20
40
  const normalize = options.normalizeComponentName ?? (name => name);
21
- /** @type {Map<string, {component: string, props: ReadonlySet<string>}>} */
22
- const directBindings = new Map();
41
+ /** @type {ImportedComponents['direct']} */
42
+ const direct = new Map();
23
43
  /** @type {Set<string>} */
24
- const namespaceBindings = new Set();
44
+ const namespaces = new Set();
25
45
 
26
46
  root.find(j.ImportDeclaration).forEach((/** @type {any} */ importPath) => {
27
47
  const source = importPath.node.source.value;
@@ -46,53 +66,83 @@ export function transformProp(root, j, options, visit) {
46
66
  const component = normalize(importedName);
47
67
  const props = options.components.get(component);
48
68
  if (!props) continue;
49
- directBindings.set(specifier.local?.name ?? importedName, {
50
- component,
51
- props,
52
- });
69
+ direct.set(specifier.local?.name ?? importedName, {component, props});
53
70
  } else if (specifier.type === 'ImportDefaultSpecifier' && subpathProps) {
54
- directBindings.set(specifier.local.name, {
71
+ direct.set(specifier.local.name, {
55
72
  component: subpathName,
56
73
  props: subpathProps,
57
74
  });
58
75
  } else if (specifier.type === 'ImportNamespaceSpecifier') {
59
- namespaceBindings.add(specifier.local.name);
76
+ namespaces.add(specifier.local.name);
60
77
  }
61
78
  }
62
79
  });
63
80
 
64
- if (directBindings.size === 0 && namespaceBindings.size === 0) return;
81
+ return {direct, namespaces, normalize, components: options.components};
82
+ }
65
83
 
66
- root.find(j.JSXOpeningElement).forEach((/** @type {any} */ jsxPath) => {
67
- const name = jsxPath.node.name;
68
- let localName;
69
- let component;
70
- let props;
84
+ /**
85
+ * True when `localName` at `path` resolves to a module-scope binding. A
86
+ * parameter or local declaration with the same name shadows the import.
87
+ * @param {any} path any node path inside the file
88
+ * @param {string} localName
89
+ */
90
+ export function isModuleBinding(path, localName) {
91
+ return path.scope?.lookup(localName)?.path.node.type === 'Program';
92
+ }
71
93
 
72
- if (name.type === 'JSXIdentifier') {
73
- localName = name.name;
74
- const binding = directBindings.get(localName);
75
- if (!binding) return;
76
- ({component, props} = binding);
77
- } else if (
78
- name.type === 'JSXMemberExpression' &&
79
- name.object?.type === 'JSXIdentifier' &&
80
- name.property?.type === 'JSXIdentifier' &&
81
- namespaceBindings.has(name.object.name)
82
- ) {
83
- localName = name.object.name;
84
- component = normalize(name.property.name);
85
- props = options.components.get(component);
86
- if (!props) return;
87
- } else {
88
- return;
89
- }
94
+ /**
95
+ * Resolve a component reference (`Card`, `Lib.Card`, or their JSX forms) to
96
+ * the imported component it names at `scopePath`, or null.
97
+ * @param {any} nameNode Identifier, JSXIdentifier, MemberExpression, or JSXMemberExpression
98
+ * @param {any} scopePath the path whose scope the reference is evaluated in
99
+ * @param {ImportedComponents} imports
100
+ * @returns {{component: string, props: ReadonlySet<string>, localName: string} | null}
101
+ */
102
+ export function resolveImportedComponent(nameNode, scopePath, imports) {
103
+ let localName;
104
+ let binding;
105
+ if (nameNode?.type === 'JSXIdentifier' || nameNode?.type === 'Identifier') {
106
+ localName = nameNode.name;
107
+ binding = imports.direct.get(localName);
108
+ } else if (
109
+ (nameNode?.type === 'JSXMemberExpression' ||
110
+ (nameNode?.type === 'MemberExpression' && !nameNode.computed)) &&
111
+ (nameNode.object?.type === 'JSXIdentifier' ||
112
+ nameNode.object?.type === 'Identifier') &&
113
+ (nameNode.property?.type === 'JSXIdentifier' ||
114
+ nameNode.property?.type === 'Identifier') &&
115
+ imports.namespaces.has(nameNode.object.name)
116
+ ) {
117
+ localName = nameNode.object.name;
118
+ const component = imports.normalize(nameNode.property.name);
119
+ const props = imports.components.get(component);
120
+ if (props) binding = {component, props};
121
+ }
122
+ if (!binding || !isModuleBinding(scopePath, localName)) return null;
123
+ return {...binding, localName};
124
+ }
90
125
 
91
- // A parameter or local declaration with the imported name shadows the
92
- // module binding. Only JSX tags whose root binding resolves to Program are
93
- // considered usages of the import discovered above.
94
- const bindingScope = jsxPath.scope.lookup(localName);
95
- if (!bindingScope || bindingScope.path.node.type !== 'Program') return;
126
+ /**
127
+ * @param {any} root jscodeshift collection for one source file
128
+ * @param {any} j configured jscodeshift instance
129
+ * @param {ImportOptions} options
130
+ * @param {(propPath: any, context: {component: string, localName: string}) => void} visit
131
+ */
132
+ export function transformProp(root, j, options, visit) {
133
+ const imports = importedComponents(root, j, options);
134
+ if (imports.direct.size === 0 && imports.namespaces.size === 0) return;
135
+
136
+ root.find(j.JSXOpeningElement).forEach((/** @type {any} */ jsxPath) => {
137
+ // Only JSX tags whose root binding resolves to Program are usages of the
138
+ // import discovered above.
139
+ const resolved = resolveImportedComponent(
140
+ jsxPath.node.name,
141
+ jsxPath,
142
+ imports,
143
+ );
144
+ if (!resolved) return;
145
+ const {component, props, localName} = resolved;
96
146
 
97
147
  for (let index = 0; index < jsxPath.node.attributes.length; index++) {
98
148
  const attribute = jsxPath.node.attributes[index];
@@ -2,7 +2,11 @@
2
2
 
3
3
  import {describe, expect, it} from 'vitest';
4
4
  import jscodeshift from 'jscodeshift';
5
- import {transformProp} from './transform-prop.mjs';
5
+ import {
6
+ importedComponents,
7
+ resolveImportedComponent,
8
+ transformProp,
9
+ } from './transform-prop.mjs';
6
10
 
7
11
  const j = jscodeshift.withParser('tsx');
8
12
  const components = new Map([
@@ -92,4 +96,35 @@ const b = <Group tone="old" size="old" />;`);
92
96
  expect(output).toContain('<Widget size="old" migratedtone="old"');
93
97
  expect(output).toContain('<Group tone="old" migratedsize="old"');
94
98
  });
99
+
100
+ it('resolves non-JSX references through the same import and shadow rules', () => {
101
+ const root = j(`import {Widget as Card} from '@example/core';
102
+ import * as Lib from '@example/core';
103
+ const a = Card;
104
+ const b = Lib.Group;
105
+ function shadow(Card, Lib) {
106
+ return [Card, Lib.Group];
107
+ }`);
108
+ const imports = importedComponents(root, j, {
109
+ matchesImport: value => /^@example\/core(?:\/|$)/.test(value),
110
+ components,
111
+ });
112
+ const resolved = root
113
+ .find(j.VariableDeclarator)
114
+ .paths()
115
+ .map(
116
+ path =>
117
+ resolveImportedComponent(path.node.init, path, imports)?.component,
118
+ );
119
+ expect(resolved).toEqual(['Widget', 'Group']);
120
+ const shadowed = root
121
+ .find(j.ArrayExpression)
122
+ .paths()
123
+ .flatMap(path =>
124
+ path.node.elements.map(element =>
125
+ resolveImportedComponent(element, path, imports),
126
+ ),
127
+ );
128
+ expect(shadowed).toEqual([null, null]);
129
+ });
95
130
  });
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Rename Text/Heading/Link/Timestamp color "active" to "accent"
5
- * @see https://github.com/facebook/astryx/issues/2863
5
+ * @see https://github.com/meta/astryx/issues/2863
6
6
  *
7
7
  * The `active` text-color value is renamed to `accent` and now maps to the
8
8
  * dedicated `--color-text-accent` token (legible accent ink) instead of
@@ -162,6 +162,52 @@ export default createConfig({
162
162
  });
163
163
  });
164
164
 
165
+ describe('migrate-layout-components-to-experimental — default export by const reference', () => {
166
+ it('returns null for a generated config with no layout key', async () => {
167
+ const input = `// Generated Astryx config.
168
+ const config = {
169
+ integrations: ["@acme/ui"],
170
+ issuesUrl: "https://example.com/issues",
171
+ };
172
+
173
+ export default config;
174
+ `;
175
+ const output = await applyTransform(input);
176
+ expect(output).toBeNull();
177
+ });
178
+
179
+ it('migrates layout.components in a const-bound object literal', async () => {
180
+ const input = `const config = {
181
+ layout: { components: { KpiCard: '@/components/KpiCard' } },
182
+ };
183
+
184
+ export default config;
185
+ `;
186
+ const output = await applyTransform(input);
187
+ expect(output).not.toBeNull();
188
+ expect(output).toMatch(/const config = \{/);
189
+ expect(output).toContain('export default config;');
190
+ expect(output).toMatch(/experimental:\s*\{\s*xle:\s*\{\s*components:\s*\{\s*KpiCard:\s*\{\s*from:\s*['"]@\/components\/KpiCard['"]/);
191
+ expect(output).not.toMatch(/layout\s*:/);
192
+ });
193
+
194
+ it('migrates layout.components in a const-bound createConfig({ ... }) call', async () => {
195
+ const input = `import { createConfig } from '@astryxdesign/cli';
196
+
197
+ const config = createConfig({
198
+ layout: { components: { KpiCard: '@/components/KpiCard' } },
199
+ });
200
+
201
+ export default config;
202
+ `;
203
+ const output = await applyTransform(input);
204
+ expect(output).not.toBeNull();
205
+ expect(output).toContain('createConfig(');
206
+ expect(output).toMatch(/KpiCard:\s*\{\s*from:\s*['"]@\/components\/KpiCard['"]/);
207
+ expect(output).not.toMatch(/layout\s*:/);
208
+ });
209
+ });
210
+
165
211
  describe('migrate-layout-components-to-experimental — placement', () => {
166
212
  it('adds xle to a pre-existing experimental (no xle)', async () => {
167
213
  const input = `export default {
@@ -239,8 +285,69 @@ export default {
239
285
  await expect(applyTransform(input)).rejects.toThrow(/cannot be safely migrated/);
240
286
  });
241
287
 
242
- it('throws on a non-object default export (variable reference)', async () => {
243
- const input = `const config = { layout: { components: {} } };
288
+ it('throws when the default export is an identifier with no top-level const binding', async () => {
289
+ const input = `import config from './base-config.mjs';
290
+ export default config;
291
+ `;
292
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
293
+ });
294
+
295
+ it('throws when a const-bound config is mutated after its declaration', async () => {
296
+ const input = `const config = {};
297
+ config.layout = { components: { KpiCard: '@/components/KpiCard' } };
298
+ export default config;
299
+ `;
300
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
301
+ });
302
+
303
+ it('throws when a const-bound config is passed to another call', async () => {
304
+ const input = `const config = {};
305
+ Object.assign(config, { layout: { components: { KpiCard: '@/components/KpiCard' } } });
306
+ export default config;
307
+ `;
308
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
309
+ });
310
+
311
+ it('throws when a const-bound config is aliased', async () => {
312
+ const input = `const config = {};
313
+ const alias = config;
314
+ alias.layout = { components: { KpiCard: '@/components/KpiCard' } };
315
+ export default config;
316
+ `;
317
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
318
+ });
319
+
320
+ it('throws when the const-bound config is also exported with export const', async () => {
321
+ const input = `export const config = {
322
+ layout: { components: { KpiCard: '@/components/KpiCard' } },
323
+ };
324
+ export default config;
325
+ `;
326
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
327
+ });
328
+
329
+ it('throws when the const-bound config is also exported by name', async () => {
330
+ const input = `const config = {
331
+ layout: { components: { KpiCard: '@/components/KpiCard' } },
332
+ };
333
+ export { config };
334
+ export default config;
335
+ `;
336
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
337
+ });
338
+
339
+ it('throws when the const-bound config is also exported under another name', async () => {
340
+ const input = `const config = {
341
+ layout: { components: { KpiCard: '@/components/KpiCard' } },
342
+ };
343
+ export { config as baseConfig };
344
+ export default config;
345
+ `;
346
+ await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
347
+ });
348
+
349
+ it('throws when the default export is bound by a reassignable let', async () => {
350
+ const input = `let config = { layout: { components: { KpiCard: '@/components/KpiCard' } } };
244
351
  export default config;
245
352
  `;
246
353
  await expect(applyTransform(input)).rejects.toThrow(/statically analyze/);
@@ -306,6 +413,113 @@ describe('migrate-layout-components-to-experimental — end-to-end via runConfig
306
413
  }
307
414
  });
308
415
 
416
+ it('leaves a generated const-bound config with no layout untouched and reports no error', async () => {
417
+ const {runConfigCodemod} = await import('../../../run-codemod.mjs');
418
+ const {default: transform, meta} = await import(
419
+ '../migrate-layout-components-to-experimental.mjs'
420
+ );
421
+ const jscodeshift = (await import('jscodeshift')).default;
422
+
423
+ const originalCwd = process.cwd();
424
+ const tmpDir = fs.mkdtempSync(
425
+ path.join(process.cwd(), '.astryx-v013-codemod-test-'),
426
+ );
427
+ const configSource = `// Generated Astryx config.
428
+ const config = {
429
+ integrations: ["@acme/ui"],
430
+ issuesUrl: "https://example.com/issues",
431
+ };
432
+
433
+ export default config;
434
+ `;
435
+ try {
436
+ process.chdir(tmpDir);
437
+ fs.writeFileSync(
438
+ path.join(tmpDir, 'package.json'),
439
+ JSON.stringify({name: 'consumer'}),
440
+ );
441
+ fs.writeFileSync(path.join(tmpDir, 'astryx.config.mjs'), configSource);
442
+
443
+ const entry = {
444
+ id: 'migrate-layout-components-to-experimental',
445
+ type: 'config',
446
+ codemod: {title: meta.title, transform},
447
+ package: 'core',
448
+ version: '0.1.3',
449
+ };
450
+ const log = {
451
+ step() {},
452
+ info() {},
453
+ success() {},
454
+ warn() {},
455
+ error() {},
456
+ message() {},
457
+ };
458
+ const result = runConfigCodemod(entry, {apply: true, log, jscodeshift});
459
+
460
+ expect(result.errors).toHaveLength(0);
461
+ expect(result.filesChanged).toBe(0);
462
+ expect(
463
+ fs.readFileSync(path.join(tmpDir, 'astryx.config.mjs'), 'utf-8'),
464
+ ).toBe(configSource);
465
+ } finally {
466
+ process.chdir(originalCwd);
467
+ fs.rmSync(tmpDir, {recursive: true, force: true});
468
+ }
469
+ });
470
+
471
+ it('reports an error, not success, for a mutated const-bound config', async () => {
472
+ const {runConfigCodemod} = await import('../../../run-codemod.mjs');
473
+ const {default: transform, meta} = await import(
474
+ '../migrate-layout-components-to-experimental.mjs'
475
+ );
476
+ const jscodeshift = (await import('jscodeshift')).default;
477
+
478
+ const originalCwd = process.cwd();
479
+ const tmpDir = fs.mkdtempSync(
480
+ path.join(process.cwd(), '.astryx-v013-codemod-test-'),
481
+ );
482
+ const configSource = `const config = {};
483
+ config.layout = { components: { KpiCard: '@/components/KpiCard' } };
484
+ export default config;
485
+ `;
486
+ try {
487
+ process.chdir(tmpDir);
488
+ fs.writeFileSync(
489
+ path.join(tmpDir, 'package.json'),
490
+ JSON.stringify({name: 'consumer'}),
491
+ );
492
+ fs.writeFileSync(path.join(tmpDir, 'astryx.config.mjs'), configSource);
493
+
494
+ const entry = {
495
+ id: 'migrate-layout-components-to-experimental',
496
+ type: 'config',
497
+ codemod: {title: meta.title, transform},
498
+ package: 'core',
499
+ version: '0.1.3',
500
+ };
501
+ const log = {
502
+ step() {},
503
+ info() {},
504
+ success() {},
505
+ warn() {},
506
+ error() {},
507
+ message() {},
508
+ };
509
+ const result = runConfigCodemod(entry, {apply: true, log, jscodeshift});
510
+
511
+ expect(result.filesChanged).toBe(0);
512
+ expect(result.errors).toHaveLength(1);
513
+ expect(result.errors[0].error).toMatch(/statically analyze/);
514
+ expect(
515
+ fs.readFileSync(path.join(tmpDir, 'astryx.config.mjs'), 'utf-8'),
516
+ ).toBe(configSource);
517
+ } finally {
518
+ process.chdir(originalCwd);
519
+ fs.rmSync(tmpDir, {recursive: true, force: true});
520
+ }
521
+ });
522
+
309
523
  it('surfaces a thrown bail as a structured error through runConfigCodemod', async () => {
310
524
  const {runConfigCodemod} = await import('../../../run-codemod.mjs');
311
525
  const {default: transform, meta} = await import(
@@ -28,10 +28,14 @@
28
28
  * - object `{ from, description?, default? }` → carried over UNCHANGED
29
29
  * (from/description/default preserved, key order preserved).
30
30
  *
31
- * The transform recognizes the config default export as EITHER a bare object
32
- * literal (`export default { ... }`) or a `createConfig({ ... })` call wrapping
33
- * an object literal (`export default createConfig({ ... })`). Anything else is
34
- * not statically analyzable and the transform THROWS with a clear
31
+ * The transform recognizes the config default export as a bare object literal
32
+ * (`export default { ... }`), a `createConfig({ ... })` call wrapping an object
33
+ * literal (`export default createConfig({ ... })`), or an identifier bound by a
34
+ * top-level `const` to either form (`const config = { ... }; export default
35
+ * config;`, the shape generated configs use) when the binding is not
36
+ * exported any other way and the default export is its only reference, so
37
+ * nothing can mutate the object after its declaration. Anything else is not statically analyzable and the transform
38
+ * THROWS with a clear
35
39
  * migrate-manually message (the unified config-codemod contract has no
36
40
  * structured-error return — `runConfigCodemod` surfaces a thrown `err.message`
37
41
  * as `{file, codemod, error}`).
@@ -76,10 +80,16 @@ export default function transformer(file, api) {
76
80
 
77
81
  const declaration = defaultExports.paths()[0].node.declaration;
78
82
 
79
- // Resolve the config object literal from either form:
83
+ // Resolve the config object literal from any supported form:
80
84
  // export default { ... }
81
85
  // export default createConfig({ ... })
82
- const configObject = resolveConfigObject(j, declaration);
86
+ // const config = { ... } | createConfig({ ... }); export default config;
87
+ const configObject = resolveConfigObject(
88
+ j,
89
+ j.Identifier.check(declaration)
90
+ ? findTopLevelConstInit(j, root, declaration)
91
+ : declaration,
92
+ );
83
93
  if (!configObject) {
84
94
  throw new Error(
85
95
  `could not statically analyze astryx config; ${MANUAL}`,
@@ -179,6 +189,78 @@ function resolveConfigObject(/** @type {any} */ j, /** @type {any} */ declaratio
179
189
  return null;
180
190
  }
181
191
 
192
+ /**
193
+ * Find the initializer of the top-level, non-exported `const <name> = <init>`
194
+ * declaration that the default-exported identifier refers to. `let`/`var`
195
+ * bindings can be reassigned, so they are not treated as statically
196
+ * analyzable. Neither is a binding exported any other way (`export const`,
197
+ * `export { name }`, `export { name as other }`), since another module could
198
+ * import and mutate it, nor one referenced anywhere besides its declaration
199
+ * and the default export: a member write, an alias or a call argument could
200
+ * change the object after its initializer. A named export specifier is such a
201
+ * reference, so `hasOtherReference` rejects it.
202
+ * @returns {any} the initializer node, or null if there is no such binding
203
+ */
204
+ function findTopLevelConstInit(/** @type {any} */ j, /** @type {any} */ root, /** @type {any} */ exported) {
205
+ const name = exported.name;
206
+ for (const statement of root.get().node.program.body) {
207
+ // `export const` is skipped on purpose: another module can import the
208
+ // binding and mutate the object.
209
+ if (
210
+ !j.VariableDeclaration.check(statement) ||
211
+ statement.kind !== 'const'
212
+ ) {
213
+ continue;
214
+ }
215
+ for (const declarator of statement.declarations) {
216
+ if (
217
+ j.VariableDeclarator.check(declarator) &&
218
+ j.Identifier.check(declarator.id) &&
219
+ declarator.id.name === name
220
+ ) {
221
+ return hasOtherReference(j, root, name, [declarator.id, exported])
222
+ ? null
223
+ : declarator.init ?? null;
224
+ }
225
+ }
226
+ }
227
+ return null;
228
+ }
229
+
230
+ /**
231
+ * Whether any identifier named `name`, other than the `allowed` nodes, could
232
+ * refer to the binding. Non-computed member properties (`x.name`) and object
233
+ * keys (`{ name: 1 }`) name a property, not the binding, so they don't count;
234
+ * every other occurrence (including a shadowing one) does.
235
+ * @returns {boolean}
236
+ */
237
+ function hasOtherReference(/** @type {any} */ j, /** @type {any} */ root, /** @type {string} */ name, /** @type {any[]} */ allowed) {
238
+ return root
239
+ .find(j.Identifier, {name})
240
+ .some((/** @type {any} */ path) => {
241
+ const node = path.node;
242
+ if (allowed.includes(node)) return false;
243
+ const parent = path.parent?.node;
244
+ if (
245
+ (j.MemberExpression.check(parent) ||
246
+ j.OptionalMemberExpression.check(parent)) &&
247
+ parent.property === node &&
248
+ !parent.computed
249
+ ) {
250
+ return false;
251
+ }
252
+ if (
253
+ (j.ObjectProperty.check(parent) || j.Property.check(parent)) &&
254
+ parent.key === node &&
255
+ !parent.computed &&
256
+ !parent.shorthand
257
+ ) {
258
+ return false;
259
+ }
260
+ return true;
261
+ });
262
+ }
263
+
182
264
  /**
183
265
  * Find a (non-computed, non-spread) property on an object literal by key name.
184
266
  * Supports Identifier and string-literal keys.
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Rename Switch labelSpacing "default" to "hug"
5
- * @see https://github.com/facebook/astryx/issues/2889
5
+ * @see https://github.com/meta/astryx/issues/2889
6
6
  *
7
7
  * The `labelSpacing="default"` value is renamed to `labelSpacing="hug"` so
8
8
  * the name describes the behavior (label and switch hug each other), matching
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Migrate Table tableProps to direct root props
5
- * @see https://github.com/facebook/astryx/issues/3679
5
+ * @see https://github.com/meta/astryx/issues/3679
6
6
  *
7
7
  * `tableProps` (typed HTMLAttributes<HTMLTableElement>, nesting HTML
8
8
  * attributes one level deep) is deprecated. As of v0.1.7, <Table> honors
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Rename Avatar named sizes to Icon's abbreviated scale
5
- * @see https://github.com/facebook/astryx/issues/2672
5
+ * @see https://github.com/meta/astryx/issues/2672
6
6
  *
7
7
  * This transform is deliberately limited to static literals written directly
8
8
  * in the `size` prop of an imported Avatar or AvatarGroup JSX element. It does
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Remove TabList orientation prop
5
- * @see https://github.com/facebook/astryx/issues/4231
5
+ * @see https://github.com/meta/astryx/issues/4231
6
6
  *
7
7
  * As of v0.2.0, TabList no longer accepts an `orientation` prop. The prop
8
8
  * was misleading: it only drove the keyboard-hint badge, not a real vertical
@@ -2,7 +2,7 @@
2
2
 
3
3
  /**
4
4
  * @file Codemod: Migrate Dialog position left/right -> start/end
5
- * @see https://github.com/facebook/astryx/pull/4568
5
+ * @see https://github.com/meta/astryx/pull/4568
6
6
  *
7
7
  * The Dialog `position` prop's physical `left`/`right` offsets are deprecated
8
8
  * in favor of the logical `start`/`end` offsets. `start`/`end` map to