From 96570de5f82445226e872e2851e6e29e240726ff Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Thu, 15 Dec 2022 11:49:59 +0100 Subject: [PATCH] Make tsx default parser csf-2-to-3 codemod And don't transform storybook source code with babel when running jscodeshift --- code/lib/cli/src/generate.ts | 2 +- code/lib/codemod/src/index.js | 14 ++++++++++++-- code/lib/codemod/src/transforms/csf-2-to-3.ts | 2 ++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/code/lib/cli/src/generate.ts b/code/lib/cli/src/generate.ts index 63b4f788fde6..33f8ba1b0f0e 100644 --- a/code/lib/cli/src/generate.ts +++ b/code/lib/cli/src/generate.ts @@ -107,7 +107,7 @@ program .description('Run a Storybook codemod migration on your source files') .option('-l --list', 'List available migrations') .option('-g --glob ', 'Glob for files upon which to apply the migration', '**/*.js') - .option('-p --parser ', 'jscodeshift parser') + .option('-p --parser ', 'jscodeshift parser (default: tsx)') .option( '-n --dry-run', 'Dry run: verify the migration exists and show the files to which it will be applied' diff --git a/code/lib/codemod/src/index.js b/code/lib/codemod/src/index.js index 5d44b44210da..edeedeb785d8 100644 --- a/code/lib/codemod/src/index.js +++ b/code/lib/codemod/src/index.js @@ -56,10 +56,20 @@ export async function runCodemod(codemod, { glob, logger, dryRun, rename, parser const files = await globby([glob, '!**/node_modules', '!**/dist']); logger.log(`=> Applying ${codemod}: ${files.length} files`); if (!dryRun) { - const parserArgs = inferredParser ? ['--parser', inferredParser] : []; + const parserArgs = ['--parser', inferredParser ?? 'tsx']; spawnSync( 'npx', - ['jscodeshift', '-t', `${TRANSFORM_DIR}/${codemod}.js`, ...parserArgs, ...files], + [ + 'jscodeshift', + // this makes sure codeshift doesn't transform our own source code with babel + // which is faster, and also makes sure the user won't see babel messages such as: + // [BABEL] Note: The code generator has deoptimised the styling of repo/node_modules/prettier/index.js as it exceeds the max of 500KB. + '--no-babel', + '-t', + `${TRANSFORM_DIR}/${codemod}.js`, + ...parserArgs, + ...files, + ], { stdio: 'inherit', shell: true, diff --git a/code/lib/codemod/src/transforms/csf-2-to-3.ts b/code/lib/codemod/src/transforms/csf-2-to-3.ts index 7520b7d8582b..7d8ede84b126 100644 --- a/code/lib/codemod/src/transforms/csf-2-to-3.ts +++ b/code/lib/codemod/src/transforms/csf-2-to-3.ts @@ -181,4 +181,6 @@ function transform({ source }: { source: string }, api: any, options: { parser?: }); } +export const parser = 'tsx'; + export default transform;