11import type { ESLint , Linter , Rule } from 'eslint'
22import type { JSONSchema4 } from 'json-schema'
33import 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'
55
66export interface RulesTypeGenOptions {
77 /**
@@ -178,7 +178,7 @@ export async function pluginsToRulesDTS(
178178}
179179
180180export async function compileRule (
181- name : string ,
181+ ruleName : string ,
182182 rule : Rule . RuleModule ,
183183 compileOptions : Partial < CompileOptions > = { } ,
184184) {
@@ -187,7 +187,7 @@ export async function compileRule(
187187 if ( ! Array . isArray ( schemas ) )
188188 schemas = [ schemas ]
189189
190- const capitalizedName = name . replace ( / (?: ^ | \W + ) ( [ a - z | \d ] ) / g , ( _ , c ) => c . toUpperCase ( ) )
190+ const id = normalizeIdentifier ( ruleName )
191191
192192 const jsdoc : string [ ] = [ ]
193193 if ( meta . docs ?. description )
@@ -200,59 +200,56 @@ export async function compileRule(
200200 if ( ! meta . schema || ! schemas . length ) {
201201 return {
202202 jsdoc,
203- name,
203+ name : ruleName ,
204204 typeName : '[]' ,
205205 typeDeclarations : [ ] ,
206206 }
207207 }
208208
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` )
232239 }
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- ]
243240
244241 lines = lines
245242 . join ( '\n' )
246243 . split ( '\n' )
247244 . map ( line => line . replace ( / ^ ( e x p o r t ) / , '' ) )
248245 . filter ( Boolean )
249246
250- lines . unshift ( `// ----- ${ name } -----` )
247+ lines . unshift ( `// ----- ${ ruleName } -----` )
251248
252249 return {
253- name,
250+ name : ruleName ,
254251 jsdoc,
255- typeName : capitalizedName ,
252+ typeName : id ,
256253 typeDeclarations : lines ,
257254 }
258255}
0 commit comments