From 51583cc0325a375d105432ff3a05f0e88acf0d2b Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:48:54 +0800 Subject: [PATCH 01/14] Stop returning `meta` from `defineMeta()` --- src/index.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 668fa64..84ebd9c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,10 +14,9 @@ import type { Cmp, } from './types'; -export function defineMeta(meta: MetaType) { +export function defineMeta(_meta: MetaType) { return { Story: StoryComponent as typeof StoryComponent, - meta, }; } From b7aebe57fae841103fc4bf803618dc6c4175fdfb Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:49:15 +0800 Subject: [PATCH 02/14] Add `createASTIdentifier()` fn --- src/parser/ast.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/parser/ast.ts b/src/parser/ast.ts index ce73cbb..c3eb3d2 100644 --- a/src/parser/ast.ts +++ b/src/parser/ast.ts @@ -111,4 +111,14 @@ export function createASTScript(options: ASTScriptOptions): SvelteAST.Script { }; } +/** + * Create ESTree compliant AST node for {@link ESTreeAST.Identifier}. + */ +export function createASTIdentifier(name: string): ESTreeAST.Identifier { + return { + type: 'Identifier', + name, + }; +} + export type { ESTreeAST, SvelteAST }; From 4ed3b0bda9816cb47d15335bb1c4ade5f5c59677 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:50:39 +0800 Subject: [PATCH 03/14] change `createExportDefaultMeta()` and move it to `create-appendix.ts` --- .../appendix/create-export-default.test.ts | 34 ------------------- .../appendix/create-export-default.ts | 16 --------- .../post-transform/create-appendix.ts | 10 ++++-- 3 files changed, 8 insertions(+), 52 deletions(-) delete mode 100644 src/compiler/post-transform/appendix/create-export-default.test.ts delete mode 100644 src/compiler/post-transform/appendix/create-export-default.ts diff --git a/src/compiler/post-transform/appendix/create-export-default.test.ts b/src/compiler/post-transform/appendix/create-export-default.test.ts deleted file mode 100644 index cd1531c..0000000 --- a/src/compiler/post-transform/appendix/create-export-default.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { print } from 'esrap'; -import { describe, it } from 'vitest'; - -import { createExportDefaultMeta } from './create-export-default'; - -import type { ESTreeAST } from '#parser/ast'; - -describe(createExportDefaultMeta.name, () => { - it('creates a new export default correctly', ({ expect }) => { - const stringified = print( - createExportDefaultMeta({ - metaIdentifier: { - type: 'Identifier', - name: 'meta', - }, - }) as unknown as ESTreeAST.Program - ).code; - - expect(stringified).toMatchInlineSnapshot(`"export default meta;"`); - }); - - it("works when 'meta' identifier was destructured manually and renamed by user", ({ expect }) => { - const stringified = print( - createExportDefaultMeta({ - metaIdentifier: { - type: 'Identifier', - name: '__renamed_meta', - }, - }) as unknown as ESTreeAST.Program - ).code; - - expect(stringified).toMatchInlineSnapshot(`"export default __renamed_meta;"`); - }); -}); diff --git a/src/compiler/post-transform/appendix/create-export-default.ts b/src/compiler/post-transform/appendix/create-export-default.ts deleted file mode 100644 index c01de2b..0000000 --- a/src/compiler/post-transform/appendix/create-export-default.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { getMetaIdentifier } from '#parser/analyse/define-meta/meta-identifier'; -import type { ESTreeAST } from '#parser/ast'; - -interface Params { - metaIdentifier: ReturnType; - filename?: string; -} - -export function createExportDefaultMeta(params: Params): ESTreeAST.ExportDefaultDeclaration { - const { metaIdentifier } = params; - - return { - type: 'ExportDefaultDeclaration', - declaration: metaIdentifier, - }; -} diff --git a/src/compiler/post-transform/create-appendix.ts b/src/compiler/post-transform/create-appendix.ts index ad5f0f4..60fbcbe 100644 --- a/src/compiler/post-transform/create-appendix.ts +++ b/src/compiler/post-transform/create-appendix.ts @@ -1,7 +1,6 @@ import { print } from 'esrap'; import MagicString from 'magic-string'; -import { createExportDefaultMeta } from './appendix/create-export-default'; import { createExportOrderVariable } from './appendix/create-export-order'; import { createRuntimeStoriesImport } from './appendix/create-import'; import { createVariableFromRuntimeStoriesCall } from './appendix/create-variable-from-runtime-stories-call'; @@ -53,7 +52,7 @@ export async function createAppendix(params: Params) { body: [ createRuntimeStoriesImport(), variableFromRuntimeStoriesCall, - createExportDefaultMeta({ metaIdentifier, filename }), + createExportDefaultMeta(), createExportOrderVariable({ storyIdentifiers, filename }), ...storiesExports, ], @@ -61,3 +60,10 @@ export async function createAppendix(params: Params) { code.append('\n' + appendix.code); } + +function createExportDefaultMeta(): ESTreeAST.ExportDefaultDeclaration { + return { + type: 'ExportDefaultDeclaration', + declaration: createASTIdentifier('meta'), + }; +} From 2423fd2278f528c41b8899c4b0119fc6b5c64647 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:51:46 +0800 Subject: [PATCH 04/14] remove `geteMetaIdentifier()` & apply changes --- ...eate-variable-from-runtime-stories-call.ts | 5 +-- .../post-transform/create-appendix.ts | 11 ++--- .../analyse/define-meta/meta-identifier.ts | 40 ------------------- 3 files changed, 5 insertions(+), 51 deletions(-) delete mode 100644 src/parser/analyse/define-meta/meta-identifier.ts diff --git a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts index 386518e..e576321 100644 --- a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts +++ b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts @@ -1,9 +1,8 @@ import type { getMetaIdentifier } from '#parser/analyse/define-meta/meta-identifier'; -import type { ESTreeAST } from '#parser/ast'; +import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; interface Params { storiesFunctionDeclaration: ESTreeAST.FunctionDeclaration; - metaIdentifier: ReturnType; filename?: string; } @@ -36,7 +35,7 @@ export function createVariableFromRuntimeStoriesCall( type: 'Identifier', name: storiesFunctionDeclaration.id.name, }, - metaIdentifier, + createASTIdentifier('meta'), ], }, }, diff --git a/src/compiler/post-transform/create-appendix.ts b/src/compiler/post-transform/create-appendix.ts index 60fbcbe..1c6e870 100644 --- a/src/compiler/post-transform/create-appendix.ts +++ b/src/compiler/post-transform/create-appendix.ts @@ -6,10 +6,10 @@ import { createRuntimeStoriesImport } from './appendix/create-import'; import { createVariableFromRuntimeStoriesCall } from './appendix/create-variable-from-runtime-stories-call'; import { createNamedExportStory } from './appendix/create-named-export-story'; -import { getMetaIdentifier } from '#parser/analyse/define-meta/meta-identifier'; +import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; +import { getStoriesIdentifiers } from '#parser/analyse/story/attributes/identifiers'; import type { CompiledASTNodes } from '#parser/extract/compiled/nodes'; import type { SvelteASTNodes } from '#parser/extract/svelte/nodes'; -import { getStoriesIdentifiers } from '#parser/analyse/story/attributes/identifiers'; interface Params { code: MagicString; @@ -23,19 +23,14 @@ interface Params { export async function createAppendix(params: Params) { const { code, nodes, filename } = params; const { compiled, svelte } = nodes; - const { defineMetaVariableDeclaration, storiesFunctionDeclaration } = compiled; + const { storiesFunctionDeclaration } = compiled; const storyIdentifiers = getStoriesIdentifiers({ nodes: svelte, filename, }); - const metaIdentifier = getMetaIdentifier({ - node: defineMetaVariableDeclaration, - filename, - }); const variableFromRuntimeStoriesCall = createVariableFromRuntimeStoriesCall({ storiesFunctionDeclaration, - metaIdentifier, filename, }); const storiesExports = storyIdentifiers.map(({ exportName }) => diff --git a/src/parser/analyse/define-meta/meta-identifier.ts b/src/parser/analyse/define-meta/meta-identifier.ts deleted file mode 100644 index 7a97c39..0000000 --- a/src/parser/analyse/define-meta/meta-identifier.ts +++ /dev/null @@ -1,40 +0,0 @@ -import type { ESTreeAST } from '#parser/ast'; -import type { SvelteASTNodes } from '#parser/extract/svelte/nodes'; -import { - NoDestructuredDefineMetaCallError, - NoMetaIdentifierFoundError, -} from '#utils/error/parser/analyse/define-meta'; - -interface Params { - node: SvelteASTNodes['defineMetaVariableDeclaration']; - filename?: string; -} - -export function getMetaIdentifier(params: Params): ESTreeAST.Identifier { - const { node, filename } = params; - const { declarations } = node; - const { id } = declarations[0]; - - if (id.type !== 'ObjectPattern') { - throw new NoDestructuredDefineMetaCallError({ - filename, - defineMetaVariableDeclarator: declarations[0], - }); - } - - const { properties } = id; - - const metaProperty = properties.find( - (p) => p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'meta' - ); - - if ( - !metaProperty || - metaProperty.type !== 'Property' || - metaProperty.value.type !== 'Identifier' - ) { - throw new NoMetaIdentifierFoundError(filename); - } - - return metaProperty.value; -} From 9adb177ff94dbf3aac7e15712992db3d27687a11 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:57:55 +0800 Subject: [PATCH 05/14] remove `NoMetaIdentifierFoundError` && update `ERRORS.md` (codes) --- ERRORS.md | 14 ++----------- src/utils/error/parser/analyse/define-meta.ts | 20 ++++--------------- 2 files changed, 6 insertions(+), 28 deletions(-) diff --git a/ERRORS.md b/ERRORS.md index cf88390..c1bf507 100644 --- a/ERRORS.md +++ b/ERRORS.md @@ -218,16 +218,6 @@ You most likely forgot to destructure the return value. ### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0003` -The addon automatically adds a `meta` identifier to the return value of `defineMeta()` during compilation. -But for some reason, this couldn't be found in the compiled output. - -If you see this error, please open a [bug report](https://github.com/storybookjs/addon-svelte-csf/issues/new). - -While you create an issue, please provide original code of the stories file that caused this error. -It will help us investigate the occurred issue better. - -### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0004` - When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The following properties must be **static string literals**, but got something else: - `title` @@ -235,13 +225,13 @@ When analysing the object passed to `defineMeta({ ... })`, invalid properties we Dynamically generating these properties with functions or with template strings is not supported. -### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0005` +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0004` When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The `tags` property must be a **static array of static string literals**, but got something else Dynamically generating the array or the entries with functions or with template strings is not supported. -### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0006` +### `SB_SVELTE_CSF_PARSER_ANALYSE_DEFINE_META_0005` When analysing the object passed to `defineMeta({ ... })`, invalid properties were found. The `tags` property must be a **static array of static string literals**, but got something else diff --git a/src/utils/error/parser/analyse/define-meta.ts b/src/utils/error/parser/analyse/define-meta.ts index b8306e4..2df0c8a 100644 --- a/src/utils/error/parser/analyse/define-meta.ts +++ b/src/utils/error/parser/analyse/define-meta.ts @@ -58,26 +58,14 @@ export class NoDestructuredDefineMetaCallError extends StorybookSvelteCSFError { } } -export class NoMetaIdentifierFoundError extends StorybookSvelteCSFError { - readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; - readonly code = 3; - public documentation = true; - - constructor(filename: StorybookSvelteCSFError['filename']) { - super({ filename }); - } - - template(): string { - return dedent` - Could not find 'meta' identifier in the compiled output of stories file: ${this.filepathURL} - This is most likely a bug in @storybook/addon-svelte-csf. Please open an issue on GitHub. + The current pattern type is: "${this.node.id.type}", and expected is "ObjectPattern". `; } } export class NoStringLiteralError extends StorybookSvelteCSFError { readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; - readonly code = 4; + readonly code = 3; public documentation = true; readonly property: ESTreeAST.Property; @@ -105,7 +93,7 @@ export class NoStringLiteralError extends StorybookSvelteCSFError { export class NoArrayExpressionError extends StorybookSvelteCSFError { readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; - readonly code = 5; + readonly code = 4; public documentation = true; readonly property: ESTreeAST.Property; @@ -133,7 +121,7 @@ export class NoArrayExpressionError extends StorybookSvelteCSFError { export class ArrayElementNotStringError extends StorybookSvelteCSFError { readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; - readonly code = 6; + readonly code = 5; public documentation = true; readonly property: ESTreeAST.Property; From 4e83967b2a437407e6914ea996c700c06bb4b06e Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 22:58:42 +0800 Subject: [PATCH 06/14] replace `destructure-meta` with `replace-argument` --- .../define-meta/destructure-meta.ts | 61 ------------------- .../post-transform/define-meta/index.ts | 32 ++++++++-- .../define-meta/replace-argument.ts | 43 +++++++++++++ src/utils/error/parser/analyse/define-meta.ts | 12 ++-- 4 files changed, 75 insertions(+), 73 deletions(-) delete mode 100644 src/compiler/post-transform/define-meta/destructure-meta.ts create mode 100644 src/compiler/post-transform/define-meta/replace-argument.ts diff --git a/src/compiler/post-transform/define-meta/destructure-meta.ts b/src/compiler/post-transform/define-meta/destructure-meta.ts deleted file mode 100644 index 7fdd0e5..0000000 --- a/src/compiler/post-transform/define-meta/destructure-meta.ts +++ /dev/null @@ -1,61 +0,0 @@ -import type { CompiledASTNodes } from '#parser/extract/compiled/nodes'; -import { NoDestructuredDefineMetaCallError } from '#utils/error/parser/analyse/define-meta'; - -interface Params { - nodes: CompiledASTNodes; - filename?: string; -} - -/** - * Attempt to destructure 'meta' identifier in the object pattern of the variable declaration from call `defineMeta({...})` - * if it wasn't done by user manually. - * - * Before: - * - * ```js - * const { Story } = defineMeta({}); - * ``` - * - * After: - * - * ```js - * const { Story, meta } = defineMeta({}); - * ``` - */ -export function destructureMetaFromDefineMeta(params: Params): void { - const { nodes, filename } = params; - const { defineMetaVariableDeclaration } = nodes; - const { declarations } = defineMetaVariableDeclaration; - const { id } = declarations[0]; - - if (id.type !== 'ObjectPattern') { - throw new NoDestructuredDefineMetaCallError({ - filename, - defineMetaVariableDeclarator: declarations[0], - }); - } - - const destructuredMeta = id.properties.find((p) => { - return p.type === 'Property' && p.key.type === 'Identifier' && p.key.name === 'meta'; - }); - - if (destructuredMeta) { - return; - } - - id.properties.push({ - type: 'Property', - kind: 'init', - key: { - type: 'Identifier', - name: 'meta', - }, - value: { - type: 'Identifier', - name: 'meta', - }, - shorthand: true, - computed: false, - method: false, - }); -} diff --git a/src/compiler/post-transform/define-meta/index.ts b/src/compiler/post-transform/define-meta/index.ts index bc20527..0d28608 100644 --- a/src/compiler/post-transform/define-meta/index.ts +++ b/src/compiler/post-transform/define-meta/index.ts @@ -1,11 +1,12 @@ import { print } from 'esrap'; import type MagicString from 'magic-string'; -import { destructureMetaFromDefineMeta } from './destructure-meta'; +import { replaceDefineMetaArgument } from './replace-argument'; import { insertDefineMetaJSDocCommentAsDescription } from './insert-description'; import type { CompiledASTNodes } from '#parser/extract/compiled/nodes'; import type { SvelteASTNodes } from '#parser/extract/svelte/nodes'; +import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; interface Params { code: MagicString; @@ -23,18 +24,41 @@ interface Params { export function transformDefineMeta(params: Params): void { const { code, nodes, filename } = params; - destructureMetaFromDefineMeta({ - nodes: nodes.compiled, + insertDefineMetaJSDocCommentAsDescription({ + nodes, filename, }); - insertDefineMetaJSDocCommentAsDescription({ + const metaObjectExpression = replaceDefineMetaArgument({ nodes, filename, }); + const metaVariableDeclaration = createMetaVariableDeclaration({ + init: metaObjectExpression, + }); const { compiled } = nodes; const { defineMetaVariableDeclaration } = compiled; const { start, end } = defineMetaVariableDeclaration; code.update(start as number, end as number, print(defineMetaVariableDeclaration).code); + code.appendLeft(start as number, print(metaVariableDeclaration).code + '\n'); +} + +export function createMetaVariableDeclaration({ + init, +}: { + init: ESTreeAST.ObjectExpression; +}): ESTreeAST.VariableDeclaration { + // + return { + type: 'VariableDeclaration', + kind: 'const', + declarations: [ + { + type: 'VariableDeclarator', + id: createASTIdentifier('meta'), + init, + }, + ], + }; } diff --git a/src/compiler/post-transform/define-meta/replace-argument.ts b/src/compiler/post-transform/define-meta/replace-argument.ts new file mode 100644 index 0000000..50b8105 --- /dev/null +++ b/src/compiler/post-transform/define-meta/replace-argument.ts @@ -0,0 +1,43 @@ +import { createASTIdentifier, type ESTreeAST } from '../../../parser/ast'; +import type { CompiledASTNodes } from '../../../parser/extract/compiled/nodes'; +import type { SvelteASTNodes } from '../../../parser/extract/svelte/nodes'; +import { getDefineMetaFirstArgumentObjectExpression } from '../../../parser/extract/svelte/define-meta'; +import { NoDestructuredDefineMetaCallError } from '../../../utils/error/parser/analyse/define-meta'; + +interface Params { + nodes: { + compiled: CompiledASTNodes; + svelte: SvelteASTNodes; + }; + filename?: string; +} + +/** + * Replaces `defineMeta({ ... })` with `defineMeta(meta)`, + * and also it returns {@link ESTreeASTAST.ObjectExpression} which was replaced with {@link ESTreeAST.Identifier} + */ +export function replaceDefineMetaArgument(params: Params): ESTreeAST.ObjectExpression { + const defineMetaFirstArgumentObjectExpression = getDefineMetaFirstArgumentObjectExpression({ + nodes: params.nodes.compiled, + filename: params.filename, + }); + + const declaration = params.nodes.compiled.defineMetaVariableDeclaration.declarations[0]; + + if ( + !declaration || + declaration.init?.type !== 'CallExpression' || + declaration?.init?.callee.type !== 'Identifier' || + declaration?.init?.callee.name !== 'defineMeta' + ) { + throw new NoDestructuredDefineMetaCallError({ + defineMetaVariableDeclarator: declaration, + filename: params.filename, + }); + } + + declaration.init.arguments[0] = createASTIdentifier('meta'); + params.nodes.compiled.defineMetaVariableDeclaration.declarations[0] = declaration; + + return defineMetaFirstArgumentObjectExpression; +} diff --git a/src/utils/error/parser/analyse/define-meta.ts b/src/utils/error/parser/analyse/define-meta.ts index 2df0c8a..85e85e6 100644 --- a/src/utils/error/parser/analyse/define-meta.ts +++ b/src/utils/error/parser/analyse/define-meta.ts @@ -30,22 +30,23 @@ export class InvalidComponentValueError extends StorybookSvelteCSFError { } } +// TODO: Rename this error export class NoDestructuredDefineMetaCallError extends StorybookSvelteCSFError { readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; readonly code = 2; public documentation = true; - public defineMetaVariableDeclarator: ESTreeAST.VariableDeclarator; + public node: ESTreeAST.VariableDeclarator; constructor({ filename, defineMetaVariableDeclarator, }: { filename: StorybookSvelteCSFError['filename']; - defineMetaVariableDeclarator: NoDestructuredDefineMetaCallError['defineMetaVariableDeclarator']; + defineMetaVariableDeclarator: NoDestructuredDefineMetaCallError['node']; }) { super({ filename }); - this.defineMetaVariableDeclarator = defineMetaVariableDeclarator; + this.node = defineMetaVariableDeclarator; } template(): string { @@ -53,11 +54,6 @@ export class NoDestructuredDefineMetaCallError extends StorybookSvelteCSFError { The return value of the 'defineMeta' call was not destructured to { Story }. The issue occurred in Stories file: ${this.filepathURL} - The current pattern type is: "${this.defineMetaVariableDeclarator.id.type}", and expected is "ObjectPattern". - `; - } -} - The current pattern type is: "${this.node.id.type}", and expected is "ObjectPattern". `; } From 22a3d011c25f354d3220f340586e6a789aab10c1 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:00:37 +0800 Subject: [PATCH 07/14] update snapshots --- .../post-transform/define-meta/index.test.ts | 20 +------------------ src/compiler/post-transform/index.test.ts | 5 +++-- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/compiler/post-transform/define-meta/index.test.ts b/src/compiler/post-transform/define-meta/index.test.ts index 57b15a4..af0c266 100644 --- a/src/compiler/post-transform/define-meta/index.test.ts +++ b/src/compiler/post-transform/define-meta/index.test.ts @@ -51,24 +51,6 @@ describe(transformDefineMeta.name, () => { ast: parseAst(code.toString()), }); - expect(print(defineMetaVariableDeclaration).code).toMatchInlineSnapshot(` - "const { Story, meta } = defineMeta({ - title: 'Example', - component: Example, - tags: ['autodocs'], - args: { - onclick: action('onclick'), - onmouseenter: action('onmouseenter'), - onmouseleave: action('onmouseleave') - }, - parameters: { - docs: { - description: { - component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`." - } - } - } - });" - `); + expect(print(defineMetaVariableDeclaration).code).toMatchInlineSnapshot(`"const { Story } = defineMeta(meta);"`); }); }); diff --git a/src/compiler/post-transform/index.test.ts b/src/compiler/post-transform/index.test.ts index 1d00c26..c9b9178 100644 --- a/src/compiler/post-transform/index.test.ts +++ b/src/compiler/post-transform/index.test.ts @@ -69,7 +69,7 @@ describe(transformStoriesCode.name, () => { * * _Italic_, * * \`Code\`. */ - const { Story, meta } = defineMeta({ + const meta = { title: 'Example', component: Example, tags: ['autodocs'], @@ -85,7 +85,8 @@ describe(transformStoriesCode.name, () => { } } } - }); + }; + const { Story } = defineMeta(meta); function Example_stories($$anchor, $$props) { $.check_target(new.target); From 1e8c312b827dad64d7d3c31cab378b165155cc66 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:04:54 +0800 Subject: [PATCH 08/14] update import paths in changed files --- src/compiler/post-transform/create-appendix.ts | 8 ++++---- src/compiler/post-transform/define-meta/index.test.ts | 8 ++++---- src/compiler/post-transform/define-meta/index.ts | 6 +++--- src/compiler/post-transform/index.test.ts | 6 +++--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/compiler/post-transform/create-appendix.ts b/src/compiler/post-transform/create-appendix.ts index 1c6e870..03488f1 100644 --- a/src/compiler/post-transform/create-appendix.ts +++ b/src/compiler/post-transform/create-appendix.ts @@ -6,10 +6,10 @@ import { createRuntimeStoriesImport } from './appendix/create-import'; import { createVariableFromRuntimeStoriesCall } from './appendix/create-variable-from-runtime-stories-call'; import { createNamedExportStory } from './appendix/create-named-export-story'; -import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; -import { getStoriesIdentifiers } from '#parser/analyse/story/attributes/identifiers'; -import type { CompiledASTNodes } from '#parser/extract/compiled/nodes'; -import type { SvelteASTNodes } from '#parser/extract/svelte/nodes'; +import { createASTIdentifier, type ESTreeAST } from '../../parser/ast'; +import { getStoriesIdentifiers } from '../../parser/analyse/story/attributes/identifiers'; +import type { CompiledASTNodes } from '../../parser/extract/compiled/nodes'; +import type { SvelteASTNodes } from '../../parser/extract/svelte/nodes'; interface Params { code: MagicString; diff --git a/src/compiler/post-transform/define-meta/index.test.ts b/src/compiler/post-transform/define-meta/index.test.ts index af0c266..c863b3a 100644 --- a/src/compiler/post-transform/define-meta/index.test.ts +++ b/src/compiler/post-transform/define-meta/index.test.ts @@ -7,11 +7,11 @@ import MagicString from 'magic-string'; import { parseAst } from 'rollup/parseAst'; import { describe, it } from 'vitest'; -import { transformDefineMeta } from '.'; +import { transformDefineMeta } from './index'; -import { getSvelteAST } from '#parser/ast'; -import { extractSvelteASTNodes } from '#parser/extract/svelte/nodes'; -import { extractCompiledASTNodes } from '#parser/extract/compiled/nodes'; +import { getSvelteAST } from '../../../parser/ast'; +import { extractSvelteASTNodes } from '../../../parser/extract/svelte/nodes'; +import { extractCompiledASTNodes } from '../../../parser/extract/compiled/nodes'; const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); diff --git a/src/compiler/post-transform/define-meta/index.ts b/src/compiler/post-transform/define-meta/index.ts index 0d28608..59a4cb8 100644 --- a/src/compiler/post-transform/define-meta/index.ts +++ b/src/compiler/post-transform/define-meta/index.ts @@ -4,9 +4,9 @@ import type MagicString from 'magic-string'; import { replaceDefineMetaArgument } from './replace-argument'; import { insertDefineMetaJSDocCommentAsDescription } from './insert-description'; -import type { CompiledASTNodes } from '#parser/extract/compiled/nodes'; -import type { SvelteASTNodes } from '#parser/extract/svelte/nodes'; -import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; +import { createASTIdentifier, type ESTreeAST } from '../../../parser/ast'; +import type { CompiledASTNodes } from '../../../parser/extract/compiled/nodes'; +import type { SvelteASTNodes } from '../../../parser/extract/svelte/nodes'; interface Params { code: MagicString; diff --git a/src/compiler/post-transform/index.test.ts b/src/compiler/post-transform/index.test.ts index c9b9178..0fd6c48 100644 --- a/src/compiler/post-transform/index.test.ts +++ b/src/compiler/post-transform/index.test.ts @@ -8,9 +8,9 @@ import { describe, it } from 'vitest'; import { transformStoriesCode } from '.'; -import { getSvelteAST } from '#parser/ast'; -import { extractSvelteASTNodes } from '#parser/extract/svelte/nodes'; -import { extractCompiledASTNodes } from '#parser/extract/compiled/nodes'; +import { getSvelteAST } from '../../parser/ast'; +import { extractSvelteASTNodes } from '../../parser/extract/svelte/nodes'; +import { extractCompiledASTNodes } from '../../parser/extract/compiled/nodes'; const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); From 76334cc8330d49b7fa34b46835aacf544fe7a8c2 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:08:53 +0800 Subject: [PATCH 09/14] remove todo comment --- src/utils/error/parser/analyse/define-meta.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/error/parser/analyse/define-meta.ts b/src/utils/error/parser/analyse/define-meta.ts index 85e85e6..b15495f 100644 --- a/src/utils/error/parser/analyse/define-meta.ts +++ b/src/utils/error/parser/analyse/define-meta.ts @@ -30,7 +30,6 @@ export class InvalidComponentValueError extends StorybookSvelteCSFError { } } -// TODO: Rename this error export class NoDestructuredDefineMetaCallError extends StorybookSvelteCSFError { readonly category = StorybookSvelteCSFError.CATEGORY.parserAnalyseDefineMeta; readonly code = 2; From d62c50f2e10a6af85ce1d51de09aa4419cc22e6a Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:10:28 +0800 Subject: [PATCH 10/14] remove `meta` destructuring from tests --- src/index.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index d4be01e..92bdbda 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -17,7 +17,7 @@ import Button from '../examples/components/Button.svelte'; describe(defineMeta.name, () => { it('works with provided meta entry "component" entry', () => { - const { Story, meta } = defineMeta({ + const { Story } = defineMeta({ component: Button, args: { children: createRawSnippet(() => ({ @@ -37,7 +37,6 @@ describe(defineMeta.name, () => { }); expectTypeOf(Story).toMatchTypeOf>(); - expectTypeOf(meta).toMatchTypeOf>(); }); }); @@ -71,7 +70,7 @@ describe("type helper for snippets 'Args'", () => { describe("type helper for snippets 'StoryContext'", () => { it("infers the type of entry 'args' from 'defineMeta' correctly", () => { - const { Story, meta } = defineMeta({ + const { Story } = defineMeta({ component: Button, args: { children: createRawSnippet(() => ({ @@ -91,7 +90,6 @@ describe("type helper for snippets 'StoryContext'", () => { }); expectTypeOf>().toMatchTypeOf>(); - expectTypeOf(meta).toMatchTypeOf>(); }); }); From 134c20f81a9b64a797e83d8f11ceccd9f67d3a2e Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:11:21 +0800 Subject: [PATCH 11/14] remove obsolote `metaIdentifier` arg --- .../create-variable-from-runtime-stories-call.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.test.ts b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.test.ts index fc70bea..797d67a 100644 --- a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.test.ts +++ b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.test.ts @@ -7,10 +7,6 @@ describe(createVariableFromRuntimeStoriesCall.name, () => { it('creates a variable correctly', ({ expect }) => { const stringified = print( createVariableFromRuntimeStoriesCall({ - metaIdentifier: { - type: 'Identifier', - name: 'meta', - }, storiesFunctionDeclaration: { type: 'FunctionDeclaration', id: { From 25e5ca4e56365910e1cd447a457fbb4b343ab156 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:19:38 +0800 Subject: [PATCH 12/14] remove obsolote code causing `svelte-check` to fail --- .../post-transform/appendix/create-named-export-story.test.ts | 4 ---- .../appendix/create-variable-from-runtime-stories-call.ts | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/compiler/post-transform/appendix/create-named-export-story.test.ts b/src/compiler/post-transform/appendix/create-named-export-story.test.ts index e50cc4b..ee066ee 100644 --- a/src/compiler/post-transform/appendix/create-named-export-story.test.ts +++ b/src/compiler/post-transform/appendix/create-named-export-story.test.ts @@ -12,10 +12,6 @@ describe(createNamedExportStory.name, () => { createNamedExportStory({ exportName: 'Default', node: createVariableFromRuntimeStoriesCall({ - metaIdentifier: { - type: 'Identifier', - name: 'meta', - }, storiesFunctionDeclaration: { type: 'FunctionDeclaration', id: { diff --git a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts index e576321..6b31ec8 100644 --- a/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts +++ b/src/compiler/post-transform/appendix/create-variable-from-runtime-stories-call.ts @@ -1,4 +1,3 @@ -import type { getMetaIdentifier } from '#parser/analyse/define-meta/meta-identifier'; import { createASTIdentifier, type ESTreeAST } from '#parser/ast'; interface Params { @@ -9,7 +8,7 @@ interface Params { export function createVariableFromRuntimeStoriesCall( params: Params ): ESTreeAST.VariableDeclaration { - const { storiesFunctionDeclaration, metaIdentifier } = params; + const { storiesFunctionDeclaration } = params; return { type: 'VariableDeclaration', From b7d5f5b8d29c60bdaae2774bcae0d6d95d7b3fa6 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:22:25 +0800 Subject: [PATCH 13/14] fix: remove overriding inexistent destructured `meta` --- tests/stories/test/Overrides.stories.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stories/test/Overrides.stories.svelte b/tests/stories/test/Overrides.stories.svelte index 3f30a04..f079c51 100644 --- a/tests/stories/test/Overrides.stories.svelte +++ b/tests/stories/test/Overrides.stories.svelte @@ -5,7 +5,7 @@ /** * Testing if **overriding identifier names** of the addon important AST nodes does work. */ - const { Story: S, meta: m } = d({ + const { Story: S } = d({ title: 'Overrides', parameters: { actions: { disable: true }, From f34bbc28a10cc3728467eb2e9839087310548712 Mon Sep 17 00:00:00 2001 From: Mateusz Kadlubowski Date: Fri, 13 Dec 2024 23:27:41 +0800 Subject: [PATCH 14/14] fix overriden `defineMeta` import local name --- src/compiler/post-transform/define-meta/replace-argument.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/post-transform/define-meta/replace-argument.ts b/src/compiler/post-transform/define-meta/replace-argument.ts index 50b8105..efa265e 100644 --- a/src/compiler/post-transform/define-meta/replace-argument.ts +++ b/src/compiler/post-transform/define-meta/replace-argument.ts @@ -28,7 +28,7 @@ export function replaceDefineMetaArgument(params: Params): ESTreeAST.ObjectExpre !declaration || declaration.init?.type !== 'CallExpression' || declaration?.init?.callee.type !== 'Identifier' || - declaration?.init?.callee.name !== 'defineMeta' + declaration?.init?.callee.name !== params.nodes.compiled.defineMetaImport.local.name ) { throw new NoDestructuredDefineMetaCallError({ defineMetaVariableDeclarator: declaration,