Skip to content

Commit

Permalink
fix(eslint-plugin): [naming-convention] handle no options correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed May 25, 2020
1 parent cae037f commit fd7d02b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
22 changes: 12 additions & 10 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Expand Up @@ -360,15 +360,17 @@ export default util.createRule<Options, MessageIds>({
},
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);

Expand Down Expand Up @@ -748,7 +750,7 @@ type ValidatorFunction = (
modifiers?: Set<Modifiers>,
) => void;
type ParsedOptions = Record<SelectorsString, null | ValidatorFunction>;
type Context = TSESLint.RuleContext<MessageIds, Options>;
type Context = Readonly<TSESLint.RuleContext<MessageIds, Options>>;
function parseOptions(context: Context): ParsedOptions {
const normalizedOptions = context.options.map(opt => normalizeOption(opt));
const parsedOptions = util.getEnumNames(Selectors).reduce((acc, k) => {
Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -607,7 +607,6 @@ const cases: Cases = [

ruleTester.run('naming-convention', rule, {
valid: [
'const x = 1;', // no options shouldn't crash
...createValidTestCases(cases),
{
code: `
Expand Down Expand Up @@ -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: `
Expand Down

0 comments on commit fd7d02b

Please sign in to comment.