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

Commit abdbcd1

Browse files
authored
fix: extract prop type from decorator arguments as well (#81)
* fix: describe typo * feat: handle slots in render functions * fix: extract prop type from decorator arguments as well * add e2e tests to show it off * add description to show how to use it fixes #79
1 parent 5afc17d commit abdbcd1

File tree

6 files changed

+58
-8
lines changed

6 files changed

+58
-8
lines changed

src/script-handlers/__tests__/classPropHandler.ts

+13
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,18 @@ describe('propHandler', () => {
100100
})
101101
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('testDescribed')
102102
})
103+
104+
it('should extract type from decorator arguments', () => {
105+
const src = `
106+
@Component
107+
export default class MyTest {
108+
@Prop({type:String})
109+
testTyped;
110+
}`
111+
tester(src, {
112+
type: { name: 'string' },
113+
})
114+
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('testTyped')
115+
})
103116
})
104117
})

src/script-handlers/classPropHandler.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getDocblock from '../utils/getDocblock'
55
import getDoclets from '../utils/getDoclets'
66
import getTypeFromAnnotation from '../utils/getTypeFromAnnotation'
77
import transformTagsIntoObject from '../utils/transformTagsIntoObject'
8-
import { describeDefault, describeRequired } from './propHandler'
8+
import { describeDefault, describeRequired, describeType } from './propHandler'
99

1010
export default function propHandler(
1111
documentation: Documentation,
@@ -60,6 +60,10 @@ export default function propHandler(
6060
.filter((p: NodePath) => bt.isObjectProperty(p.node)) as Array<
6161
NodePath<bt.ObjectProperty>
6262
>
63+
// if there is no type annotation, get it from the decorators arguments
64+
if (!propPath.node.typeAnnotation) {
65+
describeType(propsPath, propDescriptor)
66+
}
6367
describeDefault(propsPath, propDescriptor)
6468
describeRequired(propsPath, propDescriptor)
6569
}

src/script-handlers/propHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default function propHandler(documentation: Documentation, path: NodePath
8585
}
8686
}
8787

88-
function describeType(
88+
export function describeType(
8989
propPropertiesPath: Array<NodePath<bt.ObjectProperty>>,
9090
propDescriptor: PropDescriptor,
9191
) {

tests/components/button-typescript/Button.vue

+23-2
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,40 @@ import { Component, Prop, Vue } from 'vue-property-decorator'
1515
*/
1616
@Component
1717
export default class MyComponent extends Vue {
18-
@Prop() propA: number
18+
aHiddenData: string
1919
20+
/**
21+
* An example of a property typed through the decorators arguments
22+
*/
23+
@Prop({ type: String })
24+
propNoType
25+
26+
/**
27+
* An example of a property typed through the annotation
28+
*/
29+
@Prop
30+
propA: number
31+
32+
/**
33+
* A prop with a default value
34+
*/
2035
@Prop({ default: 'default value' })
2136
propB: string
2237
23-
@Prop([String, Boolean])
38+
/**
39+
* A prop with a hybrid type
40+
*/
41+
@Prop
2442
propC: string | boolean
2543
2644
/**
2745
* method testing
2846
* @public
2947
*/
3048
onClick(a: string) {
49+
/**
50+
* Success event when we click
51+
*/
3152
this.$emit('success', a)
3253
}
3354
}

tests/components/button-typescript/__snapshots__/button-ts.test.ts.snap

+12-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Object {
66
"displayName": "MyComponent",
77
"events": Object {
88
"success": Object {
9-
"description": "",
9+
"description": "Success event when we click",
1010
"properties": Array [
1111
Object {
1212
"name": "<anonymous>",
@@ -45,7 +45,7 @@ Object {
4545
],
4646
"props": Object {
4747
"propA": Object {
48-
"description": "",
48+
"description": "An example of a property typed through the annotation",
4949
"tags": Object {},
5050
"type": Object {
5151
"name": "number",
@@ -56,20 +56,28 @@ Object {
5656
"func": false,
5757
"value": "'default value'",
5858
},
59-
"description": "",
59+
"description": "A prop with a default value",
6060
"required": "",
6161
"tags": Object {},
6262
"type": Object {
6363
"name": "string",
6464
},
6565
},
6666
"propC": Object {
67-
"description": "",
67+
"description": "A prop with a hybrid type",
6868
"tags": Object {},
6969
"type": Object {
7070
"name": "TSUnionType",
7171
},
7272
},
73+
"propNoType": Object {
74+
"description": "An example of a property typed through the decorators arguments",
75+
"required": "",
76+
"tags": Object {},
77+
"type": Object {
78+
"name": "string",
79+
},
80+
},
7381
},
7482
"slots": Object {
7583
"default": Object {

tests/components/button-typescript/button-ts.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ describe('tests button', () => {
2323
beforeEach(() => {
2424
props = docButton.props || {}
2525
})
26+
it('should return propNoType type as string', () => {
27+
expect(props.propNoType.type).toMatchObject({ name: 'string' })
28+
})
29+
2630
it('should return propA type as number', () => {
2731
expect(props.propA.type).toMatchObject({ name: 'number' })
2832
})

0 commit comments

Comments
 (0)