Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch types generated by jsonschema compiler #190

Merged
merged 1 commit into from Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions scripts/generate-rule-files/src/json-schema-to-ts.ts
Expand Up @@ -16,14 +16,36 @@ 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.
*/
export async function generateTypeFromSchema(
schema: JSONSchema4,
typeName: string,
): Promise<string> {
const result: string = await compile(schema, typeName, {
let result: string = await compile(schema, typeName, {
format: false,
bannerComment: '',
style: {
Expand All @@ -33,5 +55,8 @@ export async function generateTypeFromSchema(
unknownAny: false,
});

return cleanJsDoc(result);
result = cleanJsDoc(result);
result = patchTypes(result);

return result;
}
6 changes: 1 addition & 5 deletions src/rules/vue/attribute-hyphenation.d.ts
Expand Up @@ -4,11 +4,7 @@ import type { RuleConfig } from '../rule-config';
* Config.
*/
export interface AttributeHyphenationConfig {
ignore?: (string & {
[k: string]: any;
} & {
[k: string]: any;
})[];
ignore?: string[];
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/rules/vue/html-indent.d.ts
Expand Up @@ -15,11 +15,7 @@ export interface HtmlIndentConfig {
};
switchCase?: number;
alignAttributesVertically?: boolean;
ignores?: (string & {
[k: string]: any;
} & {
[k: string]: any;
})[];
ignores?: string[];
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/rules/vue/script-indent.d.ts
Expand Up @@ -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[];
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/rules/vue/v-on-event-hyphenation.d.ts
Expand Up @@ -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[];
}

/**
Expand Down