Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [naming-convention] handle no options correctly #2095

Merged
merged 1 commit into from May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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