UNPKG

@storybook/codemod

Version:

A collection of codemod scripts written with JSCodeshift

33 lines (29 loc) 1.59 kB
import CJS_COMPAT_NODE_URL_94b6aurt4b from 'node:url'; import CJS_COMPAT_NODE_PATH_94b6aurt4b from 'node:path'; import CJS_COMPAT_NODE_MODULE_94b6aurt4b from "node:module"; var __filename = CJS_COMPAT_NODE_URL_94b6aurt4b.fileURLToPath(import.meta.url); var __dirname = CJS_COMPAT_NODE_PATH_94b6aurt4b.dirname(__filename); var require = CJS_COMPAT_NODE_MODULE_94b6aurt4b.createRequire(import.meta.url); // ------------------------------------------------------------ // end of CJS compatibility banner, injected by Storybook's esbuild configuration // ------------------------------------------------------------ // src/transforms/upgrade-hierarchy-separators.js function upgradeSeparator(path) { return path.replace(/[|.]/g, "/"); } function transformer(file, api, options) { let j = api.jscodeshift, root = j(file.source); return root.find(j.CallExpression).filter((call) => call.node.callee.name === "storiesOf").filter( (call) => call.node.arguments.length > 0 && ["Literal", "StringLiteral"].includes(call.node.arguments[0].type) ).forEach((call) => { let arg0 = call.node.arguments[0]; arg0.value = upgradeSeparator(arg0.value); }), root.find(j.ExportDefaultDeclaration).filter((def) => def.node.declaration.properties.map((p) => p.key.name).includes("title")).forEach((def) => { def.node.declaration && def.node.declaration.properties && def.node.declaration.properties.forEach((p) => { p.key.name === "title" && (p.value.value = upgradeSeparator(p.value.value)); }); }), root.toSource({ quote: "single" }); } export { transformer as default };