Skip to content

Commit

Permalink
fix(eslint-plugin): [naming-convention] fix precedence of method and …
Browse files Browse the repository at this point in the history
…property meta selectors (#2877)
  • Loading branch information
susisu committed Dec 27, 2020
1 parent 51b2269 commit 2f10e1a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
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,17 @@ function createValidator(
return -1;
}

const aIsMethodOrProperty = isMethodOrPropertySelector(a.selector);
const bIsMethodOrProperty = isMethodOrPropertySelector(b.selector);

// for backward compatibility, method and property have higher precedence than other meta selectors
if (aIsMethodOrProperty && !bIsMethodOrProperty) {
return -1;
}
if (!aIsMethodOrProperty && bIsMethodOrProperty) {
return 1;
}

// 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

0 comments on commit 2f10e1a

Please sign in to comment.