Skip to content

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

TypeScript

fix: double semicolon after 'use client' in codemod output (#1346) (#1349)

Cindy Zhange07bdbe4c70b
diff --git a/packages/cli/src/codemods/__tests__/validation.test.mjs b/packages/cli/src/codemods/__tests__/validation.test.mjs
index 7992e1d1d2..d9fa2da3ae 100644
--- a/packages/cli/src/codemods/__tests__/validation.test.mjs
+++ b/packages/cli/src/codemods/__tests__/validation.test.mjs
@@ -1,5 +1,5 @@
import {describe, it, expect} from 'vitest';
-import {validateOutput} from '../runner.mjs';
+import {validateOutput, fixDirectiveCorruption} from '../runner.mjs';
async function getJscodeshift(parser = 'tsx') {
const jscodeshift = (await import('jscodeshift')).default;
@@ -67,3 +67,40 @@ const x = someValue.function toString() { [native code] }();
expect(validation.reason).toMatch(/corruption/);
});
});
+
+describe('fixDirectiveCorruption', () => {
+ it('fixes double semicolon on use client', () => {
+ const input = "'use client';;\nimport {X} from 'y';";
+ expect(fixDirectiveCorruption(input)).toBe("'use client';\nimport {X} from 'y';");
+ });
+
+ it('fixes double semicolon on use server', () => {
+ const input = "'use server';;\nimport {X} from 'y';";
+ expect(fixDirectiveCorruption(input)).toBe("'use server';\nimport {X} from 'y';");
+ });
+
+ it('fixes double semicolon on use strict', () => {
+ const input = "'use strict';;\nvar x = 1;";
+ expect(fixDirectiveCorruption(input)).toBe("'use strict';\nvar x = 1;");
+ });
+
+ it('handles double-quoted directives', () => {
+ const input = '"use client";;\nimport {X} from "y";';
+ expect(fixDirectiveCorruption(input)).toBe('"use client";\nimport {X} from "y";');
+ });
+
+ it('does not modify correct directives', () => {
+ const input = "'use client';\nimport {X} from 'y';";
+ expect(fixDirectiveCorruption(input)).toBe(input);
+ });
+
+ it('does not modify double semicolons elsewhere', () => {
+ const input = "for (;;) {}";
+ expect(fixDirectiveCorruption(input)).toBe(input);
+ });
+
+ it('handles whitespace between semicolons', () => {
+ const input = "'use client'; ;\nimport {X} from 'y';";
+ expect(fixDirectiveCorruption(input)).toBe("'use client';\nimport {X} from 'y';");
+ });
+});
diff --git a/packages/cli/src/codemods/runner.mjs b/packages/cli/src/codemods/runner.mjs
index 0dc68448d5..c098f19019 100644
--- a/packages/cli/src/codemods/runner.mjs
+++ b/packages/cli/src/codemods/runner.mjs
@@ -18,6 +18,27 @@ const CORRUPTION_PATTERNS = [
[/function \w+\(\) \{ \[native code\] \}/g, 'native function toString() leak'],
];
+/**
+ * Fix jscodeshift directive corruption.
+ *
+ * jscodeshift has a known bug where toSource() double-prints the semicolon
+ * on directive prologues ('use client';, 'use server';, 'use strict';)
+ * when the AST has been modified with new nodes nearby. The parser treats
+ * the directive as an ExpressionStatement with a StringLiteral, and the
+ * printer emits both the original semicolon and a new one for the statement.
+ *
+ * This is applied in the runner so every codemod gets the fix automatically.
+ *
+ * @param {string} code
+ * @returns {string}
+ */
+export function fixDirectiveCorruption(code) {
+ return code.replace(
+ /^(['"]use (client|server|strict)['"]);\s*;/gm,
+ '$1;',
+ );
+}
+
/**
* Recursively find all source files in a directory.
* @param {string} dir
@@ -190,9 +211,12 @@ export async function runCodemods(versionManifests, {apply, path: srcPath, codem
};
const file = {source, path: filePath};
- const result = transform(file, api);
+ let result = transform(file, api);
if (result != null && result !== source) {
+ // Fix known jscodeshift output corruption before validation
+ result = fixDirectiveCorruption(result);
+
// Validate output before writing
const validation = validateOutput(result, source, j);
if (!validation.valid) {
diff --git a/packages/cli/src/codemods/transforms/v0.0.12/__tests__/add-is-icon-only.test.mjs b/packages/cli/src/codemods/transforms/v0.0.12/__tests__/add-is-icon-only.test.mjs
index c04a3cbd54..cb267d48ce 100644
--- a/packages/cli/src/codemods/transforms/v0.0.12/__tests__/add-is-icon-only.test.mjs
+++ b/packages/cli/src/codemods/transforms/v0.0.12/__tests__/add-is-icon-only.test.mjs
@@ -145,6 +145,24 @@ function Foo() {
expect(output).toContain("'use client'");
});
+ it('may produce double semicolons from jscodeshift bug (runner fixes this)', async () => {
+ // jscodeshift has a known bug where inserting new imports corrupts
+ // directive prologues. The runner's fixDirectiveCorruption() handles
+ // this — see validation.test.mjs for runner-level tests.
+ const input = `'use client';
+
+import {XDSButton} from '@xds/core/Button';
+import {SomeIcon} from '@heroicons/react/24/outline';
+
+export function F() {
+ return <><XDSButton label="X" icon={<I />} /><XDSButton label="Y" variant="primary" /></>;
+}`;
+ const output = await applyTransform(input);
+ // The transform itself works correctly
+ expect(output).toContain('XDSIconButton');
+ expect(output).toContain('XDSButton');
+ });
+
it('splits type imports to correct module when replacing Button import', async () => {
const input = `import {XDSButton, type XDSButtonProps} from '@xds/core/Button';
diff --git a/packages/cli/src/codemods/transforms/v0.0.12/add-is-icon-only.mjs b/packages/cli/src/codemods/transforms/v0.0.12/add-is-icon-only.mjs
index a577a00e7a..37c4f025dd 100644
--- a/packages/cli/src/codemods/transforms/v0.0.12/add-is-icon-only.mjs
+++ b/packages/cli/src/codemods/transforms/v0.0.12/add-is-icon-only.mjs
@@ -55,6 +55,24 @@ export default function transformer(file, api) {
let needsIconButtonImport = false;
let hasRemainingXDSButton = false;
+ // Insert a new node after a target node in the program body.
+ // Uses direct body manipulation instead of jscodeshift's insertAfter(),
+ // which has a known bug that corrupts 'use client' directives into
+ // double semicolons ('use client';;).
+ function insertAfterInBody(targetPath, newNode) {
+ const body = root.get().node.program.body;
+ const targetIndex = body.indexOf(targetPath.node);
+ if (targetIndex !== -1) {
+ body.splice(targetIndex + 1, 0, newNode);
+ } else {
+ // Fallback: insert after all imports
+ const lastImportIdx = body.findLastIndex(
+ (n) => n.type === 'ImportDeclaration',
+ );
+ body.splice(lastImportIdx + 1, 0, newNode);
+ }
+ }
+
// ---- 1. JSX elements: convert icon-only XDSButton to XDSIconButton ----
root.find(j.JSXOpeningElement).forEach((path) => {
const name = path.node.name;
@@ -248,7 +266,7 @@ export default function transformer(file, api) {
[j.importSpecifier(j.identifier('XDSIconButton'))],
j.stringLiteral('@xds/core/IconButton'),
);
- path.insertAfter(newImport);
+ insertAfterInBody(path, newImport);
}
alreadyImported = true;
});
@@ -279,11 +297,11 @@ export default function transformer(file, api) {
.filter((p) => p.node.source.value.startsWith('@xds/'));
if (xdsImports.length > 0) {
- xdsImports.at(-1).insertAfter(newImport);
+ insertAfterInBody(xdsImports.at(-1), newImport);
} else {
const allImports = root.find(j.ImportDeclaration);
if (allImports.length > 0) {
- allImports.at(-1).insertAfter(newImport);
+ insertAfterInBody(allImports.at(-1), newImport);
} else {
root.get().node.program.body.unshift(newImport);
}