Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve custom tsType handling #25

Merged
merged 5 commits into from
Nov 18, 2021
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
4 changes: 3 additions & 1 deletion src/generator/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ function _genTypes (schema: Schema, spaces: string): string[] {
for (const key in schema.properties) {
const val = schema.properties[key] as Schema
buff.push(...generateJSDoc(val))
if (val.type === 'object') {
if (val.tsType) {
buff.push(`${escapeKey(key)}: ${val.tsType},\n`)
} else if (val.type === 'object') {
buff.push(`${escapeKey(key)}: {`, ..._genTypes(val, spaces + ' '), '},\n')
} else {
let type: string
Expand Down
20 changes: 14 additions & 6 deletions src/loader/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ export default <PluginItem> function babelPluginUntyped (api: ConfigAPI) {
// Extract and apply any manual types
schema.tags = schema.tags?.filter((tag) => {
if (tag.startsWith('@returns')) {
const { type } = tag.match(/^@returns\s+\{(?<type>.+)\}/)?.groups || {}
const { type } = tag.match(/^@returns\s+\{(?<type>[\S\s]+)\}/)?.groups || {}
if (type) {
schema.returns = schema.returns || {}
Object.assign(schema.returns, getTypeDescriptor(type))
return false
}
}
if (tag.startsWith('@param')) {
const { type, param } = tag.match(/^@param\s+\{(?<type>.+)\}\s+(?<param>\w+)/)?.groups || {}
const { type, param } = tag.match(/^@param\s+\{(?<type>[\S\s]+)\}\s+(?<param>\w+)/)?.groups || {}
if (type && param) {
const arg = schema.args?.find(arg => arg.name === param)
if (arg) {
Expand Down Expand Up @@ -186,14 +186,22 @@ function parseJSDocs (input: string | string[]): Schema {

if (firstTag >= 0) {
const tags = clumpLines(lines.slice(firstTag), ['@'], '\n')
const typedefs = tags.reduce((typedefs, tag) => {
const { typedef, alias } = tag.match(/@typedef\s+\{(?<typedef>[\S\s]+)\} (?<alias>.*)/)?.groups || {}
if (typedef && alias) {
typedefs[typedef] = alias
}
return typedefs
}, {} as Record<string, string>)
for (const tag of tags) {
if (tag.startsWith('@type')) {
const type = tag.match(/@type\s+\{(.+)\}/)?.[1]
const type = tag.match(/@type\s+\{([\S\s]+)\}/)?.[1]
// Skip typedefs
if (!type) { continue }
Object.assign(schema, getTypeDescriptor(type))
const typedef = tags.find(t => t.match(/@typedef\s+\{(.+)\} (.*)/)?.[2] === type)
if (typedef) {
for (const typedef in typedefs) {
schema.markdownType = type
schema.tsType = typedef.match(/@typedef\s+\{(.+)\}/)?.[1]
schema.tsType = schema.tsType.replace(new RegExp(typedefs[typedef], 'g'), typedef)
}
continue
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export function isJSType (val: unknown): val is JSType {
return jsTypes.includes(val as any)
}

const FRIENDLY_TYPE_RE = /typeof import\(['"](?<importName>[^'"]+)['"]\)(\[['"]|\.)(?<firstType>[^'"\s]+)(['"]\])?/g
const FRIENDLY_TYPE_RE = /(typeof )?import\(['"](?<importName>[^'"]+)['"]\)(\[['"]|\.)(?<firstType>[^'"\s]+)(['"]\])?/g

export function getTypeDescriptor (type: string | JSType): TypeDescriptor {
if (!type) {
Expand Down
22 changes: 18 additions & 4 deletions test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,13 @@ describe('transform (jsdoc)', () => {
/**
* @type {null | typeof import('path').posix | typeof import('net')['Socket']['PassThrough']}
*/
posix: null
posix: null,
/**
* @type {null | {
* foo: 'bar' | 'baz'
* }}
*/
multiline: null
}
`)
expectCodeToMatch(result, /export default ([\s\S]*)$/, {
Expand All @@ -208,6 +214,14 @@ describe('transform (jsdoc)', () => {
tsType: "null | typeof import('path').posix | typeof import('net')['Socket']['PassThrough']",
markdownType: 'null | PathPosix | NetSocket[\'PassThrough\']'
}
},
multiline: {
$default: null,
$schema: {
title: '',
description: '',
tsType: "null | {\n foo: 'bar' | 'baz'\n}"
}
}
})
})
Expand All @@ -217,7 +231,7 @@ describe('transform (jsdoc)', () => {
export default {
/**
* @typedef {'src' | 'root'} HumanReadable
* @type {HumanReadable}
* @type {Array<HumanReadable>}
*/
srcDir: 'src',
}
Expand All @@ -228,8 +242,8 @@ describe('transform (jsdoc)', () => {
$schema: {
title: '',
description: '',
tsType: "'src' | 'root'",
markdownType: 'HumanReadable'
tsType: "Array<'src' | 'root'>",
markdownType: 'Array<HumanReadable>'
}
}
})
Expand Down