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] fix precedence of method and property meta selectors #2877

Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -17,4 +17,16 @@ function isMetaSelector(
return selector in MetaSelectors;
}

export { selectorTypeToMessageString, isMetaSelector };
function isMethodOrPropertySelector(
selector: IndividualAndMetaSelectorsString | Selectors | MetaSelectors,
): boolean {
return (
selector === MetaSelectors.method || selector === MetaSelectors.property
);
}

export {
selectorTypeToMessageString,
isMetaSelector,
isMethodOrPropertySelector,
};
Expand Up @@ -13,7 +13,11 @@ import {
UnderscoreOptions,
} from './enums';
import { PredefinedFormatToCheckFunction } from './format';
import { isMetaSelector, selectorTypeToMessageString } from './shared';
import {
isMetaSelector,
isMethodOrPropertySelector,
selectorTypeToMessageString,
} from './shared';
import type { Context, NormalizedSelector } from './types';
import * as util from '../../util';

Expand Down Expand Up @@ -49,6 +53,14 @@ function createValidator(
return -1;
}

// for backward compatibility, method and property have higher precedence than other meta selectors
if (isMethodOrPropertySelector(a.selector)) {
return -1;
}
if (isMethodOrPropertySelector(b.selector)) {
return 1;
}

Copy link
Contributor Author

@susisu susisu Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ordering can be modified by adding a flag bit to method and property selectors, as suggested in #2860 (comment).
But I think it's bit tricky, and I implemented a simpler one.
This should work well as long as method and property are the only exceptional cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to special case this here. It's the easiest way to handle it. We can always expand on it later if we need to!


This is almost correct!
This logic will create an unstable ordering when both selectors are method/property - but both aren't the same selector.

If both are method/property (and not the same selector), then the order will be the order as written.

This is technically "consistent" for the most part as config rarely changes, but if someone changes their config and reorders the config then all of a sudden the rule could produce a different result - which will be super confusing and hard to figure out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed that case... Thanks!
Fixed in 77c36ce

// both aren't meta selectors
// sort descending - the meta selectors are "least important"
return b.selector - a.selector;
Expand Down
25 changes: 25 additions & 0 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -1453,6 +1453,31 @@ ruleTester.run('naming-convention', rule, {
},
],
},
{
code: `
const obj = {
Foo: 42,
Bar() {
return 42;
},
};
`,
parserOptions,
options: [
{
selector: 'memberLike',
format: ['camelCase'],
},
{
selector: 'property',
format: ['PascalCase'],
},
{
selector: 'method',
format: ['PascalCase'],
},
],
},
],
invalid: [
{
Expand Down