Skip to content

Commit

Permalink
[Fix] jsx-pascal-case: support regex ignore option
Browse files Browse the repository at this point in the history
Fixes #2905.
  • Loading branch information
Boris Cherny committed Jan 23, 2021
1 parent 0e9a193 commit ca8a5fa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/rules/jsx-pascal-case.md
Expand Up @@ -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`

Expand All @@ -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.
If you are not using JSX.
15 changes: 12 additions & 3 deletions lib/rules/jsx-pascal-case.js
Expand Up @@ -79,8 +79,15 @@ module.exports = {
type: 'boolean'
},
ignore: {
type: 'array'
}
items: [
{
type: 'string'
},
],
minItems: 0,
type: 'array',
uniqueItems: true,
},
},
additionalProperties: false
}]
Expand Down Expand Up @@ -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`;
Expand Down
7 changes: 7 additions & 0 deletions tests/lib/rules/jsx-pascal-case.js
Expand Up @@ -76,6 +76,9 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<IGNORED />',
options: [{ignore: ['IGNORED']}]
}, {
code: '<Foo_DEPRECATED />',
options: [{ignore: ['_D\\w+D']}]
}, {
code: '<$ />'
}, {
Expand Down Expand Up @@ -110,5 +113,9 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<$a />',
errors: [{message: 'Imported JSX component $a must be in PascalCase'}]
}, {
code: '<Foo_DEPRECATED />',
options: [{ignore: ['_A\\w+Z']}],
errors: [{message: 'Imported JSX component Foo_DEPRECATED must be in PascalCase'}]
}]
});

0 comments on commit ca8a5fa

Please sign in to comment.