From 8dd92ed22b7ea6670ad3b23b57d06921bb037e1f Mon Sep 17 00:00:00 2001 From: tolking Date: Sun, 4 Sep 2022 17:39:40 +0800 Subject: [PATCH] feat: add config `reWebTypesType` --- src/type.ts | 7 +++- src/utils.ts | 95 +++++++++++++++++++++++++++++++++++++++++++++++-- src/webTypes.ts | 5 +-- 3 files changed, 101 insertions(+), 6 deletions(-) diff --git a/src/type.ts b/src/type.ts index 06c5a1e..25c1d87 100644 --- a/src/type.ts +++ b/src/type.ts @@ -29,6 +29,10 @@ export type ReWebTypesSource = ( path: string, ) => Source +export type ReWebTypesType = ( + type: string, +) => undefined | string | BaseContribution + export interface OptionsConfig { entry: string outDir: string @@ -40,6 +44,7 @@ export interface OptionsConfig { reAttribute?: ReAttribute reVeturDescription?: ReVeturDescription reWebTypesSource?: ReWebTypesSource + reWebTypesType?: ReWebTypesType } export interface Config { @@ -239,7 +244,7 @@ export type NamePattern = export type Required = boolean export type NamePatternTemplate = [ string | NamePatternTemplate | NamePattern, - ...(string | NamePatternTemplate | NamePattern)[], + ...(string | NamePatternTemplate | NamePattern)[] ] /** * A reference to an element in Web-Types model. diff --git a/src/utils.ts b/src/utils.ts index 1aad073..d7eb8b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import type { Options, Source } from './type' +import type { Options, Source, BaseContribution } from './type' export function hyphenate(str: string): string { return str.replace(/\B([A-Z])/g, '-$1').toLowerCase() @@ -17,6 +17,63 @@ export function isFunction(val: unknown): val is Function { return typeof val === 'function' } +export function isCommonType(type: string): boolean { + const typeList = [ + 'undefined', + 'null', + 'string', + 'number', + 'object', + 'array', + 'String', + 'Number', + 'Object', + 'Array(<.*>)?', + 'Function(\\(.*\\))?', + 'Symbol', + 'Date', + 'Map(<.*>)?', + 'Set(<.*>)?', + 'WeakMap(<.*>)?', + 'WeakSet(<.*>)?', + 'Promise(<.*>)?', + 'RegExp', + 'JSON', + 'ArrayBuffer', + 'DataView', + 'Int\\d+Array', + 'Uint\\d+Array', + 'Float\\d+Array', + 'BigInt\\d+Array', + 'BigUint\\d+Array', + 'Partial(<.*>)?', + 'Required(<.*>)?', + 'Readonly(<.*>)?', + 'Pick(<.*>)?', + 'Record(<.*>)?', + 'Exclude(<.*>)?', + 'Extract(<.*>)?', + 'Omit(<.*>)?', + 'ReturnType(<.*>)?', + 'InstanceType(<.*>)?', + '(HTML.*)?Element', + ] + const regExp = arrayToRegExp(typeList) + + return regExp.test(getTypeSymbol(type)) +} + +export function arrayToRegExp(arr: string[], flags?: string): RegExp { + return new RegExp(`^(${arr.join('|')})$`, flags) +} + +export function getTypeSymbol(type: string) { + return type + .replace(/\[\]$/, '') + .replace(/<.*?>$/, '') + .replace(/\{.*\}$/, '') +} + export function normalizeFile(file: string): string { return file.replace(/\r\n/g, '\n') } @@ -83,8 +140,40 @@ export function getWebTypesSource( fileName: string, path: string, ): Source { - const { name, reWebTypesSource } = options + const { reWebTypesSource } = options return isFunction(reWebTypesSource) ? reWebTypesSource(title, fileName, path) - : { module: name, symbol: title } + : { symbol: title } +} + +function getType(type: string): string | BaseContribution { + const isPublicType = isCommonType(type) + const symbol = getTypeSymbol(type) + + return isPublicType || !symbol ? type : { name: type, source: { symbol } } +} + +export function getWebTypesType( + options: Options, + type?: string, +): undefined | Array { + const { reWebTypesType } = options + const list = splitString(options, type) + + if (list?.length) { + const types = [] + + for (let i = 0; i < list.length; i++) { + const item = list[i].replaceAll('\\', '') + const result = isFunction(reWebTypesType) + ? reWebTypesType(item) + : getType(item) + + result && types.push(result) + } + + return types + } else { + return undefined + } } diff --git a/src/webTypes.ts b/src/webTypes.ts index a6d86bf..aa31a71 100644 --- a/src/webTypes.ts +++ b/src/webTypes.ts @@ -4,6 +4,7 @@ import { getWebTypesSource, checkArray, splitString, + getWebTypesType, } from './utils' import type { Options, @@ -83,7 +84,7 @@ export function getWebTypes(options: Options, list: NormalizeData[]) { source: getWebTypesSource(options, title, fileName, path), description: item[directivesDescription], 'doc-url': getDocUrl(options, fileName, directives?.title, path), - type: checkArray(splitString(options, item[directivesType])), + type: checkArray(getWebTypesType(options, item[directivesType])), }) } }) @@ -111,7 +112,7 @@ export function getWebTypes(options: Options, list: NormalizeData[]) { name: _item, description: item[propsDescription], 'doc-url': getDocUrl(options, fileName, props?.title, path), - type: checkArray(splitString(options, item[propsType])), + type: checkArray(getWebTypesType(options, item[propsType])), default: item[propsDefault], 'attribute-value': checkArray(_optionsList) ? { type: 'enum' }