diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index 59b1bdfee80..d9f5b319c11 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -360,15 +360,17 @@ export default util.createRule({ }, defaultOptions: defaultCamelCaseAllTheThingsConfig, create(contextWithoutDefaults) { - const context: Context = contextWithoutDefaults.options - ? contextWithoutDefaults - : // only apply the defaults when the user provides no config - Object.setPrototypeOf( - { - options: defaultCamelCaseAllTheThingsConfig, - }, - contextWithoutDefaults, - ); + const context: Context = + contextWithoutDefaults.options && + contextWithoutDefaults.options.length > 0 + ? contextWithoutDefaults + : // only apply the defaults when the user provides no config + Object.setPrototypeOf( + { + options: defaultCamelCaseAllTheThingsConfig, + }, + contextWithoutDefaults, + ); const validators = parseOptions(context); @@ -748,7 +750,7 @@ type ValidatorFunction = ( modifiers?: Set, ) => void; type ParsedOptions = Record; -type Context = TSESLint.RuleContext; +type Context = Readonly>; function parseOptions(context: Context): ParsedOptions { const normalizedOptions = context.options.map(opt => normalizeOption(opt)); const parsedOptions = util.getEnumNames(Selectors).reduce((acc, k) => { diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index f6b0143c17f..13f67f6c988 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -607,7 +607,6 @@ const cases: Cases = [ ruleTester.run('naming-convention', rule, { valid: [ - 'const x = 1;', // no options shouldn't crash ...createValidTestCases(cases), { code: ` @@ -794,6 +793,17 @@ ruleTester.run('naming-convention', rule, { }, ], invalid: [ + { + // make sure we handle no options and apply defaults + code: 'const x_x = 1;', + errors: [{ messageId: 'doesNotMatchFormat' }], + }, + { + // make sure we handle empty options and apply defaults + code: 'const x_x = 1;', + options: [], + errors: [{ messageId: 'doesNotMatchFormat' }], + }, ...createInvalidTestCases(cases), { code: `