@storybook/addon-svelte-csf
Version:
Allows to write stories in Svelte syntax
68 lines (67 loc) • 2.05 kB
JavaScript
/**
* Codemod to transform AST node of `export const meta` export named declaration to `defineMeta` variable declaration.
*
* @example
* ```diff
* - export const meta = {
* + const { Story } = defineMeta({
* title: "Atom/Button",
* component: Button,
* args: {
* // ...
* },
* // .. and more
* - } satisfies Meta<Button>;
* + });
* ```
*/
export function transformExportMetaToDefineMeta(exportMeta) {
const { declaration, leadingComments, start, end } = exportMeta;
if (!declaration || declaration.type !== 'VariableDeclaration') {
throw new Error("Invalid syntax - 'export meta' declaration was empty or not a variable declaration");
}
const { declarations } = declaration;
const { init } = declarations[0];
if (!init || init.type !== 'ObjectExpression') {
throw new Error("Invalid syntax - 'export meta' init was empty or not an object expression");
}
const key = {
type: 'Identifier',
name: 'Story',
};
return {
type: 'VariableDeclaration',
kind: 'const',
declarations: [
{
type: 'VariableDeclarator',
init: {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'defineMeta',
},
arguments: [init],
optional: false,
},
id: {
type: 'ObjectPattern',
properties: [
{
type: 'Property',
kind: 'init',
key,
value: key,
method: false,
shorthand: true,
computed: false,
},
],
},
},
],
leadingComments,
start,
end,
};
}