Skip to content

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

TypeScript

fix(cli): scope migrate-gap-to-numeric codemod to XDS layout components (#2517)

Joeyafb9fe6f13c2
diff --git a/packages/cli/src/codemods/transforms/v0.0.2/__tests__/migrate-gap-to-numeric.test.mjs b/packages/cli/src/codemods/transforms/v0.0.2/__tests__/migrate-gap-to-numeric.test.mjs
index 0d9d4b1623..45bddbab31 100644
--- a/packages/cli/src/codemods/transforms/v0.0.2/__tests__/migrate-gap-to-numeric.test.mjs
+++ b/packages/cli/src/codemods/transforms/v0.0.2/__tests__/migrate-gap-to-numeric.test.mjs
@@ -103,9 +103,59 @@ describe('migrate-gap-to-numeric', () => {
expect(output).toContain('gap="custom"');
});
- it('converts gap on any component with gap prop', async () => {
+ it('does not modify gap on user-defined components', async () => {
const input = '<CustomComponent gap="space3" />';
const output = await applyTransform(input);
- expect(output).toContain('gap={3}');
+ expect(output).toContain('gap="space3"');
+ expect(output).not.toContain('gap={3}');
+ });
+
+ it('does not modify gap on HTML elements', async () => {
+ const input = '<div gap="space4"><span /></div>';
+ const output = await applyTransform(input);
+ expect(output).toContain('gap="space4"');
+ expect(output).not.toContain('gap={4}');
+ });
+
+ it('does not modify rowGap/columnGap on non-XDS components', async () => {
+ const input =
+ '<MyGrid rowGap="space2" columnGap="space6"><div /></MyGrid>';
+ const output = await applyTransform(input);
+ expect(output).toContain('rowGap="space2"');
+ expect(output).toContain('columnGap="space6"');
+ });
+
+ it('does not modify gap on namespaced or member-expression components', async () => {
+ const input = '<Foo.Bar gap="space4"><div /></Foo.Bar>';
+ const output = await applyTransform(input);
+ expect(output).toContain('gap="space4"');
+ });
+
+ it('only rewrites XDS components in mixed files', async () => {
+ const input = `
+ <div>
+ <XDSStack gap="space2"><div /></XDSStack>
+ <CustomComponent gap="space3" />
+ <div gap="space4" />
+ </div>
+ `;
+ const output = await applyTransform(input);
+ expect(output).toContain('gap={2}');
+ expect(output).toContain('gap="space3"');
+ expect(output).toContain('gap="space4"');
+ });
+
+ it.each([
+ 'XDSStack',
+ 'XDSHStack',
+ 'XDSVStack',
+ 'XDSGrid',
+ 'XDSGridSpan',
+ 'XDSStackItem',
+ ])('rewrites gap on %s', async (component) => {
+ const input = `<${component} gap="space5"><div /></${component}>`;
+ const output = await applyTransform(input);
+ expect(output).toContain('gap={5}');
+ expect(output).not.toContain('"space5"');
});
});
diff --git a/packages/cli/src/codemods/transforms/v0.0.2/migrate-gap-to-numeric.mjs b/packages/cli/src/codemods/transforms/v0.0.2/migrate-gap-to-numeric.mjs
index 076d7f20e9..4e0f716396 100644
--- a/packages/cli/src/codemods/transforms/v0.0.2/migrate-gap-to-numeric.mjs
+++ b/packages/cli/src/codemods/transforms/v0.0.2/migrate-gap-to-numeric.mjs
@@ -43,43 +43,76 @@ const TOKEN_TO_NUMBER = {
const GAP_PROPS = ['gap', 'rowGap', 'columnGap'];
+/**
+ * Only XDS layout components accept the numeric gap scale. We must not
+ * rewrite gap props on user-defined components or HTML elements that happen
+ * to share the prop name — doing so would silently corrupt their code when
+ * `xds upgrade` runs.
+ */
+const XDS_LAYOUT_ELEMENTS = new Set([
+ 'XDSStack',
+ 'XDSHStack',
+ 'XDSVStack',
+ 'XDSGrid',
+ 'XDSGridSpan',
+ 'XDSStackItem',
+]);
+
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
let hasChanges = false;
- root.find(j.JSXAttribute).forEach((path) => {
- const attrName = path.node.name.name;
- if (!GAP_PROPS.includes(attrName)) {
+ root.find(j.JSXOpeningElement).forEach((elementPath) => {
+ const nameNode = elementPath.node.name;
+ // Only handle simple identifier element names like <XDSStack>. Skip member
+ // expressions (e.g. <Foo.Bar>) and namespaced names — they're never XDS.
+ if (!nameNode || nameNode.type !== 'JSXIdentifier') {
+ return;
+ }
+ if (!XDS_LAYOUT_ELEMENTS.has(nameNode.name)) {
return;
}
- const value = path.node.value;
-
- // Handle gap="space4" — JSX string attributes are Literal nodes
- if (value && (value.type === 'Literal' || value.type === 'StringLiteral')) {
- const numericValue = TOKEN_TO_NUMBER[value.value];
- if (numericValue !== undefined) {
- path.node.value = j.jsxExpressionContainer(
- j.literal(numericValue),
- );
- hasChanges = true;
+ for (const attr of elementPath.node.attributes ?? []) {
+ if (attr.type !== 'JSXAttribute' || !attr.name) {
+ continue;
}
- }
- // Handle gap={"space4"} (JSXExpressionContainer wrapping a string)
- if (
- value &&
- value.type === 'JSXExpressionContainer' &&
- (value.expression.type === 'Literal' ||
- value.expression.type === 'StringLiteral')
- ) {
- const strValue = value.expression.value;
- if (typeof strValue === 'string') {
- const numericValue = TOKEN_TO_NUMBER[strValue];
+ const attrName = attr.name.name;
+ if (!GAP_PROPS.includes(attrName)) {
+ continue;
+ }
+
+ const value = attr.value;
+
+ // Handle gap="space4" — JSX string attributes are Literal nodes
+ if (
+ value &&
+ (value.type === 'Literal' || value.type === 'StringLiteral')
+ ) {
+ const numericValue = TOKEN_TO_NUMBER[value.value];
if (numericValue !== undefined) {
- value.expression = j.literal(numericValue);
+ attr.value = j.jsxExpressionContainer(j.literal(numericValue));
hasChanges = true;
}
+ continue;
+ }
+
+ // Handle gap={"space4"} (JSXExpressionContainer wrapping a string)
+ if (
+ value &&
+ value.type === 'JSXExpressionContainer' &&
+ (value.expression.type === 'Literal' ||
+ value.expression.type === 'StringLiteral')
+ ) {
+ const strValue = value.expression.value;
+ if (typeof strValue === 'string') {
+ const numericValue = TOKEN_TO_NUMBER[strValue];
+ if (numericValue !== undefined) {
+ value.expression = j.literal(numericValue);
+ hasChanges = true;
+ }
+ }
}
}
});