Skip to content

Commit

Permalink
Merge pull request #28 from tolking/dev
Browse files Browse the repository at this point in the history
feat: add config `reWebTypesType`
  • Loading branch information
tolking authored Sep 4, 2022
2 parents c018eba + 8dd92ed commit 433aad5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,6 +44,7 @@ export interface OptionsConfig {
reAttribute?: ReAttribute
reVeturDescription?: ReVeturDescription
reWebTypesSource?: ReWebTypesSource
reWebTypesType?: ReWebTypesType
}

export interface Config {
Expand Down Expand Up @@ -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.
Expand Down
95 changes: 92 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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')
}
Expand Down Expand Up @@ -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<string | BaseContribution> {
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
}
}
5 changes: 3 additions & 2 deletions src/webTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getWebTypesSource,
checkArray,
splitString,
getWebTypesType,
} from './utils'
import type {
Options,
Expand Down Expand Up @@ -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])),
})
}
})
Expand Down Expand Up @@ -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' }
Expand Down

0 comments on commit 433aad5

Please sign in to comment.