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

Skip to content

Commit 718cd88

Browse files
authored
fix(eslint-plugin): [naming-convention] fix filter option (typescript-eslint#1482)
1 parent 802e347 commit 718cd88

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

packages/eslint-plugin/docs/rules/naming-convention.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ This can be useful if you want to enforce no particular format for a specific se
115115
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.
116116
Accepts an object with the following properties:
117117

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

121121
#### `leadingUnderscore` / `trailingUnderscore`

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,8 @@ function createValidator(
766766
.sort((a, b) => {
767767
if (a.selector === b.selector) {
768768
// in the event of the same selector, order by modifier weight
769-
// sort ascending - the type modifiers are "more important"
770-
return a.modifierWeight - b.modifierWeight;
769+
// sort descending - the type modifiers are "more important"
770+
return b.modifierWeight - a.modifierWeight;
771771
}
772772

773773
/*
@@ -797,7 +797,7 @@ function createValidator(
797797
// return will break the loop and stop checking configs
798798
// it is only used when the name is known to have failed or succeeded a config.
799799
for (const config of configs) {
800-
if (config.filter?.test(originalName)) {
800+
if (config.filter?.test(originalName) === false) {
801801
// name does not match the filter
802802
continue;
803803
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const formatTestNames: Readonly<Record<
8080
};
8181

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

8485
type Cases = {
8586
code: string[];
@@ -99,7 +100,7 @@ function createValidTestCases(cases: Cases): TSESLint.ValidTestCase<Options>[] {
99100
options: [
100101
{
101102
...options,
102-
filter: '[iI]gnored',
103+
filter: IGNORED_REGEX.source,
103104
},
104105
],
105106
code: `// ${JSON.stringify(options)}\n${test.code
@@ -205,7 +206,7 @@ function createInvalidTestCases(
205206
options: [
206207
{
207208
...options,
208-
filter: '[iI]gnored',
209+
filter: IGNORED_REGEX.source,
209210
},
210211
],
211212
code: `// ${JSON.stringify(options)}\n${test.code
@@ -724,6 +725,20 @@ ruleTester.run('naming-convention', rule, {
724725
},
725726
],
726727
},
728+
// https://github.com/typescript-eslint/typescript-eslint/issues/1478
729+
{
730+
code: `
731+
const child_process = require('child_process');
732+
`,
733+
options: [
734+
{ selector: 'variable', format: ['camelCase', 'UPPER_CASE'] },
735+
{
736+
selector: 'variable',
737+
format: ['snake_case'],
738+
filter: 'child_process',
739+
},
740+
],
741+
},
727742
],
728743
invalid: [
729744
...createInvalidTestCases(cases),

0 commit comments

Comments
 (0)