Skip to content

Commit fd7d02b

Browse files
authored
fix(eslint-plugin): [naming-convention] handle no options correctly (typescript-eslint#2095)
1 parent cae037f commit fd7d02b

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,17 @@ export default util.createRule<Options, MessageIds>({
360360
},
361361
defaultOptions: defaultCamelCaseAllTheThingsConfig,
362362
create(contextWithoutDefaults) {
363-
const context: Context = contextWithoutDefaults.options
364-
? contextWithoutDefaults
365-
: // only apply the defaults when the user provides no config
366-
Object.setPrototypeOf(
367-
{
368-
options: defaultCamelCaseAllTheThingsConfig,
369-
},
370-
contextWithoutDefaults,
371-
);
363+
const context: Context =
364+
contextWithoutDefaults.options &&
365+
contextWithoutDefaults.options.length > 0
366+
? contextWithoutDefaults
367+
: // only apply the defaults when the user provides no config
368+
Object.setPrototypeOf(
369+
{
370+
options: defaultCamelCaseAllTheThingsConfig,
371+
},
372+
contextWithoutDefaults,
373+
);
372374

373375
const validators = parseOptions(context);
374376

@@ -748,7 +750,7 @@ type ValidatorFunction = (
748750
modifiers?: Set<Modifiers>,
749751
) => void;
750752
type ParsedOptions = Record<SelectorsString, null | ValidatorFunction>;
751-
type Context = TSESLint.RuleContext<MessageIds, Options>;
753+
type Context = Readonly<TSESLint.RuleContext<MessageIds, Options>>;
752754
function parseOptions(context: Context): ParsedOptions {
753755
const normalizedOptions = context.options.map(opt => normalizeOption(opt));
754756
const parsedOptions = util.getEnumNames(Selectors).reduce((acc, k) => {

packages/eslint-plugin/tests/rules/naming-convention.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ const cases: Cases = [
607607

608608
ruleTester.run('naming-convention', rule, {
609609
valid: [
610-
'const x = 1;', // no options shouldn't crash
611610
...createValidTestCases(cases),
612611
{
613612
code: `
@@ -794,6 +793,17 @@ ruleTester.run('naming-convention', rule, {
794793
},
795794
],
796795
invalid: [
796+
{
797+
// make sure we handle no options and apply defaults
798+
code: 'const x_x = 1;',
799+
errors: [{ messageId: 'doesNotMatchFormat' }],
800+
},
801+
{
802+
// make sure we handle empty options and apply defaults
803+
code: 'const x_x = 1;',
804+
options: [],
805+
errors: [{ messageId: 'doesNotMatchFormat' }],
806+
},
797807
...createInvalidTestCases(cases),
798808
{
799809
code: `

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy