Skip to content

Commit de1e10b

Browse files
committed
chore: add a Proxy to the config object to provide better error messages
1 parent a2820c7 commit de1e10b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/configs.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,29 @@ function toArray<T>(value: T | T[]): T[] {
2323
}
2424

2525
export class TsEslintConfigForVue {
26-
constructor(readonly configName: ExtendableConfigName) {
27-
this.configName = configName
26+
// the name property is here to provide better error messages when ESLint throws an error
27+
name: string
28+
29+
#configName: ExtendableConfigName
30+
31+
constructor(configName: ExtendableConfigName) {
32+
this.#configName = configName
33+
34+
this.name = `vueTsConfigs.${configName}`
2835
}
2936

3037
needsTypeChecking(): boolean {
31-
if (this.configName === 'disableTypeChecked') {
38+
if (this.#configName === 'disableTypeChecked') {
3239
return false
3340
}
34-
if (this.configName === 'all') {
41+
if (this.#configName === 'all') {
3542
return true
3643
}
37-
return this.configName.includes('TypeChecked')
44+
return this.#configName.includes('TypeChecked')
3845
}
3946

4047
toConfigArray(): FlatConfig.ConfigArray {
41-
return toArray(tseslint.configs[this.configName])
48+
return toArray(tseslint.configs[this.#configName])
4249
.flat()
4350
.map(config =>
4451
config.files && config.files.includes('**/*.ts')
@@ -52,5 +59,17 @@ export class TsEslintConfigForVue {
5259
}
5360

5461
export const vueTsConfigs = Object.fromEntries(
55-
CONFIG_NAMES.map(name => [name, new TsEslintConfigForVue(name)]),
62+
CONFIG_NAMES.map(name => [
63+
name,
64+
new Proxy(new TsEslintConfigForVue(name), {
65+
// `ownKeys` is called by ESLint when validating the config object.
66+
// The only possible scenario where this is called is when the placeholder object
67+
// isn't replaced, which means it's passed to ESLint without being wrapped by
68+
// `defineConfigWithVueTs()`
69+
// We throw an error here to provide a better error message to the user.
70+
ownKeys() {
71+
throw new Error('Please wrap the config object with `defineConfigWithVueTs()`')
72+
},
73+
}),
74+
]),
5675
) as Record<ExtendableConfigName, TsEslintConfigForVue>

0 commit comments

Comments
 (0)