generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move parsing of instance to module & experiment with
defineMeta
- Loading branch information
Showing
7 changed files
with
233 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,27 @@ | ||
import { compile, type Root } from 'svelte/compiler'; | ||
import { compile, type Root } from "svelte/compiler"; | ||
|
||
import type { StoriesFileMeta } from './types.js'; | ||
import { walkOnModule } from './walkers/module.js'; | ||
import { walkOnInstance } from './walkers/instance.js'; | ||
import { walkOnFragment } from './walkers/fragment.js'; | ||
import type { StoriesFileMeta } from "./types.js"; | ||
import { walkOnModule } from "./walkers/module.js"; | ||
import { walkOnFragment } from "./walkers/fragment.js"; | ||
|
||
/** | ||
* Parse raw stories file component in Svelte format, | ||
* and extract the most stories file meta, | ||
* which are required to generate `StoryFn's` for `@storybook/svelte` components. | ||
*/ | ||
export function extractStories(rawSource: string): StoriesFileMeta { | ||
const { ast }: { ast: Root } = compile(rawSource, { modernAst: true }); | ||
const { module, instance, fragment } = ast; | ||
const { ast }: { ast: Root } = compile(rawSource, { modernAst: true }); | ||
const { module, fragment } = ast; | ||
|
||
const moduleMeta = walkOnModule(module); | ||
const instanceMeta = walkOnInstance(instance); | ||
const fragmentMeta = walkOnFragment({ | ||
fragment, | ||
rawSource, | ||
addonComponentName: instanceMeta.addonComponentName, | ||
}); | ||
const moduleMeta = walkOnModule(module); | ||
const fragmentMeta = walkOnFragment({ | ||
fragment, | ||
rawSource, | ||
addonComponentName: moduleMeta.addonComponentName, | ||
}); | ||
|
||
return { | ||
module: moduleMeta, | ||
instance: instanceMeta, | ||
fragment: fragmentMeta, | ||
}; | ||
return { | ||
module: moduleMeta, | ||
fragment: fragmentMeta, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,59 @@ | ||
import type { Meta } from '@storybook/svelte'; | ||
import type { Meta } from "@storybook/svelte"; | ||
|
||
export const ADDON_COMPONENT_NAME = 'Story'; | ||
import type { defineMeta } from "../index.js"; | ||
|
||
export const ADDON_FN_NAME = "defineMeta"; | ||
export const ADDON_COMPONENT_NAME = "Story"; | ||
export const ADDON_META_VAR_NAME = "meta"; | ||
|
||
/** | ||
* Data extracted from the static analytic of a single stories file - `*.stories.svelte`. | ||
*/ | ||
export interface StoriesFileMeta { | ||
module: ModuleMeta; | ||
instance: InstanceMeta; | ||
fragment: FragmentMeta; | ||
} | ||
|
||
/** | ||
* Meta extracted from static analysis of the instance tag _(`<script>`)_ | ||
* from the single stories file - `*.stories.svelte`. | ||
*/ | ||
export interface InstanceMeta extends Pick<Meta, 'id' | 'title' | 'tags'> { | ||
// Can be overriden with `import { Story as S } ...` | ||
addonComponentName: typeof ADDON_COMPONENT_NAME | (string & {}); | ||
module: ModuleMeta; | ||
fragment: FragmentMeta; | ||
} | ||
|
||
/** | ||
* Meta extracted from static analysis of the module tag _(`<script context="module">`)_ | ||
* from the single stories file - `*.stories.svelte`. | ||
*/ | ||
export interface ModuleMeta extends Pick<Meta, 'id' | 'title' | 'tags'> { | ||
description?: string; | ||
export interface ModuleMeta extends Pick<Meta, "tags"> { | ||
description?: string; | ||
// NOTE: Why? It could be overriden with `import { defineMeta as d } ...` | ||
addonFnName: typeof defineMeta.name | (string & {}); | ||
// NOTE: Why? It could be overriden with `const { Story: S } ...` | ||
addonComponentName: typeof ADDON_COMPONENT_NAME | (string & {}); | ||
// NOTE: Why? It could be overriden with `const { meta: m } ...` | ||
addonMetaVarName: typeof ADDON_META_VAR_NAME | (string & {}); | ||
} | ||
|
||
/** | ||
* Meta extracted from static analysis of the `Fragment` _(html-like code)_ | ||
* from the single stories file - `*.stories.svelte`. | ||
*/ | ||
export interface FragmentMeta { | ||
stories: Record<StoryMeta['id'], StoryMeta>; | ||
stories: Record<StoryMeta["id"], StoryMeta>; | ||
} | ||
|
||
/** | ||
* Meta extracted from static analysis of the single <Story /> component | ||
* in the stories file - `*.stories.svelte`. | ||
*/ | ||
export interface StoryMeta { | ||
/** | ||
* Id of the story. By default is hashed, otherwise can be overriden. | ||
*/ | ||
id: string; | ||
/** | ||
* Name of the story. | ||
* @default "Default" | ||
*/ | ||
name: string; | ||
/** | ||
* Description of the story, will display above the sample in docs mode. | ||
*/ | ||
description?: string; | ||
/** Raw source for children _(what is inside the <Story>...</Story> tags)_ */ | ||
rawSource?: string; | ||
/** | ||
* Id of the story. By default is hashed, otherwise can be overriden. | ||
*/ | ||
id: string; | ||
/** | ||
* Name of the story. | ||
* @default "Default" | ||
*/ | ||
name: string; | ||
/** | ||
* Description of the story, will display above the sample in docs mode. | ||
*/ | ||
description?: string; | ||
/** Raw source for children _(what is inside the <Story>...</Story> tags)_ */ | ||
rawSource?: string; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.