From ca8a5fa0d20df7433f60e9eba82848ba6b6291bf Mon Sep 17 00:00:00 2001 From: Boris Cherny Date: Fri, 22 Jan 2021 23:41:40 -0800 Subject: [PATCH] [Fix] `jsx-pascal-case`: support regex ignore option Fixes #2905. --- docs/rules/jsx-pascal-case.md | 4 ++-- lib/rules/jsx-pascal-case.js | 15 ++++++++++++--- tests/lib/rules/jsx-pascal-case.js | 7 +++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/docs/rules/jsx-pascal-case.md b/docs/rules/jsx-pascal-case.md index 92430bfc27..aeea7c1e3b 100644 --- a/docs/rules/jsx-pascal-case.md +++ b/docs/rules/jsx-pascal-case.md @@ -46,7 +46,7 @@ Examples of **correct** code for this rule: * `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0. * `allowAllCaps`: optional boolean set to `true` to allow components name in all caps (default to `false`). -* `ignore`: optional string-array of component names to ignore during validation. +* `ignore`: optional string-array of component names to ignore during validation (supports regex). ### `allowAllCaps` @@ -59,4 +59,4 @@ Examples of **correct** code for this rule, when `allowAllCaps` is `true`: ## When Not To Use It -If you are not using JSX. \ No newline at end of file +If you are not using JSX. diff --git a/lib/rules/jsx-pascal-case.js b/lib/rules/jsx-pascal-case.js index d2ee51d4ea..9131a1d7e9 100644 --- a/lib/rules/jsx-pascal-case.js +++ b/lib/rules/jsx-pascal-case.js @@ -79,8 +79,15 @@ module.exports = { type: 'boolean' }, ignore: { - type: 'array' - } + items: [ + { + type: 'string' + }, + ], + minItems: 0, + type: 'array', + uniqueItems: true, + }, }, additionalProperties: false }] @@ -109,7 +116,9 @@ module.exports = { const isPascalCase = testPascalCase(name); const isAllowedAllCaps = allowAllCaps && testAllCaps(name); - const isIgnored = ignore.indexOf(name) !== -1; + const isIgnored = ignore.some( + (entry) => name === entry || name.match(new RegExp(entry, 'u')) + ); if (!isPascalCase && !isAllowedAllCaps && !isIgnored) { let message = `Imported JSX component ${name} must be in PascalCase`; diff --git a/tests/lib/rules/jsx-pascal-case.js b/tests/lib/rules/jsx-pascal-case.js index e7bdc5af75..a0335e37b4 100644 --- a/tests/lib/rules/jsx-pascal-case.js +++ b/tests/lib/rules/jsx-pascal-case.js @@ -76,6 +76,9 @@ ruleTester.run('jsx-pascal-case', rule, { }, { code: '', options: [{ignore: ['IGNORED']}] + }, { + code: '', + options: [{ignore: ['_D\\w+D']}] }, { code: '<$ />' }, { @@ -110,5 +113,9 @@ ruleTester.run('jsx-pascal-case', rule, { }, { code: '<$a />', errors: [{message: 'Imported JSX component $a must be in PascalCase'}] + }, { + code: '', + options: [{ignore: ['_A\\w+Z']}], + errors: [{message: 'Imported JSX component Foo_DEPRECATED must be in PascalCase'}] }] });