Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/script-handlers/__tests__/classPropHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,18 @@ describe('propHandler', () => {
})
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('testDescribed')
})

it('should extract type from decorator arguments', () => {
const src = `
@Component
export default class MyTest {
@Prop({type:String})
testTyped;
}`
tester(src, {
type: { name: 'string' },
})
expect(documentation.getPropDescriptor).toHaveBeenCalledWith('testTyped')
})
})
})
6 changes: 5 additions & 1 deletion src/script-handlers/classPropHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getDocblock from '../utils/getDocblock'
import getDoclets from '../utils/getDoclets'
import getTypeFromAnnotation from '../utils/getTypeFromAnnotation'
import transformTagsIntoObject from '../utils/transformTagsIntoObject'
import { describeDefault, describeRequired } from './propHandler'
import { describeDefault, describeRequired, describeType } from './propHandler'

export default function propHandler(
documentation: Documentation,
Expand Down Expand Up @@ -60,6 +60,10 @@ export default function propHandler(
.filter((p: NodePath) => bt.isObjectProperty(p.node)) as Array<
NodePath<bt.ObjectProperty>
>
// if there is no type annotation, get it from the decorators arguments
if (!propPath.node.typeAnnotation) {
describeType(propsPath, propDescriptor)
}
describeDefault(propsPath, propDescriptor)
describeRequired(propsPath, propDescriptor)
}
Expand Down
2 changes: 1 addition & 1 deletion src/script-handlers/propHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function propHandler(documentation: Documentation, path: NodePath
}
}

function describeType(
export function describeType(
propPropertiesPath: Array<NodePath<bt.ObjectProperty>>,
propDescriptor: PropDescriptor,
) {
Expand Down
25 changes: 23 additions & 2 deletions tests/components/button-typescript/Button.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,40 @@ import { Component, Prop, Vue } from 'vue-property-decorator'
*/
@Component
export default class MyComponent extends Vue {
@Prop() propA: number
aHiddenData: string

/**
* An example of a property typed through the decorators arguments
*/
@Prop({ type: String })
propNoType

/**
* An example of a property typed through the annotation
*/
@Prop
propA: number

/**
* A prop with a default value
*/
@Prop({ default: 'default value' })
propB: string

@Prop([String, Boolean])
/**
* A prop with a hybrid type
*/
@Prop
propC: string | boolean

/**
* method testing
* @public
*/
onClick(a: string) {
/**
* Success event when we click
*/
this.$emit('success', a)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object {
"displayName": "MyComponent",
"events": Object {
"success": Object {
"description": "",
"description": "Success event when we click",
"properties": Array [
Object {
"name": "<anonymous>",
Expand Down Expand Up @@ -45,7 +45,7 @@ Object {
],
"props": Object {
"propA": Object {
"description": "",
"description": "An example of a property typed through the annotation",
"tags": Object {},
"type": Object {
"name": "number",
Expand All @@ -56,20 +56,28 @@ Object {
"func": false,
"value": "'default value'",
},
"description": "",
"description": "A prop with a default value",
"required": "",
"tags": Object {},
"type": Object {
"name": "string",
},
},
"propC": Object {
"description": "",
"description": "A prop with a hybrid type",
"tags": Object {},
"type": Object {
"name": "TSUnionType",
},
},
"propNoType": Object {
"description": "An example of a property typed through the decorators arguments",
"required": "",
"tags": Object {},
"type": Object {
"name": "string",
},
},
},
"slots": Object {
"default": Object {
Expand Down
4 changes: 4 additions & 0 deletions tests/components/button-typescript/button-ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ describe('tests button', () => {
beforeEach(() => {
props = docButton.props || {}
})
it('should return propNoType type as string', () => {
expect(props.propNoType.type).toMatchObject({ name: 'string' })
})

it('should return propA type as number', () => {
expect(props.propA.type).toMatchObject({ name: 'number' })
})
Expand Down