Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(eslint-plugin): [naming-convention] fix filter option (#1482)
  • Loading branch information
bradzacher committed Jan 24, 2020
1 parent 802e347 commit 718cd88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/eslint-plugin/docs/rules/naming-convention.md
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
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
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

0 comments on commit 718cd88

Please sign in to comment.