Skip to content
This repository was archived by the owner on Mar 7, 2025. It is now read-only.

Commit 2d55c0c

Browse files
committed
Use json-schema-to-typescript for main schema
1 parent 253739d commit 2d55c0c

File tree

70 files changed

+1231
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1231
-285
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ module.exports = defineConfig({
2929
'@typescript-eslint/ban-ts-comment': 'off',
3030
'@typescript-eslint/explicit-function-return-type': ['error', { allowExpressions: true }],
3131
'@typescript-eslint/interface-name-prefix': 'off',
32-
'@typescript-eslint/member-ordering': 'warn',
32+
'@typescript-eslint/member-ordering': 'off',
3333
'@typescript-eslint/no-explicit-any': 'off',
3434
'@typescript-eslint/no-inferrable-types': 'off',
35+
'@typescript-eslint/no-non-null-assertion': 'off',
3536
'@typescript-eslint/no-parameter-properties': 'off',
3637
'@typescript-eslint/no-unsafe-assignment': 'off',
3738
'@typescript-eslint/no-unused-vars': 'off',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"eslint-plugin-prettier": "~4.0.0",
5252
"eslint-plugin-spellcheck": "~0.0.19",
5353
"eslint-plugin-vue": "~7.18.0",
54+
"json-schema-to-typescript": "~10.1.5",
5455
"np": "~7.5.0",
5556
"prettier": "2.4.1",
5657
"prettier-plugin-organize-imports": "~2.3.4",

scripts/generate-files.ts

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import eslintPluginSpellcheck from 'eslint-plugin-spellcheck';
88
import eslintPluginVue from 'eslint-plugin-vue';
99
import * as fs from 'fs';
1010
import type { JSONSchema4 } from 'json-schema';
11+
import { compile } from 'json-schema-to-typescript';
1112
import * as path from 'path';
1213
import type { Options } from 'prettier';
1314
import { format } from 'prettier';
@@ -120,55 +121,70 @@ function generateOptionType(schema: JSONSchema4): string {
120121
return result;
121122
}
122123

123-
for (const pluginName in generationMap) {
124-
const { rules, name } = generationMap[pluginName]!;
124+
async function main(): Promise<void> {
125+
for (const pluginName in generationMap) {
126+
const { rules, name } = generationMap[pluginName]!;
125127

126-
const ruleProviderDir: string = path.resolve(rulesDir, pluginName);
128+
const ruleProviderDir: string = path.resolve(rulesDir, pluginName);
127129

128-
fs.mkdirSync(ruleProviderDir, { mode: 0o755, recursive: true });
130+
fs.mkdirSync(ruleProviderDir, { mode: 0o755, recursive: true });
129131

130-
Object.entries(rules).forEach(([ruleName, { meta }]) => {
131-
const rulePath: string = path.resolve(ruleProviderDir, `${ruleName}.d.ts`);
132-
let ruleContent: string = "import type { RuleConfig } from '../rule-config';";
132+
for (const [ruleName, { meta }] of Object.entries(rules)) {
133+
const rulePath: string = path.resolve(ruleProviderDir, `${ruleName}.d.ts`);
134+
let ruleContent: string = "import type { RuleConfig } from '../rule-config';";
133135

134-
const ruleNamePascalCase: string = pascalCase(ruleName);
136+
const ruleNamePascalCase: string = pascalCase(ruleName);
135137

136-
const description: string = upperCaseFirst(meta?.docs?.description ?? '');
137-
const seeDocLink: string = meta?.docs?.url ? `@see [${ruleName}](${meta.docs.url})` : '';
138+
const description: string = upperCaseFirst(meta?.docs?.description ?? '');
139+
const seeDocLink: string = meta?.docs?.url ? `@see [${ruleName}](${meta.docs.url})` : '';
138140

139-
const schema: JSONSchema4 | JSONSchema4[] | undefined = meta?.schema;
140-
const mainSchema: JSONSchema4 | undefined = Array.isArray(schema) ? schema[0] : schema;
141-
const sideSchema: JSONSchema4 | undefined =
142-
schema && Array.isArray(schema) && schema.length > 1 ? schema[1] : undefined;
143-
// TODO: vue/max-len has also a third schema
144-
if (mainSchema) {
145-
if (sideSchema) {
146-
ruleContent += `
141+
const schema: JSONSchema4 | JSONSchema4[] | undefined = meta?.schema;
142+
const mainSchema: JSONSchema4 | undefined = Array.isArray(schema) ? schema[0] : schema;
143+
const sideSchema: JSONSchema4 | undefined =
144+
schema && Array.isArray(schema) && schema.length > 1 ? schema[1] : undefined;
145+
// TODO: vue/max-len has also a third schema
146+
if (mainSchema) {
147+
if (sideSchema) {
148+
ruleContent += `
147149
148150
/**
149151
* Config.
150152
*/
151153
export type ${ruleNamePascalCase}Config = ${generateOptionType(sideSchema)}`;
152-
}
154+
}
153155

154-
ruleContent += `
156+
const ruleOption: string = await compile(mainSchema, `${ruleNamePascalCase}Option`, {
157+
bannerComment: '',
158+
style: {
159+
bracketSpacing: true,
160+
printWidth: 120,
161+
semi: true,
162+
singleQuote: true,
163+
tabWidth: 2,
164+
trailingComma: 'none',
165+
useTabs: false
166+
},
167+
unknownAny: false
168+
});
169+
170+
ruleContent += `
155171
156172
/**
157173
* Option.
158174
*/
159-
export type ${ruleNamePascalCase}Option = ${generateOptionType(mainSchema)}
175+
${ruleOption}
160176
161177
/**
162178
* Options.
163179
*/
164180
export type ${ruleNamePascalCase}Options = [${ruleNamePascalCase}Option?${
165-
sideSchema ? `, ${ruleNamePascalCase}Config?` : ''
166-
}];`;
167-
}
181+
sideSchema ? `, ${ruleNamePascalCase}Config?` : ''
182+
}];`;
183+
}
168184

169-
// TODO: Add third option
185+
// TODO: Add third option
170186

171-
ruleContent += `
187+
ruleContent += `
172188
173189
/**
174190
* ${description}
@@ -191,27 +207,36 @@ export type ${ruleNamePascalCase}Options = [${ruleNamePascalCase}Option?${
191207
'${camelCase(pluginName)}/${ruleName}': ${ruleNamePascalCase}RuleConfig;
192208
}
193209
`;
194-
ruleContent = format(ruleContent, PRETTIER_OPTIONS);
195-
fs.writeFileSync(rulePath, ruleContent);
196-
});
210+
ruleContent = format(ruleContent, PRETTIER_OPTIONS);
211+
fs.writeFileSync(rulePath, ruleContent);
212+
}
197213

198-
// Generating index.d.ts for rules
199-
const indexPath: string = path.resolve(ruleProviderDir, 'index.d.ts');
200-
let indexContent: string = Object.keys(rules)
201-
.map((name) => `import type { ${pascalCase(name)}Rule } from './${name}';`)
202-
.join('\n');
214+
// Generating index.d.ts for rules
215+
const indexPath: string = path.resolve(ruleProviderDir, 'index.d.ts');
216+
let indexContent: string = Object.keys(rules)
217+
.map((name) => `import type { ${pascalCase(name)}Rule } from './${name}';`)
218+
.join('\n');
203219

204-
indexContent += `
220+
indexContent += `
205221
206222
/**
207223
* All ${name} rules.
208224
*/
209225
export type ${name}Rules = ${Object.keys(rules)
210-
.map((name) => `${pascalCase(name)}Rule`)
211-
.join(' & ')}
226+
.map((name) => `${pascalCase(name)}Rule`)
227+
.join(' & ')}
212228
`;
213229

214-
indexContent = format(indexContent, PRETTIER_OPTIONS);
230+
indexContent = format(indexContent, PRETTIER_OPTIONS);
215231

216-
fs.writeFileSync(indexPath, indexContent);
232+
fs.writeFileSync(indexPath, indexContent);
233+
}
217234
}
235+
236+
main()
237+
.then(() => {
238+
//
239+
})
240+
.catch(() => {
241+
//
242+
});

src/rules/jsdoc/check-examples.d.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckExamplesOption = {
6+
export interface CheckExamplesOption {
77
allowInlineConfig?: boolean;
8-
baseConfig?: Record<string, any>;
8+
baseConfig?: {
9+
[k: string]: any;
10+
};
911
captionRequired?: boolean;
1012
checkDefaults?: boolean;
1113
checkEslintrc?: boolean;
@@ -21,7 +23,7 @@ export type CheckExamplesOption = {
2123
paddedIndent?: number;
2224
rejectExampleCodeRegex?: string;
2325
reportUnusedDisableDirectives?: boolean;
24-
};
26+
}
2527

2628
/**
2729
* Options.

src/rules/jsdoc/check-indentation.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckIndentationOption = {
6+
export interface CheckIndentationOption {
77
excludeTags?: string[];
8-
};
8+
}
99

1010
/**
1111
* Options.

src/rules/jsdoc/check-param-names.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckParamNamesOption = {
6+
export interface CheckParamNamesOption {
77
allowExtraTrailingParamDocs?: boolean;
88
checkDestructured?: boolean;
99
checkRestProperty?: boolean;
1010
checkTypesPattern?: string;
1111
disableExtraPropertyReporting?: boolean;
1212
enableFixer?: boolean;
1313
useDefaultObjectProperties?: boolean;
14-
};
14+
}
1515

1616
/**
1717
* Options.

src/rules/jsdoc/check-property-names.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckPropertyNamesOption = {
6+
export interface CheckPropertyNamesOption {
77
enableFixer?: boolean;
8-
};
8+
}
99

1010
/**
1111
* Options.

src/rules/jsdoc/check-tag-names.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckTagNamesOption = {
6+
export interface CheckTagNamesOption {
77
definedTags?: string[];
88
jsxTags?: boolean;
9-
};
9+
}
1010

1111
/**
1212
* Options.

src/rules/jsdoc/check-types.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckTypesOption = {
7-
exemptTagContexts?: any[];
6+
export interface CheckTypesOption {
7+
exemptTagContexts?: {
8+
tag?: string;
9+
types?: boolean | string[];
10+
}[];
811
noDefaults?: boolean;
912
unifyParentAndChildTypeChecks?: boolean;
10-
};
13+
}
1114

1215
/**
1316
* Options.

src/rules/jsdoc/check-values.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { RuleConfig } from '../rule-config';
33
/**
44
* Option.
55
*/
6-
export type CheckValuesOption = {
6+
export interface CheckValuesOption {
77
allowedAuthors?: string[];
8-
allowedLicenses?: any;
8+
allowedLicenses?: string[] | boolean;
99
licensePattern?: string;
1010
numericOnlyVariation?: boolean;
11-
};
11+
}
1212

1313
/**
1414
* Options.

0 commit comments

Comments
 (0)