Skip to content

Commit

Permalink
fix(docgen): fix ts array, intersection type print
Browse files Browse the repository at this point in the history
  • Loading branch information
simsim0709 committed Apr 14, 2020
1 parent fec4f65 commit 4ca38bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@
</template>

<script lang="ts">
import { Vue, Component, Prop } from 'nuxt-property-decorator'
interface Book {
name: string
desc: string
}
@Component
export default class SomeComponent extends Vue {
/** Description for propA */
@Prop({ type: Number, default: 3}) readonly propA!: number
@Prop({ default: 3 })
readonly propA!: 'string literal' | 3 | Book | string[] | number[] | Book[] | Array<Book>
@Prop({ default: 3 })
readonly propB!: 'string literal' & 3 & Book & string[] & number[] & Book[] & Array<Book>
/**
* Description for mwthod. Don't froget to put `@public`.
/**
* Description for mwthod. Don't froget to put `@public`.
* @public
*/
*/
public onClick(name: string) {
/**
* Success event when we click.
/**
* Success event when we click.
* @property {string} name description of name.
* @property {boolean} success always true.
*/
this.$emit('success', { name, success: true })
* @property {boolean} success always true.
*/
this.$emit('success', { name, success: true })
}
}
</script>
54 changes: 46 additions & 8 deletions packages/vue-docgen-api/src/utils/getTypeFromAnnotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,56 @@ const TS_TYPE_NAME_MAP: { [name: string]: string } = {
TSVoidKeyword: 'void',
TSUndefinedKeyword: 'undefined',
TSNullKeyword: 'null',
TSNeverKeyword: 'never'
TSNeverKeyword: 'never',
TSThisType: 'this'
}

function printUnionOrIntersectionType(t: bt.TSType): string | number | boolean {
if (bt.isTSArrayType(t)) {
return printArrayType(t)
}

if (bt.isTSTypeReference(t) && bt.isIdentifier(t.typeName) && t.typeName.name === 'Array') {
return printArrayTypeFromGeneric(t)
}

return printType(t)
}

function printArrayType(t: bt.TSArrayType) {
return `${printType(t.elementType)}[]`
}

function printArrayTypeFromGeneric(t: bt.TSTypeReference) {
const type = t.typeParameters ? t.typeParameters.params[0] : undefined
return `${printType(type)}[]`
}

function printType(t: bt.TSType | undefined): string {
if (!t) {
return ''
}

if (TS_TYPE_NAME_MAP[t.type]) {
return TS_TYPE_NAME_MAP[t.type]
}

if (bt.isTSLiteralType(t)) {
return String(t.literal.value)
}

if (bt.isTSTypeReference(t) && bt.isIdentifier(t.typeName)) {
return t.typeName.name
}

return t.type
}

function getTypeObjectFromTSType(type: bt.TSType): ParamType {
const name =
bt.isTSTypeReference(type) && bt.isIdentifier(type.typeName)
? type.typeName.name
: bt.isTSUnionType(type)
? type.types.map(t => TS_TYPE_NAME_MAP[t.type]).join(' | ')
: TS_TYPE_NAME_MAP[type.type]
? TS_TYPE_NAME_MAP[type.type]
: type.type
bt.isTSUnionType(type) || bt.isTSIntersectionType(type)
? type.types.map(printUnionOrIntersectionType).join(bt.isTSUnionType(type) ? ' | ' : ' & ')
: printType(type)

return { name }
}
Expand Down

0 comments on commit 4ca38bf

Please sign in to comment.