An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): migrate-grid-minchildwidth-to-columns handles columns + minChildWidth combo (#4792)
EJ Hammond683846bf280f
diff --git a/.changeset/cli-fix-grid-minchildwidth-columns-combo.md b/.changeset/cli-fix-grid-minchildwidth-columns-combo.mdnew file mode 100644index 0000000000..c20e48dd56--- /dev/null+++ b/.changeset/cli-fix-grid-minchildwidth-columns-combo.md@@ -0,0 +1,9 @@+---+'@astryxdesign/cli': patch+---++[fix] The `migrate-grid-minchildwidth-to-columns` codemod bailed without changes when a `<Grid>` had both `columns` and `minChildWidth`, leaving the now-invalid `minChildWidth` prop in place and failing type-checking on 0.3.0.++When `columns` is a numeric literal, it now migrates losslessly to the 0.3.0 object form. This mirrors the old (0.2.0) Grid runtime, where `minChildWidth` dominated and the numeric `columns` capped the column count under `auto-fit`: `<Grid columns={3} minChildWidth={280}>` becomes `<Grid columns={{minWidth: 280, max: 3, repeat: 'fit'}}>`. Object or dynamic `columns` values remain a deliberate bail.++@ejhammonddiff --git a/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/migrate-grid-minchildwidth-to-columns.test.mjs b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/migrate-grid-minchildwidth-to-columns.test.mjsnew file mode 100644index 0000000000..95883d65c8--- /dev/null+++ b/packages/cli/assets/codemods/transforms/v0.3.0/__tests__/migrate-grid-minchildwidth-to-columns.test.mjs@@ -0,0 +1,82 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++import {describe, it, expect} from 'vitest';++async function applyTransform(source) {+ const {default: transform} =+ await import('../migrate-grid-minchildwidth-to-columns.mjs');+ const jscodeshift = (await import('jscodeshift')).default;+ const j = jscodeshift.withParser('tsx');+ const api = {jscodeshift: j, stats: () => {}, report: () => {}};+ const file = {source, path: 'test.tsx'};+ const result = transform(file, api);+ return result ?? source;+}++/** Collapse whitespace (and space padding inside braces) so formatting of+ * pretty-printed multiline objects does not matter for assertions. */+function normalize(str) {+ return str+ .replace(/\s+/g, ' ')+ .replace(/\{\s+/g, '{')+ .replace(/\s+\}/g, '}')+ .trim();+}++describe('migrate-grid-minchildwidth-to-columns', () => {+ it('migrates minChildWidth only to columns={{minWidth, repeat: "fit"}}', async () => {+ const input = `import {Grid} from '@astryxdesign/core';+const t = <Grid minChildWidth={280}><Item /></Grid>;`;+ const output = await applyTransform(input);+ expect(output).not.toContain('minChildWidth');+ expect(normalize(output)).toContain(+ normalize("columns={{minWidth: 280, repeat: 'fit'}}"),+ );+ });++ it('migrates columns={n} + minChildWidth to columns={{minWidth, max, repeat: "fit"}}', async () => {+ const input = `import {Grid} from '@astryxdesign/core';+const t = <Grid columns={3} minChildWidth={280}><Item /></Grid>;`;+ const output = await applyTransform(input);+ expect(output).not.toContain('minChildWidth');+ expect(normalize(output)).toContain(+ normalize("columns={{minWidth: 280, max: 3, repeat: 'fit'}}"),+ );+ });++ it('preserves other attributes (gap) when migrating the combo', async () => {+ const input = `import {Grid} from '@astryxdesign/core';+const t = <Grid columns={5} gap={5} minChildWidth={140}><Item /></Grid>;`;+ const output = await applyTransform(input);+ expect(output).not.toContain('minChildWidth');+ expect(normalize(output)).toContain(+ normalize("columns={{minWidth: 140, max: 5, repeat: 'fit'}}"),+ );+ expect(output).toContain('gap={5}');+ });++ it('bails when columns is already an object (ambiguous)', async () => {+ const input = `import {Grid} from '@astryxdesign/core';+const t = <Grid columns={{minWidth: 100}} minChildWidth={280}><Item /></Grid>;`;+ const output = await applyTransform(input);+ // Unchanged: object columns is ambiguous, so it is left as-is.+ expect(output).toBe(input);+ });++ it('bails when columns is a non-numeric/dynamic expression', async () => {+ const input = `import {Grid} from '@astryxdesign/core';+const t = <Grid columns={cols} minChildWidth={280}><Item /></Grid>;`;+ const output = await applyTransform(input);+ expect(output).toBe(input);+ });++ it('is alias-aware and supports the subpath import source', async () => {+ const input = `import {Grid as G} from '@astryxdesign/core/Grid';+const t = <G columns={4} minChildWidth={200}><Item /></G>;`;+ const output = await applyTransform(input);+ expect(output).not.toContain('minChildWidth');+ expect(normalize(output)).toContain(+ normalize("columns={{minWidth: 200, max: 4, repeat: 'fit'}}"),+ );+ });+});diff --git a/packages/cli/assets/codemods/transforms/v0.3.0/migrate-grid-minchildwidth-to-columns.mjs b/packages/cli/assets/codemods/transforms/v0.3.0/migrate-grid-minchildwidth-to-columns.mjsindex 46d6dd5feb..20ccc9d49d 100644--- a/packages/cli/assets/codemods/transforms/v0.3.0/migrate-grid-minchildwidth-to-columns.mjs+++ b/packages/cli/assets/codemods/transforms/v0.3.0/migrate-grid-minchildwidth-to-columns.mjs@@ -49,11 +49,10 @@ export default function transformer(file, api) {a.type === 'JSXAttribute' && a.name?.name === 'minChildWidth',);if (!minAttr) return;- const hasColumns = attrs.some(+ const columnsAttr = attrs.find((/** @type {any} */ a) =>a.type === 'JSXAttribute' && a.name?.name === 'columns',);- if (hasColumns) return;let minWidthExpr;if (@@ -67,6 +66,43 @@ export default function transformer(file, api) {return;}+ if (columnsAttr) {+ // Both `columns` and `minChildWidth` are present. This is only safely+ // migratable when `columns` is a numeric literal (e.g. `columns={3}`).+ //+ // In the old (0.2.0) Grid runtime, when both props were set and+ // `columns` was a number, `minChildWidth` dominated and the numeric+ // `columns` became a *max column cap* with `auto-fit`:+ // repeat(auto-fit, minmax(minChildWidth, 1fr)) capped at `columns`.+ // The 0.3.0 `columns` object models this exactly as+ // {minWidth, max, repeat: 'fit'}.+ // So the lossless rewrite is:+ // <Grid columns={3} minChildWidth={280}>+ // -> <Grid columns={{minWidth: 280, max: 3, repeat: 'fit'}}>+ //+ // If `columns` is already an object or a non-numeric/dynamic+ // expression, the combined behavior is genuinely ambiguous, so we+ // bail and leave it unmigrated (it surfaces separately).+ const columnsValue = columnsAttr.value;+ if (columnsValue?.type !== 'JSXExpressionContainer') return;+ const columnsExpr = columnsValue.expression;+ const isNumericLiteral =+ (columnsExpr?.type === 'NumericLiteral' ||+ columnsExpr?.type === 'Literal') &&+ typeof columnsExpr.value === 'number';+ if (!isNumericLiteral) return;++ const columnsObject = j.objectExpression([+ j.property('init', j.identifier('minWidth'), minWidthExpr),+ j.property('init', j.identifier('max'), j.literal(columnsExpr.value)),+ j.property('init', j.identifier('repeat'), j.literal('fit')),+ ]);+ columnsAttr.value = j.jsxExpressionContainer(columnsObject);+ attrs.splice(attrs.indexOf(minAttr), 1);+ hasChanges = true;+ return;+ }+const columnsObject = j.objectExpression([j.property('init', j.identifier('minWidth'), minWidthExpr),j.property('init', j.identifier('repeat'), j.literal('fit')),