Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(cli): scope authoring factory codemod imports (#6335)

Joey60b419fb530c
diff --git a/.changeset/restrict-the-authoring-factory-codemod-to-astryx-ap0k.md b/.changeset/restrict-the-authoring-factory-codemod-to-astryx-ap0k.md
new file mode 100644
index 0000000000..6485baf42e
--- /dev/null
+++ b/.changeset/restrict-the-authoring-factory-codemod-to-astryx-ap0k.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] Restrict the authoring factory codemod to Astryx imports
+@josephfarina
diff --git a/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/unwrap-authoring-factories.test.mjs b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/unwrap-authoring-factories.test.mjs
index 40e9468416..3a9bcbdbce 100644
--- a/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/unwrap-authoring-factories.test.mjs
+++ b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/unwrap-authoring-factories.test.mjs
@@ -3,7 +3,8 @@
import {describe, it, expect} from 'vitest';
async function applyTransform(source, path = 'test.ts') {
- const {default: transform} = await import('../unwrap-authoring-factories.mjs');
+ const {default: transform} =
+ await import('../unwrap-authoring-factories.mjs');
const jscodeshift = (await import('jscodeshift')).default;
const j = jscodeshift.withParser('tsx');
const api = {jscodeshift: j, stats: () => {}, report: () => {}};
@@ -18,7 +19,7 @@ export default createConfig({integrations: ['@acme/widgets']});
`;
const output = await applyTransform(input);
expect(output).not.toContain('createConfig');
- expect(output).toContain("export default {");
+ expect(output).toContain('export default {');
expect(output).toContain("integrations: ['@acme/widgets']");
expect(output).not.toContain('type:');
});
@@ -132,13 +133,34 @@ export default createComponentDoc();
expect(output).toContain("type: 'component'");
});
+ it('leaves same-named factories from unrelated packages unchanged', async () => {
+ const input = `import {createConfig} from '@acme/eslint';
+export default createConfig({strict: true});
+`;
+ expect(await applyTransform(input)).toBe(input);
+ });
+
+ it('keeps an unrelated same-named import when another factory is migrated', async () => {
+ const input = `import {createConfig as createLintConfig} from '@acme/eslint';
+import {createDoc} from '@astryxdesign/cli/doc';
+export const lintConfig = createLintConfig({strict: true});
+export const doc = createDoc({name: 'Theming', description: 'How theming works.'});
+`;
+ const output = await applyTransform(input);
+ expect(output).toContain(
+ "import {createConfig as createLintConfig} from '@acme/eslint'",
+ );
+ expect(output).toContain('createLintConfig({strict: true})');
+ expect(output).not.toContain('createDoc');
+ expect(output).toContain("type: 'generic'");
+ });
+
it('is a no-op when no authoring factory is imported', async () => {
const input = `import {Button} from '@astryxdesign/core';
export default Button;
`;
- const {default: transform} = await import(
- '../unwrap-authoring-factories.mjs'
- );
+ const {default: transform} =
+ await import('../unwrap-authoring-factories.mjs');
const jscodeshift = (await import('jscodeshift')).default;
const j = jscodeshift.withParser('tsx');
const api = {jscodeshift: j, stats: () => {}, report: () => {}};
diff --git a/packages/cli/assets/codemods/transforms/v0.3.0/unwrap-authoring-factories.mjs b/packages/cli/assets/codemods/transforms/v0.3.0/unwrap-authoring-factories.mjs
index 3da2dca51b..177b436b3e 100644
--- a/packages/cli/assets/codemods/transforms/v0.3.0/unwrap-authoring-factories.mjs
+++ b/packages/cli/assets/codemods/transforms/v0.3.0/unwrap-authoring-factories.mjs
@@ -5,8 +5,9 @@
*
* v0.3.0 removes the authoring factories. Authoring is now types + parsers: an
* author writes a plain object and stamps its `type` directly. This transform
- * rewrites every factory call to the plain object the factory used to return,
- * then drops the now-dead factory imports:
+ * rewrites factory calls imported from the retired Astryx authoring entrypoints
+ * to the plain object the factory used to return, then drops the now-dead
+ * factory imports:
*
* createConfig(o) / createIntegration(o) -> o (no discriminant)
* createComponentDoc(o) -> { ...o, type: 'component' }
@@ -25,8 +26,9 @@
*
* Import aliases are followed (`import {createDoc as mk}` → calls to `mk`), and
* the factory specifiers are removed afterward (the whole import statement goes
- * if nothing else was imported from it). Run this BEFORE
- * `migrate-authoring-imports`, which repoints the surviving type imports.
+ * if nothing else was imported from it). Same-named imports from other packages
+ * remain untouched. Run this BEFORE `migrate-authoring-imports`, which repoints
+ * the surviving type imports.
*/
export const meta = {
@@ -35,13 +37,24 @@ export const meta = {
'Rewrites createConfig/createIntegration/createComponentDoc/' +
'createFunctionDoc/createDoc/createPageTemplate/createBlockTemplate/' +
'createCodemod/createConfigCodemod calls to the plain object they returned ' +
- "(stamping the doc/template/codemod `type` discriminant), and removes the " +
+ '(stamping the doc/template/codemod `type` discriminant), and removes the ' +
'now-dead factory imports. Authoring is types + parsers in v0.3.0 — there ' +
'are no factories.',
pr: '#4612',
fileExtensions: ['.js', '.jsx', '.ts', '.tsx', '.mjs', '.cjs'],
};
+/** Legacy Astryx authoring entrypoints that exported the removed factories. */
+const AUTHORING_SOURCES = new Set([
+ '@astryxdesign/cli/config',
+ '@astryxdesign/cli/doc',
+ '@astryxdesign/cli/integration',
+ '@astryxdesign/cli/template',
+ '@astryxdesign/cli/codemod',
+ '@astryxdesign/core/authoring',
+ '@astryxdesign/core/config',
+]);
+
/**
* Factory name → the `type` discriminant it stamped, or `null` for the config /
* integration factories, which were pure typed-identity (no discriminant).
@@ -108,6 +121,7 @@ export default function transformer(file, api) {
/** @type {Map<string, string>} */
const localToFactory = new Map();
root.find(j.ImportDeclaration).forEach((/** @type {any} */ path) => {
+ if (!AUTHORING_SOURCES.has(path.node.source.value)) return;
for (const spec of path.node.specifiers ?? []) {
if (spec.type !== 'ImportSpecifier') continue;
const importedName = spec.imported?.name;
@@ -163,6 +177,7 @@ export default function transformer(file, api) {
// Drop the now-dead factory import specifiers; remove any import statement
// left empty.
root.find(j.ImportDeclaration).forEach((/** @type {any} */ path) => {
+ if (!AUTHORING_SOURCES.has(path.node.source.value)) return;
const specs = path.node.specifiers ?? [];
const kept = specs.filter(
(/** @type {any} */ spec) =>