1
1
import type { ESLint , Linter , Rule } from 'eslint'
2
2
import type { JSONSchema4 } from 'json-schema'
3
3
import type { Options as CompileOptions } from 'json-schema-to-typescript-lite'
4
- import { compile as compileSchema } from 'json-schema-to-typescript-lite'
4
+ import { compile as compileSchema , normalizeIdentifier } from 'json-schema-to-typescript-lite'
5
5
6
6
export interface RulesTypeGenOptions {
7
7
/**
@@ -178,7 +178,7 @@ export async function pluginsToRulesDTS(
178
178
}
179
179
180
180
export async function compileRule (
181
- name : string ,
181
+ ruleName : string ,
182
182
rule : Rule . RuleModule ,
183
183
compileOptions : Partial < CompileOptions > = { } ,
184
184
) {
@@ -187,7 +187,7 @@ export async function compileRule(
187
187
if ( ! Array . isArray ( schemas ) )
188
188
schemas = [ schemas ]
189
189
190
- const capitalizedName = name . replace ( / (?: ^ | \W + ) ( [ a - z | \d ] ) / g , ( _ , c ) => c . toUpperCase ( ) )
190
+ const id = normalizeIdentifier ( ruleName )
191
191
192
192
const jsdoc : string [ ] = [ ]
193
193
if ( meta . docs ?. description )
@@ -200,59 +200,56 @@ export async function compileRule(
200
200
if ( ! meta . schema || ! schemas . length ) {
201
201
return {
202
202
jsdoc,
203
- name,
203
+ name : ruleName ,
204
204
typeName : '[]' ,
205
205
typeDeclarations : [ ] ,
206
206
}
207
207
}
208
208
209
- async function compile ( schema : JSONSchema4 , name : string , ruleName : string ) {
210
- try {
211
- const compiled = await compileSchema ( schema , name , {
212
- unreachableDefinitions : false ,
213
- strictIndexSignatures : true ,
214
- customName ( schema , keyName ) {
215
- const resolved = schema . title || schema . $id || keyName
216
- if ( resolved === name )
217
- return name
218
- if ( ! resolved )
219
- return undefined !
220
- return `_${ name } _${ resolved } `
221
- } ,
222
- ...compileOptions ,
223
- } )
224
- return compiled
225
- . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' )
226
- }
227
- catch ( error ) {
228
- console . warn ( `Failed to compile schema ${ name } for rule ${ ruleName } . Falling back to unknown.` )
229
- console . error ( error )
230
- return `export type ${ name } = unknown\n`
231
- }
209
+ let lines : string [ ] = [ ]
210
+
211
+ const schema : JSONSchema4 = Array . isArray ( meta . schema )
212
+ ? { type : 'array' , items : meta . schema , definitions : meta . schema ?. [ 0 ] ?. definitions }
213
+ : meta . schema
214
+
215
+ try {
216
+ const compiled = await compileSchema ( schema , id , {
217
+ unreachableDefinitions : false ,
218
+ strictIndexSignatures : true ,
219
+ customName ( schema , keyName ) {
220
+ const resolved = schema . title || schema . $id || keyName
221
+ if ( resolved === id ) {
222
+ return id
223
+ }
224
+ if ( ! resolved )
225
+ return undefined !
226
+ return `_${ normalizeIdentifier ( `${ id } _${ resolved } ` ) } `
227
+ } ,
228
+ ...compileOptions ,
229
+ } )
230
+ lines . push (
231
+ compiled
232
+ . replace ( / \/ \* [ \s \S ] * ?\* \/ / g, '' ) ,
233
+ )
234
+ }
235
+ catch ( error ) {
236
+ console . warn ( `Failed to compile schema ${ ruleName } for rule ${ ruleName } . Falling back to unknown.` )
237
+ console . error ( error )
238
+ lines . push ( `export type ${ ruleName } = unknown\n` )
232
239
}
233
-
234
- let lines : string [ ] = [
235
- await compile (
236
- Array . isArray ( meta . schema )
237
- ? { type : 'array' , items : meta . schema , definitions : meta . schema ?. [ 0 ] ?. definitions }
238
- : meta . schema ,
239
- capitalizedName ,
240
- name ,
241
- ) ,
242
- ]
243
240
244
241
lines = lines
245
242
. join ( '\n' )
246
243
. split ( '\n' )
247
244
. map ( line => line . replace ( / ^ ( e x p o r t ) / , '' ) )
248
245
. filter ( Boolean )
249
246
250
- lines . unshift ( `// ----- ${ name } -----` )
247
+ lines . unshift ( `// ----- ${ ruleName } -----` )
251
248
252
249
return {
253
- name,
250
+ name : ruleName ,
254
251
jsdoc,
255
- typeName : capitalizedName ,
252
+ typeName : id ,
256
253
typeDeclarations : lines ,
257
254
}
258
255
}
0 commit comments