Skip to content

Commit

Permalink
Infer prettier parser from filepath, and continue even when failing
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Dec 15, 2022
1 parent 96570de commit d7dbb6f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions code/lib/codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 22 additions & 17 deletions code/lib/codemod/src/transforms/csf-2-to-3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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';
};
Expand Down Expand Up @@ -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;
15 changes: 13 additions & 2 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d7dbb6f

Please sign in to comment.