Skip to content

Commit

Permalink
Add example components and resulting output files.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Aug 19, 2020
1 parent f8a292c commit 6726d21
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/test
/examples
/scripts
.eslintrc.json
.gitignore
yarn.lock
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ npm install --save sveltedoc-parser

Output format are described at [this document](/typings.d.ts).

Please follow to [Examples](/examples/) folder to check how Svelte components transforms to JSON document.

See example of output for Svelte 2 component [here](/test/svelte2/integration/overall/overall.main.doc.json) presented in JSON format for [this component](/test/svelte2/integration/overall/main.svelte).

## Usage
Expand Down
73 changes: 73 additions & 0 deletions examples/Alert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"version": 3,
"name": "alert",
"data": [
{
"visibility": "private",
"description": null,
"keywords": [],
"name": "createEventDispatcher",
"kind": "const",
"static": false,
"readonly": true,
"type": {
"kind": "type",
"text": "any",
"type": "any"
},
"importPath": "svelte",
"originalName": "createEventDispatcher"
},
{
"visibility": "private",
"description": null,
"keywords": [],
"name": "dispatch",
"kind": "const",
"static": false,
"readonly": true,
"type": {
"kind": "type",
"text": "any",
"type": "any"
}
},
{
"visibility": "public",
"description": null,
"keywords": [],
"name": "closable",
"kind": "let",
"static": false,
"readonly": false,
"type": {
"kind": "type",
"text": "boolean",
"type": "boolean"
},
"defaultValue": false
}
],
"computed": [],
"methods": [],
"components": [],
"description": null,
"keywords": [],
"events": [
{
"visibility": "public",
"description": null,
"keywords": [],
"name": "close"
}
],
"slots": [
{
"name": "default",
"description": "The internal content of the alert block.",
"visibility": "public",
"parameters": []
}
],
"refs": []
}
18 changes: 18 additions & 0 deletions examples/Alert.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
export let closable = false;
</script>

<div>
<div>
<!-- The internal content of the alert block. -->
<slot></slot>
</div>
{#if closable}
<div>
<button on:click="{e => dispatch('close')}"></button>
</div>
{/if}
</div>
138 changes: 138 additions & 0 deletions examples/Button.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
{
"version": 3,
"name": "button",
"data": [
{
"keywords": [
{
"name": "type",
"description": "{string}"
}
],
"visibility": "public",
"description": "The text content of the button.",
"name": "text",
"kind": "let",
"static": false,
"readonly": false,
"type": {
"kind": "type",
"text": "string",
"type": "string"
},
"defaultValue": ""
},
{
"keywords": [
{
"name": "type",
"description": "{'default'|'primary'|'danger'|'success'}"
}
],
"visibility": "public",
"description": "The style type of the button.",
"name": "type",
"kind": "let",
"static": false,
"readonly": false,
"type": {
"kind": "union",
"text": "'default'|'primary'|'danger'|'success'",
"type": [
{
"kind": "const",
"text": "'default'",
"type": "string",
"value": "default"
},
{
"kind": "const",
"text": "'primary'",
"type": "string",
"value": "primary"
},
{
"kind": "const",
"text": "'danger'",
"type": "string",
"value": "danger"
},
{
"kind": "const",
"text": "'success'",
"type": "string",
"value": "success"
}
]
},
"defaultValue": "default"
},
{
"keywords": [
{
"name": "type",
"description": "{'small'|'normal'|'big'}"
}
],
"visibility": "public",
"description": "The size of the button.",
"name": "size",
"kind": "let",
"static": false,
"readonly": false,
"type": {
"kind": "union",
"text": "'small'|'normal'|'big'",
"type": [
{
"kind": "const",
"text": "'small'",
"type": "string",
"value": "small"
},
{
"kind": "const",
"text": "'normal'",
"type": "string",
"value": "normal"
},
{
"kind": "const",
"text": "'big'",
"type": "string",
"value": "big"
}
]
},
"defaultValue": "normal"
}
],
"computed": [],
"methods": [],
"components": [],
"description": "The simple component button.",
"keywords": [
{
"name": "component",
"description": "Button"
},
{
"name": "example",
"description": "<Button text=\"Click there!\" on:click={() => ...} />"
}
],
"events": [
{
"name": "click",
"parent": "button",
"modificators": [],
"locations": null,
"loc": null,
"visibility": "public",
"description": "",
"keywords": []
}
],
"slots": [],
"refs": []
}
31 changes: 31 additions & 0 deletions examples/Button.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
/**
* The simple component button.
*
* @component Button
* @example
* <Button text="Click there!" on:click={() => ...} />
*/
/**
* The text content of the button.
* @type {string}
*/
export let text = '';
/**
* The style type of the button.
* @type {'default'|'primary'|'danger'|'success'}
*/
export let type = 'default';
/**
* The size of the button.
* @type {'small'|'normal'|'big'}
*/
export let size = 'normal';
</script>

<button type="button" on:click>
{text}
</button>
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"test:svelte2:integration": "mocha ./test/svelte2/integration/**/*.spec.js",
"test:svelte3": "mocha ./test/svelte3/**/*.spec.js",
"test:svelte3:unit": "mocha ./test/svelte3/unit/**/*.spec.js",
"test:svelte3:integration": "mocha ./test/svelte3/integration/**/*.spec.js"
"test:svelte3:integration": "mocha ./test/svelte3/integration/**/*.spec.js",
"generate-examples": "node ./scripts/generate-examples.js"
},
"repository": {
"type": "git",
Expand Down
69 changes: 69 additions & 0 deletions scripts/generate-examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const glob = require('glob');
const path = require('path');
const fs = require('fs');

const parser = require('../index');

function getFileName(input) {
const basename = path.basename(input);
const dotIndex = basename.lastIndexOf('.');

if (dotIndex > 0) {
return basename.substring(0, dotIndex);
}

return basename;
}

function processSvelteFile(svelteFilePath) {
return new Promise(resolve => {
parser.parse({
version: 3,
filename: svelteFilePath,
ignoredVisibilities: []
}).then(doc => {
const targetFilePath = path.join(path.dirname(svelteFilePath), `${getFileName(svelteFilePath)}.json`);

fs.writeFile(
targetFilePath,
JSON.stringify(doc, undefined, 4),
(err) => {
if (err) {
console.log(`[Error] ${svelteFilePath} (${err.message})`);
resolve({ err, svelteFilePath });

return;
}

console.log('[Done]', svelteFilePath);
resolve({ svelteFilePath });
});
}).catch(err => {
console.log(`[Error] ${svelteFilePath} (${err.message})`);
resolve({ err, svelteFilePath });
});
});
}

glob('./examples/**/*.svelte', (err, files) => {
if (err) {
throw err;
}

const promises = files.map(svelteFilePath => {
return processSvelteFile(svelteFilePath);
});

Promise.all(promises)
.then(errors => {
if (errors) {
errors.forEach(({ err, svelteFilePath }) => {
if (err) {
console.error(`\nError at file processing "${svelteFilePath}":`, err);
}
});
}

process.exit(errors.filter(({ err }) => err).length);
});
});

0 comments on commit 6726d21

Please sign in to comment.