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

Skip to content

fix(eslint-plugin): [naming-convention] fix filter option #1482

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
merged 1 commit into from
Jan 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/naming-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ This can be useful if you want to enforce no particular format for a specific se
The `custom` option defines a custom regex that the identifier must (or must not) match. This option allows you to have a bit more finer-grained control over identifiers, letting you ban (or force) certain patterns and substrings.
Accepts an object with the following properties:

- `regex` - accepts a regular expression (anything accepted into `new RegExp(filter)`).
- `regex` - accepts a regular expression (anything accepted into `new RegExp(regex)`).
- `match` - true if the identifier _must_ match the `regex`, false if the identifier _must not_ match the `regex`.

#### `leadingUnderscore` / `trailingUnderscore`
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ function createValidator(
.sort((a, b) => {
if (a.selector === b.selector) {
// in the event of the same selector, order by modifier weight
// sort ascending - the type modifiers are "more important"
return a.modifierWeight - b.modifierWeight;
// sort descending - the type modifiers are "more important"
return b.modifierWeight - a.modifierWeight;
}

/*
Expand Down Expand Up @@ -797,7 +797,7 @@ function createValidator(
// return will break the loop and stop checking configs
// it is only used when the name is known to have failed or succeeded a config.
for (const config of configs) {
if (config.filter?.test(originalName)) {
if (config.filter?.test(originalName) === false) {
// name does not match the filter
continue;
}
Expand Down
19 changes: 17 additions & 2 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const formatTestNames: Readonly<Record<
};

const REPLACE_REGEX = /%/g;
const IGNORED_REGEX = /^.(?!gnored)/; // negative lookahead to not match `[iI]gnored`

type Cases = {
code: string[];
Expand All @@ -99,7 +100,7 @@ function createValidTestCases(cases: Cases): TSESLint.ValidTestCase<Options>[] {
options: [
{
...options,
filter: '[iI]gnored',
filter: IGNORED_REGEX.source,
},
],
code: `// ${JSON.stringify(options)}\n${test.code
Expand Down Expand Up @@ -205,7 +206,7 @@ function createInvalidTestCases(
options: [
{
...options,
filter: '[iI]gnored',
filter: IGNORED_REGEX.source,
},
],
code: `// ${JSON.stringify(options)}\n${test.code
Expand Down Expand Up @@ -724,6 +725,20 @@ ruleTester.run('naming-convention', rule, {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/1478
{
code: `
const child_process = require('child_process');
`,
options: [
{ selector: 'variable', format: ['camelCase', 'UPPER_CASE'] },
{
selector: 'variable',
format: ['snake_case'],
filter: 'child_process',
},
],
},
],
invalid: [
...createInvalidTestCases(cases),
Expand Down