An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): rename Resizable pixel bounds in namespace and as-const configs (#6541)
Joey15b5a921a57b
diff --git a/.changeset/cli-spec-resizable-codemod-inline-forms.md b/.changeset/cli-spec-resizable-codemod-inline-forms.mdnew file mode 100644index 0000000000..5615f2fd4b--- /dev/null+++ b/.changeset/cli-spec-resizable-codemod-inline-forms.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] The 0.6 `rename-resizable-pixel-bounds` upgrade codemod now also renames `minSizePx`/`maxSizePx` in static inline `useResizable` configurations called through a namespace import (`Astryx.useResizable({...})`) or wrapped in `as const` or `satisfies`. These were left unchanged before.++@josephfarinadiff --git a/packages/cli/assets/codemods/transforms/v0.6.0/__tests__/next-codemods.test.mjs b/packages/cli/assets/codemods/transforms/v0.6.0/__tests__/next-codemods.test.mjsindex bea1642036..dacd265b19 100644--- a/packages/cli/assets/codemods/transforms/v0.6.0/__tests__/next-codemods.test.mjs+++ b/packages/cli/assets/codemods/transforms/v0.6.0/__tests__/next-codemods.test.mjs@@ -220,4 +220,51 @@ function wrapper(useResizable) {const output = await apply('rename-resizable-pixel-bounds', input);expect(output).toBe(input);});++ it('renames bounds when useResizable comes from a namespace import', async () => {+ const input = `import * as Astryx from '@astryxdesign/core';+const region = Astryx.useResizable({defaultSize: 240, minSizePx: 120, maxSizePx: 480});`;+ const output = await apply('rename-resizable-pixel-bounds', input);+ expect(output).toContain('minSize: 120');+ expect(output).toContain('maxSize: 480');+ expect(output).not.toContain('minSizePx');+ expect(output).not.toContain('maxSizePx');+ });++ it('leaves a namespace that does not come from Astryx alone', async () => {+ const input = `import * as Other from 'other-resizable';+const region = Other.useResizable({minSizePx: 120});`;+ const output = await apply('rename-resizable-pixel-bounds', input);+ expect(output).toContain('minSizePx: 120');+ });++ it('does not rewrite a parameter that shadows the namespace import', async () => {+ const input = `import * as Astryx from '@astryxdesign/core';+function build(Astryx) {+ return Astryx.useResizable({minSizePx: 120});+}`;+ const output = await apply('rename-resizable-pixel-bounds', input);+ expect(output).toContain('minSizePx: 120');+ });++ it.each([+ ['as const', '{defaultSize: 240, minSizePx: 120} as const'],+ ['satisfies', '{defaultSize: 240, minSizePx: 120} satisfies object'],+ ])('renames a static configuration wrapped in %s', async (_label, config) => {+ const input = `import {useResizable} from '@astryxdesign/core/Resizable';+const region = useResizable(${config});`;+ const output = await apply('rename-resizable-pixel-bounds', input);+ expect(output).toContain('minSize: 120');+ expect(output).not.toContain('minSizePx');+ });++ it('renames wrapped multi-region configurations', async () => {+ const input = `import {useResizable} from '@astryxdesign/core';+const regions = useResizable({regions: {+ nav: {defaultSize: 240, minSizePx: 120} as const,+} as const});`;+ const output = await apply('rename-resizable-pixel-bounds', input);+ expect(output).toContain('minSize: 120');+ expect(output).not.toContain('minSizePx');+ });});diff --git a/packages/cli/assets/codemods/transforms/v0.6.0/rename-resizable-pixel-bounds.mjs b/packages/cli/assets/codemods/transforms/v0.6.0/rename-resizable-pixel-bounds.mjsindex 4b9069f093..36495bb418 100644--- a/packages/cli/assets/codemods/transforms/v0.6.0/rename-resizable-pixel-bounds.mjs+++ b/packages/cli/assets/codemods/transforms/v0.6.0/rename-resizable-pixel-bounds.mjs@@ -8,6 +8,9 @@* released conflict rule (the unified property wins when both are present).* A spread or computed property can change precedence invisibly, so dynamic* objects stay unchanged and receive a manual-migration TODO instead.+ * Named and namespace imports both count, and a type-only wrapper (`as`,+ * `satisfies`, a non-null assertion, parentheses) does not make a+ * configuration dynamic.*/export const meta = {@@ -33,6 +36,26 @@ const MANUAL_MIGRATION_COMMENT ='or computed property. Rename minSizePx/maxSizePx to minSize/maxSize ' +'manually without changing property precedence. ';+const TYPE_WRAPPERS = new Set([+ 'TSAsExpression',+ 'TSSatisfiesExpression',+ 'TSTypeAssertion',+ 'TSNonNullExpression',+ 'ParenthesizedExpression',+]);++/**+ * The expression under type-only wrappers such as `{...} as const`.+ * @param {any} node+ */+function unwrap(node) {+ let current = node;+ while (current && TYPE_WRAPPERS.has(current.type)) {+ current = current.expression;+ }+ return current;+}+/** @param {any} property */function staticPropertyName(property) {if (property.computed) return null;@@ -128,6 +151,8 @@ export default function transformer(file, api) {const root = j(file.source);/** @type {Map<string, any>} */const bindings = new Map();+ /** @type {Map<string, any>} */+ const namespaces = new Map();root.find(j.ImportDeclaration).forEach((/** @type {any} */ path) => {if (!IMPORT_SOURCES.has(path.node.source.value)) return;@@ -138,17 +163,40 @@ export default function transformer(file, api) {) {const local = specifier.local?.name ?? 'useResizable';bindings.set(local, path.scope.lookup(local) ?? path.scope);+ } else if (specifier.type === 'ImportNamespaceSpecifier') {+ const local = specifier.local.name;+ namespaces.set(local, path.scope.lookup(local) ?? path.scope);}}});- if (bindings.size === 0) return undefined;+ if (bindings.size === 0 && namespaces.size === 0) return undefined;++ /**+ * @param {any} callee+ * @param {any} scope+ */+ const isUseResizable = (callee, scope) => {+ if (callee.type === 'Identifier') {+ return (+ bindings.has(callee.name) &&+ scope.lookup(callee.name) === bindings.get(callee.name)+ );+ }+ return (+ callee.type === 'MemberExpression' &&+ !callee.computed &&+ callee.object.type === 'Identifier' &&+ callee.property.type === 'Identifier' &&+ callee.property.name === 'useResizable' &&+ namespaces.has(callee.object.name) &&+ scope.lookup(callee.object.name) === namespaces.get(callee.object.name)+ );+ };let changed = false;root.find(j.CallExpression).forEach((/** @type {any} */ path) => {- const callee = path.node.callee;- if (callee.type !== 'Identifier' || !bindings.has(callee.name)) return;- if (path.scope.lookup(callee.name) !== bindings.get(callee.name)) return;- const config = path.node.arguments[0];+ if (!isUseResizable(path.node.callee, path.scope)) return;+ const config = unwrap(path.node.arguments[0]);if (config?.type !== 'ObjectExpression') return;changed = migrateObject(j, config) || changed;@@ -158,13 +206,12 @@ export default function transformer(file, api) {staticPropertyName(property) === 'regions',);if (!regions || !('value' in regions)) return;- const regionsObject = regions.value;+ const regionsObject = unwrap(regions.value);if (regionsObject?.type !== 'ObjectExpression') return;for (const region of regionsObject.properties) {- if (!('value' in region) || region.value?.type !== 'ObjectExpression') {- continue;- }- changed = migrateObject(j, region.value) || changed;+ const regionConfig = 'value' in region ? unwrap(region.value) : null;+ if (regionConfig?.type !== 'ObjectExpression') continue;+ changed = migrateObject(j, regionConfig) || changed;}});