Skip to content

Commit

Permalink
[Fix] jsx-pascal-case: support minimatch ignore option
Browse files Browse the repository at this point in the history
Fixes #2905.
  • Loading branch information
Boris Cherny committed Jan 25, 2021
1 parent 0e9a193 commit 983b4c2
Show file tree
Hide file tree
Showing 4 changed files with 26 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 [minimatch](https://github.com/isaacs/minimatch)).

### `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.
16 changes: 13 additions & 3 deletions lib/rules/jsx-pascal-case.js
Expand Up @@ -6,6 +6,7 @@
'use strict';

const elementType = require('jsx-ast-utils/elementType');
const minimatch = require('minimatch');
const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');

Expand Down Expand Up @@ -79,8 +80,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 +117,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 || minimatch(name, entry, {noglobstar: true})
);

if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
let message = `Imported JSX component ${name} must be in PascalCase`;
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"doctrine": "^2.1.0",
"has": "^1.0.3",
"jsx-ast-utils": "^2.4.1 || ^3.0.0",
"minimatch": "^3.0.4",
"object.entries": "^1.1.2",
"object.fromentries": "^2.0.2",
"object.values": "^1.1.1",
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/jsx-pascal-case.js
Expand Up @@ -76,6 +76,12 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<IGNORED />',
options: [{ignore: ['IGNORED']}]
}, {
code: '<Foo_DEPRECATED />',
options: [{ignore: ['*_D*D']}]
}, {
code: '<Foo_DEPRECATED />',
options: [{ignore: ['*_+(DEPRECATED|IGNORED)']}]
}, {
code: '<$ />'
}, {
Expand Down Expand Up @@ -110,5 +116,9 @@ ruleTester.run('jsx-pascal-case', rule, {
}, {
code: '<$a />',
errors: [{message: 'Imported JSX component $a must be in PascalCase'}]
}, {
code: '<Foo_DEPRECATED />',
options: [{ignore: ['*_FOO']}],
errors: [{message: 'Imported JSX component Foo_DEPRECATED must be in PascalCase'}]
}]
});

0 comments on commit 983b4c2

Please sign in to comment.