From d7dbb6f8c24f5d55c4c54d3aafc3a8c3f11578e1 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Thu, 15 Dec 2022 16:18:06 +0100 Subject: [PATCH] Infer prettier parser from filepath, and continue even when failing --- code/lib/codemod/package.json | 1 + code/lib/codemod/src/transforms/csf-2-to-3.ts | 39 +++++++++++-------- code/yarn.lock | 15 ++++++- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/code/lib/codemod/package.json b/code/lib/codemod/package.json index 58943de21033..e83b31ba0ef4 100644 --- a/code/lib/codemod/package.json +++ b/code/lib/codemod/package.json @@ -60,6 +60,7 @@ "util": "^0.12.4" }, "devDependencies": { + "@types/jscodeshift": "^0.11.6", "jest": "^29.3.1", "jest-specific-snapshot": "^7.0.0", "typescript": "~4.9.3" 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 7d8ede84b126..8a2a5c85074b 100644 --- a/code/lib/codemod/src/transforms/csf-2-to-3.ts +++ b/code/lib/codemod/src/transforms/csf-2-to-3.ts @@ -3,7 +3,7 @@ import prettier from 'prettier'; import * as t from '@babel/types'; import type { CsfFile } from '@storybook/csf-tools'; import { formatCsf, loadCsf } from '@storybook/csf-tools'; -import { jscodeshiftToPrettierParser } from '../lib/utils'; +import type { API, FileInfo, Options } from 'jscodeshift'; const logger = console; @@ -89,7 +89,7 @@ const isReactGlobalRenderFn = (csf: CsfFile, storyFn: t.Expression) => { const isSimpleCSFStory = (init: t.Expression, annotations: t.ObjectProperty[]) => annotations.length === 0 && t.isArrowFunctionExpression(init) && init.params.length === 0; -function transform({ source }: { source: string }, api: any, options: { parser?: string }) { +export default function transform({ source, path }: FileInfo, api: API, options: Options) { const makeTitle = (userTitle?: string) => { return userTitle || 'FIXME'; }; @@ -165,22 +165,27 @@ function transform({ source }: { source: string }, api: any, options: { parser?: return acc; }, []); csf._ast.program.body = updatedBody; - const output = formatCsf(csf); - - const prettierConfig = prettier.resolveConfig.sync('.', { editorconfig: true }) || { - printWidth: 100, - tabWidth: 2, - bracketSpacing: true, - trailingComma: 'es5', - singleQuote: true, - }; + let output = formatCsf(csf); - return prettier.format(output, { - ...prettierConfig, - parser: jscodeshiftToPrettierParser(options?.parser), - }); + try { + const prettierConfig = prettier.resolveConfig.sync('.', { editorconfig: true }) || { + printWidth: 100, + tabWidth: 2, + bracketSpacing: true, + trailingComma: 'es5', + singleQuote: true, + }; + + output = prettier.format(output, { + ...prettierConfig, + // This will infer the parser from the filename. + filepath: path, + }); + } catch (e) { + logger.log(`Failed applying prettier to ${path}.`); + } + + return output; } export const parser = 'tsx'; - -export default transform; diff --git a/code/yarn.lock b/code/yarn.lock index 195ffe33b534..43e1c204743b 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -6253,6 +6253,7 @@ __metadata: "@storybook/csf-tools": 7.0.0-beta.8 "@storybook/node-logger": 7.0.0-beta.8 "@storybook/types": 7.0.0-beta.8 + "@types/jscodeshift": ^0.11.6 cross-spawn: ^7.0.3 globby: ^11.0.2 jest: ^29.3.1 @@ -8479,6 +8480,16 @@ __metadata: languageName: node linkType: hard +"@types/jscodeshift@npm:^0.11.6": + version: 0.11.6 + resolution: "@types/jscodeshift@npm:0.11.6" + dependencies: + ast-types: ^0.14.1 + recast: ^0.20.3 + checksum: 1d204a4c3d9f52669e315dfbc1e65434ec55ee884574306d35048b89ef83b625c64d510228b6aabbd4248af566e02e0ce9de0aa8ccdfff696c69fbaced7007e7 + languageName: node + linkType: hard + "@types/jsdom@npm:^16.2.4": version: 16.2.15 resolution: "@types/jsdom@npm:16.2.15" @@ -10770,7 +10781,7 @@ __metadata: languageName: node linkType: hard -"ast-types@npm:0.14.2, ast-types@npm:^0.14.2": +"ast-types@npm:0.14.2, ast-types@npm:^0.14.1, ast-types@npm:^0.14.2": version: 0.14.2 resolution: "ast-types@npm:0.14.2" dependencies: @@ -28020,7 +28031,7 @@ __metadata: languageName: node linkType: hard -"recast@npm:^0.20.4": +"recast@npm:^0.20.3, recast@npm:^0.20.4": version: 0.20.5 resolution: "recast@npm:0.20.5" dependencies: