-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12347 from BlackFenix2/next
Addon-docs: Add Controls argument autodetection for svelte
- Loading branch information
Showing
15 changed files
with
651 additions
and
84 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
}); | ||
</script> | ||
|
||
<svelte:options accessors={true} /> | ||
<div id={hash} /> |
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,8 +1,12 @@ | ||
import { extractArgTypes } from './extractArgTypes'; | ||
import { extractComponentDescription } from '../../lib/docgen'; | ||
import { prepareForInline } from './prepareForInline'; | ||
|
||
export const parameters = { | ||
docs: { | ||
inlineStories: true, | ||
prepareForInline, | ||
extractArgTypes, | ||
extractComponentDescription, | ||
}, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import svelteDoc from 'sveltedoc-parser'; | ||
import * as fs from 'fs'; | ||
import { createArgTypes } from './extractArgTypes'; | ||
|
||
const content = fs.readFileSync(`${__dirname}/sample/MockButton.svelte`, 'utf-8'); | ||
|
||
describe('Extracting Arguments', () => { | ||
it('should be svelte', () => { | ||
expect(content).toMatchInlineSnapshot(` | ||
<script> | ||
import { createEventDispatcher, afterUpdate } from 'svelte'; | ||
export let text = ''; | ||
export let rounded = true; | ||
const dispatch = createEventDispatcher(); | ||
function onClick(event) { | ||
rounded = !rounded; | ||
dispatch('click', event); | ||
} | ||
afterUpdate(() => { | ||
dispatch('afterUpdate'); | ||
}); | ||
</script> | ||
<style> | ||
.rounded { | ||
border-radius: 35px; | ||
} | ||
.button { | ||
border: 3px solid; | ||
padding: 10px 20px; | ||
background-color: white; | ||
outline: none; | ||
} | ||
</style> | ||
<svelte:options accessors="{true}"> | ||
</svelte:options> | ||
<button class="button" | ||
class:rounded | ||
on:click="{onClick}" | ||
> | ||
<strong> | ||
{rounded ? 'Round' : 'Square'} corners | ||
</strong> | ||
<br> | ||
{text} | ||
<slot> | ||
</slot> | ||
</button> | ||
`); | ||
}); | ||
|
||
it('should generate ArgTypes', async () => { | ||
const doc = await svelteDoc.parse({ fileContent: content, version: 3 }); | ||
|
||
const results = createArgTypes(doc); | ||
|
||
expect(results).toMatchInlineSnapshot(` | ||
Object { | ||
"rounded": Object { | ||
"control": Object { | ||
"type": "boolean", | ||
}, | ||
"defaultValue": true, | ||
"description": null, | ||
"name": "rounded", | ||
"table": Object { | ||
"defaultValue": Object { | ||
"summary": true, | ||
}, | ||
}, | ||
"type": Object {}, | ||
}, | ||
"text": Object { | ||
"control": Object { | ||
"type": "text", | ||
}, | ||
"defaultValue": "", | ||
"description": null, | ||
"name": "text", | ||
"table": Object { | ||
"defaultValue": Object { | ||
"summary": "", | ||
}, | ||
}, | ||
"type": Object {}, | ||
}, | ||
} | ||
`); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { ArgTypes } from '@storybook/api'; | ||
|
||
import { ArgTypesExtractor } from '../../lib/docgen'; | ||
|
||
type ComponentWithDocgen = { | ||
__docgen: Docgen; | ||
}; | ||
|
||
type Docgen = { | ||
components: []; | ||
computed: []; | ||
data: [ | ||
{ | ||
defaultValue: any; | ||
description: string; | ||
keywords: []; | ||
kind: string; | ||
name: string; | ||
readonly: boolean; | ||
static: boolean; | ||
type: { kind: string; text: string; type: string }; | ||
visibility: string; | ||
} | ||
]; | ||
description: null; | ||
events: []; | ||
keywords: []; | ||
methods: []; | ||
name: string; | ||
refs: []; | ||
slots: []; | ||
version: number; | ||
}; | ||
|
||
export const extractArgTypes: ArgTypesExtractor = (component) => { | ||
// eslint-disable-next-line new-cap | ||
const comp: ComponentWithDocgen = new component({ props: {} }); | ||
// eslint-disable-next-line no-underscore-dangle | ||
const docs = comp.__docgen; | ||
|
||
const results = createArgTypes(docs); | ||
|
||
return results; | ||
}; | ||
|
||
export const createArgTypes = (docs: Docgen) => { | ||
const results: ArgTypes = {}; | ||
docs.data.forEach((item) => { | ||
results[item.name] = { | ||
control: { type: parseType(item.type.type) }, | ||
name: item.name, | ||
description: item.description, | ||
type: {}, | ||
defaultValue: item.defaultValue, | ||
table: { | ||
defaultValue: { | ||
summary: item.defaultValue, | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
return results; | ||
}; | ||
|
||
/** | ||
* Function to convert the type from sveltedoc-parser to a storybook type | ||
* @param typeName | ||
* @returns string | ||
*/ | ||
const parseType = (typeName: string) => { | ||
switch (typeName) { | ||
case 'string': | ||
return 'text'; | ||
|
||
case 'enum': | ||
return 'radio'; | ||
case 'any': | ||
return 'object'; | ||
default: | ||
return typeName; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import path from 'path'; | ||
|
||
import { Configuration } from 'webpack'; | ||
|
||
export function webpackFinal(webpackConfig: Configuration, options: any = {}) { | ||
webpackConfig.module.rules.push({ | ||
test: /\.svelte$/, | ||
loader: path.resolve(`${__dirname}/svelte-docgen-loader`), | ||
enforce: 'pre', | ||
}); | ||
|
||
return webpackConfig; | ||
} |
38 changes: 38 additions & 0 deletions
38
addons/docs/src/frameworks/svelte/sample/MockButton.svelte
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script> | ||
import { createEventDispatcher, afterUpdate } from 'svelte'; | ||
export let text = ''; | ||
export let rounded = true; | ||
const dispatch = createEventDispatcher(); | ||
function onClick(event) { | ||
rounded = !rounded; | ||
dispatch('click', event); | ||
} | ||
afterUpdate(() => { | ||
dispatch('afterUpdate'); | ||
}); | ||
</script> | ||
|
||
<style> | ||
.rounded { | ||
border-radius: 35px; | ||
} | ||
.button { | ||
border: 3px solid; | ||
padding: 10px 20px; | ||
background-color: white; | ||
outline: none; | ||
} | ||
</style> | ||
|
||
<svelte:options accessors={true} /> | ||
<button class="button" class:rounded on:click={onClick}> | ||
<strong>{rounded ? 'Round' : 'Square'} corners</strong> | ||
<br /> | ||
{text} | ||
<slot /> | ||
</button> |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import svelteDoc from 'sveltedoc-parser'; | ||
|
||
import * as path from 'path'; | ||
|
||
/** | ||
* webpack loader for sveltedoc-parser | ||
* @param source raw svelte component | ||
*/ | ||
export default async function svelteDocgen(source: string) { | ||
// get filename for source content | ||
// eslint-disable-next-line no-underscore-dangle | ||
const file = path.basename(this._module.resource); | ||
|
||
// set SvelteDoc options | ||
const options = { | ||
fileContent: source, | ||
version: 3, | ||
}; | ||
|
||
let docgen = ''; | ||
|
||
try { | ||
const componentDoc = await svelteDoc.parse(options); | ||
|
||
// populate filename in docgen | ||
componentDoc.name = path.basename(file); | ||
|
||
docgen = ` | ||
export const __docgen = ${JSON.stringify(componentDoc)}; | ||
`; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
// inject __docgen prop in svelte component | ||
const output = source.replace('</script>', `${docgen}</script>`); | ||
|
||
return output; | ||
} |
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
Oops, something went wrong.