chore(build): optimize the conversion of type in documents (#10419)

This commit is contained in:
qiang 2022-11-04 16:00:42 +08:00 committed by GitHub
parent 8ccb7df2df
commit 98e171ef4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 8 deletions

View File

@ -23,7 +23,7 @@
"@vitejs/plugin-vue": "^2.3.3",
"@vitejs/plugin-vue-jsx": "^1.3.10",
"chalk": "^5.0.1",
"components-helper": "^2.1.3",
"components-helper": "^2.1.4",
"consola": "^2.15.3",
"esbuild": "^0.14.47",
"fast-glob": "^3.2.11",

View File

@ -71,7 +71,7 @@ const reAttribute: ReAttribute = (value, key) => {
.replaceAll(/\B([A-Z])/g, '-$1')
.toLowerCase()
} else if (key === 'Type') {
return str
return rewriteType(str)
.replaceAll(/\bfunction(\(.*\))?(:\s*\w+)?\b/gi, 'Function')
.replaceAll(/\bdate\b/g, 'Date')
.replaceAll(/\([^)]*\)(?!\s*=>)/g, '')
@ -125,6 +125,54 @@ const findModule = (type: string): string | undefined => {
return result
}
const rewriteType = (str: string): string => {
if (!/<.*>/.test(str)) return str
const list = str.matchAll(/<(\w+)Type\s([^>]*)>/g)
return Array.from(list, (item) => {
const type = item ? item[1] : ''
const params = item ? item[2] : ''
switch (type) {
case 'External':
return ''
case 'Enum':
return transformEnum(params)
case 'Function':
return transformFunction(params)
default:
return type.toLowerCase()
}
})
.filter((item) => item)
.join('|')
}
const transformEnum = (str: string) => {
const result = str.match(/:values="\[([^\]]*)\]/)
return result ? result[1].replaceAll(/,\s*/g, ' | ') : 'string'
}
const transformFunction = (str: string) => {
const paramsStr = str.match(/:params="\[(.*)\]"/)
const returnsStr = str.match(/:returns="(.*)"/)
let params = ''
let returns = ''
if (paramsStr) {
const c = paramsStr[0].matchAll(/\['([^\]]*)'\]/g)
params = Array.from(c, (item) => {
return item[1].replaceAll(/',\s*'/g, ': ')
}).join(', ')
}
returns = returnsStr ? returnsStr[1] : 'void'
return `(${params}) => ${returns}`
}
export const buildHelper: TaskFunction = (done) => {
const { name, version } = getPackageManifest(epPackage)

View File

@ -242,7 +242,7 @@ importers:
'@vitejs/plugin-vue': ^2.3.3
'@vitejs/plugin-vue-jsx': ^1.3.10
chalk: ^5.0.1
components-helper: ^2.1.3
components-helper: ^2.1.4
consola: ^2.15.3
esbuild: ^0.14.47
fast-glob: ^3.2.11
@ -264,7 +264,7 @@ importers:
'@vitejs/plugin-vue': 2.3.3_vue@3.2.37
'@vitejs/plugin-vue-jsx': 1.3.10
chalk: 5.0.1
components-helper: 2.1.3
components-helper: 2.1.4
consola: 2.15.3
esbuild: 0.14.47
fast-glob: 3.2.11
@ -5050,9 +5050,9 @@ packages:
resolution: {integrity: sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==}
dev: false
/components-helper/2.1.3:
resolution: {integrity: sha512-t3PFNxbtPtzAY2GEoJBq6OipxwunqZKvaohkRL2uPK5hsuekM47RzhfxPxIqZ9Kd6Nnc2rTUaDnjx9gz2dImEQ==}
engines: {node: '>10.0.0'}
/components-helper/2.1.4:
resolution: {integrity: sha512-XJ1vaj1pULQrctmzbyn9uxnHD1SWpVQnd2OLd+aZeUtQlRd7GDonWqWtu6u8+OZRu6jjTfxN4UuSfMX0z7HQ+g==}
engines: {node: '>15.0.0'}
dependencies:
fast-glob: 3.2.11
dev: false
@ -5117,8 +5117,8 @@ packages:
engines: {node: '>=10'}
hasBin: true
dependencies:
is-text-path: 1.0.1
JSONStream: 1.3.5
is-text-path: 1.0.1
lodash: 4.17.21
meow: 8.1.2
split2: 3.2.2