Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit a54be11

Browse files
authored
feat: allow script and template to load from external files (#111)
closes #105
1 parent 954efdd commit a54be11

File tree

6 files changed

+224
-3
lines changed

6 files changed

+224
-3
lines changed

src/parse.ts

+32-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ export function parseSource(documentation: Documentation, source: string, opt: P
7878

7979
// get slots and props from template
8080
if (parts && parts.template) {
81+
const extTemplSrc: string =
82+
parts && parts.template && parts.template.attrs ? parts.template.attrs.src : ''
83+
const extTemplSource =
84+
extTemplSrc && extTemplSrc.length
85+
? fs.readFileSync(path.resolve(path.dirname(opt.filePath), extTemplSrc), {
86+
encoding: 'utf-8',
87+
})
88+
: ''
89+
if (extTemplSource.length) {
90+
parts.template.content = extTemplSource
91+
}
8192
const addTemplateHandlers: TemplateHandler[] = opt.addTemplateHandlers || []
8293
parseTemplate(
8394
parts.template,
@@ -87,15 +98,33 @@ export function parseSource(documentation: Documentation, source: string, opt: P
8798
)
8899
}
89100

90-
const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source
101+
const extSrc: string = parts && parts.script && parts.script.attrs ? parts.script.attrs.src : ''
102+
const extSource =
103+
extSrc && extSrc.length
104+
? fs.readFileSync(path.resolve(path.dirname(opt.filePath), extSrc), {
105+
encoding: 'utf-8',
106+
})
107+
: ''
108+
109+
const scriptSource = extSource.length
110+
? extSource
111+
: parts
112+
? parts.script
113+
? parts.script.content
114+
: undefined
115+
: source
91116
if (scriptSource) {
92117
opt.lang =
93118
(parts && parts.script && parts.script.attrs && parts.script.attrs.lang === 'ts') ||
94-
/\.tsx?$/i.test(path.extname(opt.filePath))
119+
/\.tsx?$/i.test(path.extname(opt.filePath)) ||
120+
/\.tsx?$/i.test(extSrc)
95121
? 'ts'
96122
: 'js'
123+
97124
const addScriptHandlers: ScriptHandler[] = opt.addScriptHandlers || []
98-
parseScript(scriptSource, documentation, [...scriptHandlers, ...addScriptHandlers], opt)
125+
if (scriptSource) {
126+
parseScript(scriptSource, documentation, [...scriptHandlers, ...addScriptHandlers], opt)
127+
}
99128
}
100129

101130
if (!documentation.get('displayName')) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<template src="./template.html"/>
2+
<script src='./script.ts'/>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`tests button should match the snapshot 1`] = `
4+
Object {
5+
"description": "This is an example of creating a reusable button component and using it with external data.",
6+
"displayName": "MyComponent",
7+
"events": Object {
8+
"success": Object {
9+
"description": "Success event when we click",
10+
"properties": undefined,
11+
"type": Object {
12+
"names": Array [
13+
"undefined",
14+
],
15+
},
16+
},
17+
},
18+
"methods": Array [
19+
Object {
20+
"description": "method testing",
21+
"modifiers": Array [],
22+
"name": "onClick",
23+
"params": Array [
24+
Object {
25+
"name": "a",
26+
"type": Object {
27+
"name": "string",
28+
},
29+
},
30+
],
31+
"tags": Object {
32+
"access": Array [
33+
Object {
34+
"description": "public",
35+
"title": "access",
36+
},
37+
],
38+
},
39+
},
40+
],
41+
"props": Object {
42+
"propA": Object {
43+
"description": "An example of a property typed through the annotation",
44+
"tags": Object {},
45+
"type": Object {
46+
"name": "number",
47+
},
48+
},
49+
"propB": Object {
50+
"defaultValue": Object {
51+
"func": false,
52+
"value": "'default value'",
53+
},
54+
"description": "A prop with a default value",
55+
"required": "",
56+
"tags": Object {},
57+
"type": Object {
58+
"name": "string",
59+
},
60+
},
61+
"propC": Object {
62+
"description": "A prop with a hybrid type",
63+
"tags": Object {},
64+
"type": Object {
65+
"name": "TSUnionType",
66+
},
67+
},
68+
"propNoType": Object {
69+
"description": "An example of a property typed through the decorators arguments",
70+
"required": "",
71+
"tags": Object {},
72+
"type": Object {
73+
"name": "string",
74+
},
75+
},
76+
},
77+
"slots": Object {
78+
"default": Object {
79+
"bindings": Object {},
80+
"description": "Use this slot default",
81+
},
82+
},
83+
"tags": Object {
84+
"author": Array [
85+
Object {
86+
"description": "[Rafael](https://github.com/rafaesc92)",
87+
"title": "author",
88+
},
89+
],
90+
"version": Array [
91+
Object {
92+
"description": "1.0.5",
93+
"title": "version",
94+
},
95+
],
96+
},
97+
}
98+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import * as path from 'path'
2+
import { ComponentDoc, PropDescriptor } from '../../../src/Documentation'
3+
import { parse } from '../../../src/main'
4+
5+
const button = path.join(__dirname, './Button.vue')
6+
let docButton: ComponentDoc
7+
describe('tests button', () => {
8+
beforeAll(done => {
9+
docButton = parse(button)
10+
done()
11+
})
12+
13+
it('should return an object', () => {
14+
expect(typeof docButton).toEqual('object')
15+
})
16+
17+
it('should return propB type as string', () => {
18+
expect(docButton.methods).toMatchObject([{ name: 'onClick' }])
19+
})
20+
21+
describe('props', () => {
22+
let props: { [propName: string]: PropDescriptor }
23+
beforeEach(() => {
24+
props = docButton.props || {}
25+
})
26+
it('should return propNoType type as string', () => {
27+
expect(props.propNoType.type).toMatchObject({ name: 'string' })
28+
})
29+
30+
it('should return propA type as number', () => {
31+
expect(props.propA.type).toMatchObject({ name: 'number' })
32+
})
33+
34+
it('should return propB type as string', () => {
35+
expect(props.propB.type).toMatchObject({ name: 'string' })
36+
})
37+
})
38+
39+
it('should match the snapshot', () => {
40+
expect(docButton).toMatchSnapshot()
41+
})
42+
})
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Component, Prop, Vue } from 'vue-property-decorator'
2+
3+
/**
4+
* This is an example of creating a reusable button component and using it with external data.
5+
* @author [Rafael](https://github.com/rafaesc92)
6+
* @version 1.0.5
7+
*/
8+
@Component
9+
export default class MyComponent extends Vue {
10+
public aHiddenData: string
11+
12+
/**
13+
* An example of a property typed through the decorators arguments
14+
*/
15+
@Prop({ type: String })
16+
public propNoType
17+
18+
/**
19+
* An example of a property typed through the annotation
20+
*/
21+
@Prop
22+
public propA: number
23+
24+
/**
25+
* A prop with a default value
26+
*/
27+
@Prop({ default: 'default value' })
28+
public propB: string
29+
30+
/**
31+
* A prop with a hybrid type
32+
*/
33+
@Prop
34+
public propC: string | boolean
35+
36+
/**
37+
* method testing
38+
* @public
39+
*/
40+
public onClick(a: string) {
41+
/**
42+
* Success event when we click
43+
*/
44+
this.$emit('success', a)
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<button class="buttonComponent" @click.prevent="onClick">
2+
<!-- @slot Use this slot default -->
3+
<slot></slot>
4+
</button>

0 commit comments

Comments
 (0)