Skip to content

Commit

Permalink
chore: add more meaningful error message for incorrect config names
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Oct 22, 2024
1 parent 21142b1 commit 6b2d045
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ export default function createConfig({
})
}

// More meaningful error message for the user, in case they didn't know the correct config name.
for (const name of configNamesToExtend) {
if (!tseslint.configs[name]) {
const nameInCamelCase = name.replace(/-([a-z])/g, (_, letter) =>
letter.toUpperCase(),
)

// @ts-expect-error
if (tseslint.configs[nameInCamelCase]) {
throw new Error(
`The config name "${name}" is not supported in "extends". ` +
`Please use "${nameInCamelCase}" instead.`,
)
}

throw new Error(`Unknown config name in "extends": ${name}.`)
}
}

const mayHaveJsxInSfc = supportedScriptLangs.jsx || supportedScriptLangs.tsx
const needsTypeAwareLinting = configNamesToExtend.some(
name =>
Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,14 @@ test('#87: should not error if the project root has an older version of espree i
const { stdout } = await runLintAgainst('with-older-espree', FROM_FIXTURES)
expect(stdout).toMatch(WHITESPACE_ONLY)
})

test('should guide user to use camelCase names in "extends"', async () => {
const eslintConfigPath = path.join(__dirname, '../examples/type-checked/eslint.config.js')
const { modify, restore } = setupFileMutations(eslintConfigPath)
modify((oldContents) => oldContents.replace('recommendedTypeChecked', 'recommended-type-checked'))
const { failed, stderr } = await runLintAgainst('type-checked')
restore()

expect(failed).toBe(true)
expect(stderr).contain('Please use "recommendedTypeChecked"')
})

0 comments on commit 6b2d045

Please sign in to comment.