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

Skip to content

fix(eslint-plugin): [naming-convention] fix precedence of method and property meta selectors #2877

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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,
};
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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