8000 fix: more reliable type ids, close #7 · antfu/eslint-typegen@5e70d45 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 5e70d45

Browse files
committed
fix: more reliable type ids, close #7
1 parent 169b3b6 commit 5e70d45

File tree

9 files changed

+7390
-6000
lines changed

9 files changed

+7390
-6000
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
},
6363
"dependencies": {
6464
"@types/eslint": "^9.6.0",
65-
"json-schema-to-typescript-lite": "^14.0.1",
65+
"json-schema-to-typescript-lite": "^14.1.0",
6666
"ohash": "^1.1.3"
6767
},
6868
"devDependencies": {
@@ -73,6 +73,7 @@
7373
"@types/node": "^22.3.0",
7474
"bumpp": "^9.5.1",
7575
"eslint": "^9.9.0",
76+
"eslint-plugin-jsx-a11y": "^6.9.0",
7677
"esno": "^4.7.0",
7778
"lint-staged": "^15.2.9",
7879
"pnpm": "^9.7.1",

pnpm-lock.yaml

Lines changed: 981 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ESLint, Linter, Rule } from 'eslint'
22
import type { JSONSchema4 } from 'json-schema'
33
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'
55

66
export interface RulesTypeGenOptions {
77
/**
@@ -178,7 +178,7 @@ export async function pluginsToRulesDTS(
178178
}
179179

180180
export 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(/^(export )/, ''))
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

Comments
 (0)
0