From 718cd889c155a75413c571ac006c33fbc271dcc5 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 24 Jan 2020 09:06:01 -0800 Subject: [PATCH] fix(eslint-plugin): [naming-convention] fix filter option (#1482) --- .../docs/rules/naming-convention.md | 2 +- .../src/rules/naming-convention.ts | 6 +++--- .../tests/rules/naming-convention.test.ts | 19 +++++++++++++++++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/naming-convention.md b/packages/eslint-plugin/docs/rules/naming-convention.md index df0cdb02e59..99823a164c1 100644 --- a/packages/eslint-plugin/docs/rules/naming-convention.md +++ b/packages/eslint-plugin/docs/rules/naming-convention.md @@ -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` diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index 8f3fe91ed5d..1938d383a50 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -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; } /* @@ -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; } diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index 04d83717522..f8270225e58 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -80,6 +80,7 @@ const formatTestNames: Readonly[] { options: [ { ...options, - filter: '[iI]gnored', + filter: IGNORED_REGEX.source, }, ], code: `// ${JSON.stringify(options)}\n${test.code @@ -205,7 +206,7 @@ function createInvalidTestCases( options: [ { ...options, - filter: '[iI]gnored', + filter: IGNORED_REGEX.source, }, ], code: `// ${JSON.stringify(options)}\n${test.code @@ -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),