From 313dae19bc647e5bf7889a5d19f713fc9f567752 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Sat, 22 Apr 2023 12:18:27 +0200 Subject: [PATCH] patch types --- .../src/json-schema-to-ts.ts | 29 +++++++++++++++++-- src/rules/vue/attribute-hyphenation.d.ts | 6 +--- src/rules/vue/html-indent.d.ts | 6 +--- src/rules/vue/script-indent.d.ts | 6 +--- src/rules/vue/v-on-event-hyphenation.d.ts | 6 +--- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/scripts/generate-rule-files/src/json-schema-to-ts.ts b/scripts/generate-rule-files/src/json-schema-to-ts.ts index de3c17b8..f54a13b5 100644 --- a/scripts/generate-rule-files/src/json-schema-to-ts.ts +++ b/scripts/generate-rule-files/src/json-schema-to-ts.ts @@ -16,6 +16,28 @@ function cleanJsDoc(content: string): string { .join('\n'); } +/** + * Replace some types that are generated by `json-schema-to-typescript`. + */ +export function patchTypes(content: string): string { + const replacements: Array<{ + pattern: RegExp; + replacement: string; + }> = [ + { + pattern: + /\(string & \{\s*\[k: string\]: any\s*\} & \{\s*\[k: string\]: any\s*\}\)\[\]/, + replacement: 'string[]', + }, + ]; + + for (const { pattern, replacement } of replacements) { + content = content.replace(pattern, replacement); + } + + return content; +} + /** * Generate a type from the given JSON schema. */ @@ -23,7 +45,7 @@ export async function generateTypeFromSchema( schema: JSONSchema4, typeName: string, ): Promise { - const result: string = await compile(schema, typeName, { + let result: string = await compile(schema, typeName, { format: false, bannerComment: '', style: { @@ -33,5 +55,8 @@ export async function generateTypeFromSchema( unknownAny: false, }); - return cleanJsDoc(result); + result = cleanJsDoc(result); + result = patchTypes(result); + + return result; } diff --git a/src/rules/vue/attribute-hyphenation.d.ts b/src/rules/vue/attribute-hyphenation.d.ts index 2c460148..1d759350 100644 --- a/src/rules/vue/attribute-hyphenation.d.ts +++ b/src/rules/vue/attribute-hyphenation.d.ts @@ -4,11 +4,7 @@ import type { RuleConfig } from '../rule-config'; * Config. */ export interface AttributeHyphenationConfig { - ignore?: (string & { - [k: string]: any; - } & { - [k: string]: any; - })[]; + ignore?: string[]; } /** diff --git a/src/rules/vue/html-indent.d.ts b/src/rules/vue/html-indent.d.ts index 9d9643ab..4c9912aa 100644 --- a/src/rules/vue/html-indent.d.ts +++ b/src/rules/vue/html-indent.d.ts @@ -15,11 +15,7 @@ export interface HtmlIndentConfig { }; switchCase?: number; alignAttributesVertically?: boolean; - ignores?: (string & { - [k: string]: any; - } & { - [k: string]: any; - })[]; + ignores?: string[]; } /** diff --git a/src/rules/vue/script-indent.d.ts b/src/rules/vue/script-indent.d.ts index 9c81564c..9c7281bc 100644 --- a/src/rules/vue/script-indent.d.ts +++ b/src/rules/vue/script-indent.d.ts @@ -6,11 +6,7 @@ import type { RuleConfig } from '../rule-config'; export interface ScriptIndentConfig { baseIndent?: number; switchCase?: number; - ignores?: (string & { - [k: string]: any; - } & { - [k: string]: any; - })[]; + ignores?: string[]; } /** diff --git a/src/rules/vue/v-on-event-hyphenation.d.ts b/src/rules/vue/v-on-event-hyphenation.d.ts index 6f30f893..f380d402 100644 --- a/src/rules/vue/v-on-event-hyphenation.d.ts +++ b/src/rules/vue/v-on-event-hyphenation.d.ts @@ -5,11 +5,7 @@ import type { RuleConfig } from '../rule-config'; */ export interface VOnEventHyphenationConfig { autofix?: boolean; - ignore?: (string & { - [k: string]: any; - } & { - [k: string]: any; - })[]; + ignore?: string[]; } /**