Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f10e1a

Browse files
authored
fix(eslint-plugin): [naming-convention] fix precedence of method and property meta selectors (typescript-eslint#2877)
1 parent 51b2269 commit 2f10e1a

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ function isMetaSelector(
1717
return selector in MetaSelectors;
1818
}
1919

20-
export { selectorTypeToMessageString, isMetaSelector };
20+
function isMethodOrPropertySelector(
21+
selector: IndividualAndMetaSelectorsString | Selectors | MetaSelectors,
22+
): boolean {
23+
return (
24+
selector === MetaSelectors.method || selector === MetaSelectors.property
25+
);
26+
}
27+
28+
export {
29+
selectorTypeToMessageString,
30+
isMetaSelector,
31+
isMethodOrPropertySelector,
32+
};

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
UnderscoreOptions,
1414
} from './enums';
1515
import { PredefinedFormatToCheckFunction } from './format';
16-
import { isMetaSelector, selectorTypeToMessageString } from './shared';
16+
import {
17+
isMetaSelector,
18+
isMethodOrPropertySelector,
19+
selectorTypeToMessageString,
20+
} from './shared';
1721
import type { Context, NormalizedSelector } from './types';
1822
import * as util from '../../util';
1923

@@ -49,6 +53,17 @@ function createValidator(
4953
return -1;
5054
}
5155

56+
const aIsMethodOrProperty = isMethodOrPropertySelector(a.selector);
57+
const bIsMethodOrProperty = isMethodOrPropertySelector(b.selector);
58+
59+
// for backward compatibility, method and property have higher precedence than other meta selectors
60+
if (aIsMethodOrProperty && !bIsMethodOrProperty) {
61+
return -1;
62+
}
63+
if (!aIsMethodOrProperty && bIsMethodOrProperty) {
64+
return 1;
65+
}
66+
5267
// both aren't meta selectors
5368
// sort descending - the meta selectors are "least important"
5469
return b.selector - a.selector;

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,31 @@ ruleTester.run('naming-convention', rule, {
14531453
},
14541454
],
14551455
},
1456+
{
1457+
code: `
1458+
const obj = {
1459+
Foo: 42,
1460+
Bar() {
1461+
return 42;
1462+
},
1463+
};
1464+
`,
1465+
parserOptions,
1466+
options: [
1467+
{
1468+
selector: 'memberLike',
1469+
format: ['camelCase'],
1470+
},
1471+
{
1472+
selector: 'property',
1473+
format: ['PascalCase'],
1474+
},
1475+
{
1476+
selector: 'method',
1477+
format: ['PascalCase'],
1478+
},
1479+
],
1480+
},
14561481
],
14571482
invalid: [
14581483
{

0 commit comments

Comments
 (0)