From 3a034dd85d2756a7f40eeea590f17d539407dd5e Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 19 Dec 2022 14:15:04 +0100 Subject: [PATCH] feat(component-meta): make `schema.ignore` accept functions --- .../vue-component-meta/src/index.ts | 17 ++++++++++++++--- .../vue-component-meta/src/types.ts | 7 ++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/vue-language-tools/vue-component-meta/src/index.ts b/vue-language-tools/vue-component-meta/src/index.ts index aa5af07d3..f756842ef 100644 --- a/vue-language-tools/vue-component-meta/src/index.ts +++ b/vue-language-tools/vue-component-meta/src/index.ts @@ -427,8 +427,8 @@ function createSchemaResolvers( const ignore = typeof options === 'object' ? [...options?.ignore ?? []] : []; function shouldIgnore(subtype: ts.Type) { - const type = typeChecker.typeToString(subtype); - if (type === 'any') { + const name = typeChecker.typeToString(subtype); + if (name === 'any') { return true; } @@ -436,7 +436,18 @@ function createSchemaResolvers( return false; } - return ignore.includes(type); + for (const item of ignore) { + if (typeof item === 'function') { + const result = item(name, subtype, typeChecker); + if (result != null) + return result + } + else if (name === item) { + return true; + } + } + + return false; } function setVisited(subtype: ts.Type) { diff --git a/vue-language-tools/vue-component-meta/src/types.ts b/vue-language-tools/vue-component-meta/src/types.ts index 216a1b4c9..fa5be3a60 100644 --- a/vue-language-tools/vue-component-meta/src/types.ts +++ b/vue-language-tools/vue-component-meta/src/types.ts @@ -46,8 +46,13 @@ export type PropertyMetaSchema = string | { kind: 'object', type: string, schema?: Record; }; export type MetaCheckerSchemaOptions = boolean | { - ignore?: string[]; + /** + * A list of type names to be ignored in expending in schema. + * Can be functions to ignore types dynamically. + */ + ignore?: (string | ((name: string, type: ts.Type, typeChecker: ts.TypeChecker) => boolean | void | undefined | null))[]; }; + export interface MetaCheckerOptions { schema?: MetaCheckerSchemaOptions; forceUseTs?: boolean;