Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Fix storiesof-to-csf codemod for TypeScript #12453

Merged
merged 3 commits into from Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/codemod/src/lib/utils.js
Expand Up @@ -13,3 +13,14 @@ export const sanitizeName = (name) => {
}
return key;
};

export function jscodeshiftToPrettierParser(parser) {
const parserMap = {
babylon: 'babel',
flow: 'flow',
ts: 'typescript',
tsx: 'typescript',
};

return parserMap[parser] || 'babel';
}
19 changes: 12 additions & 7 deletions lib/codemod/src/transforms/storiesof-to-csf.js
@@ -1,7 +1,7 @@
import prettier from 'prettier';
import { logger } from '@storybook/node-logger';
import { storyNameFromExport } from '@storybook/csf';
import { sanitizeName } from '../lib/utils';
import { sanitizeName, jscodeshiftToPrettierParser } from '../lib/utils';

/**
* Convert a legacy story API to component story format
Expand All @@ -25,6 +25,8 @@ import { sanitizeName } from '../lib/utils';
* NOTES: only support chained storiesOf() calls
*/
export default function transformer(file, api, options) {
const LITERAL = ['ts', 'tsx'].includes(options.parser) ? 'StringLiteral' : 'Literal';

const j = api.jscodeshift;
const root = j(file.source);

Expand Down Expand Up @@ -107,7 +109,7 @@ export default function transformer(file, api, options) {
base
.find(j.CallExpression)
.filter((call) => call.node.callee.name === 'storiesOf')
.filter((call) => call.node.arguments.length > 0 && call.node.arguments[0].type === 'Literal')
.filter((call) => call.node.arguments.length > 0 && call.node.arguments[0].type === LITERAL)
.forEach((storiesOf) => {
const title = storiesOf.node.arguments[0].value;
statements.push(
Expand All @@ -125,7 +127,7 @@ export default function transformer(file, api, options) {
base
.find(j.CallExpression)
.filter((add) => add.node.callee.property && add.node.callee.property.name === 'add')
.filter((add) => add.node.arguments.length >= 2 && add.node.arguments[0].type === 'Literal')
.filter((add) => add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL)
.forEach((add) => adds.push(add));

adds.reverse();
Expand Down Expand Up @@ -233,7 +235,7 @@ export default function transformer(file, api, options) {
root
.find(j.CallExpression)
.filter((add) => add.node.callee.property && add.node.callee.property.name === 'add')
.filter((add) => add.node.arguments.length >= 2 && add.node.arguments[0].type === 'Literal')
.filter((add) => add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL)
.filter((add) =>
['ExpressionStatement', 'VariableDeclarator'].includes(add.parentPath.node.type)
)
Expand All @@ -260,13 +262,16 @@ export default function transformer(file, api, options) {
return source;
}

return prettier.format(source, {
parser: options.parser || 'babel',
// FIXME: storybook defaults
const prettierConfig = prettier.resolveConfig.sync('.', { editorconfig: true }) || {
printWidth: 100,
tabWidth: 2,
bracketSpacing: true,
trailingComma: 'es5',
singleQuote: true,
};

return prettier.format(source, {
...prettierConfig,
parser: jscodeshiftToPrettierParser(options.parser) || 'babel',
});
}