Skip to content

Commit 4881bae

Browse files
Boris Chernyljharb
Boris Cherny
authored andcommitted
[New] jsx-pascal-case: support minimatch ignore option
Fixes jsx-eslint#2905.
1 parent 0e9a193 commit 4881bae

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77

88
### Added
99
* [`jsx-no-target-blank`]: add fixer ([#2862][] @Nokel81)
10+
* [`jsx-pascal-case`]: support minimatch `ignore` option ([#2906][] @bcherny)
1011

1112
### Fixed
1213
* [`jsx-no-constructed-context-values`]: avoid a crash with `as X` TS code ([#2894][] @ljharb)
@@ -16,6 +17,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1617
* [`no-typos`]: avoid a crash on bindingless `prop-types` import; add warning ([#2899][] @ljharb)
1718
* [`jsx-curly-brace-presence`]: ignore containers with comments ([#2900][] @golopot)
1819

20+
[#2906]: https://github.com/yannickcr/eslint-plugin-react/pull/2906
1921
[#2900]: https://github.com/yannickcr/eslint-plugin-react/pull/2900
2022
[#2899]: https://github.com/yannickcr/eslint-plugin-react/issues/2899
2123
[#2897]: https://github.com/yannickcr/eslint-plugin-react/pull/2897

docs/rules/jsx-pascal-case.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Examples of **correct** code for this rule:
4646

4747
* `enabled`: for enabling the rule. 0=off, 1=warn, 2=error. Defaults to 0.
4848
* `allowAllCaps`: optional boolean set to `true` to allow components name in all caps (default to `false`).
49-
* `ignore`: optional string-array of component names to ignore during validation.
49+
* `ignore`: optional string-array of component names to ignore during validation (supports [minimatch](https://github.com/isaacs/minimatch)-style globs).
5050

5151
### `allowAllCaps`
5252

@@ -59,4 +59,4 @@ Examples of **correct** code for this rule, when `allowAllCaps` is `true`:
5959

6060
## When Not To Use It
6161

62-
If you are not using JSX.
62+
If you are not using JSX.

lib/rules/jsx-pascal-case.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const elementType = require('jsx-ast-utils/elementType');
9+
const minimatch = require('minimatch');
910
const docsUrl = require('../util/docsUrl');
1011
const jsxUtil = require('../util/jsx');
1112

@@ -79,7 +80,14 @@ module.exports = {
7980
type: 'boolean'
8081
},
8182
ignore: {
82-
type: 'array'
83+
items: [
84+
{
85+
type: 'string'
86+
}
87+
],
88+
minItems: 0,
89+
type: 'array',
90+
uniqueItems: true
8391
}
8492
},
8593
additionalProperties: false
@@ -109,7 +117,9 @@ module.exports = {
109117

110118
const isPascalCase = testPascalCase(name);
111119
const isAllowedAllCaps = allowAllCaps && testAllCaps(name);
112-
const isIgnored = ignore.indexOf(name) !== -1;
120+
const isIgnored = ignore.some(
121+
(entry) => name === entry || minimatch(name, entry, {noglobstar: true})
122+
);
113123

114124
if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
115125
let message = `Imported JSX component ${name} must be in PascalCase`;

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"doctrine": "^2.1.0",
3535
"has": "^1.0.3",
3636
"jsx-ast-utils": "^2.4.1 || ^3.0.0",
37+
"minimatch": "^3.0.4",
3738
"object.entries": "^1.1.2",
3839
"object.fromentries": "^2.0.2",
3940
"object.values": "^1.1.1",

tests/lib/rules/jsx-pascal-case.js

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ ruleTester.run('jsx-pascal-case', rule, {
7676
}, {
7777
code: '<IGNORED />',
7878
options: [{ignore: ['IGNORED']}]
79+
}, {
80+
code: '<Foo_DEPRECATED />',
81+
options: [{ignore: ['*_D*D']}]
82+
}, {
83+
code: '<Foo_DEPRECATED />',
84+
options: [{ignore: ['*_+(DEPRECATED|IGNORED)']}]
7985
}, {
8086
code: '<$ />'
8187
}, {
@@ -110,5 +116,9 @@ ruleTester.run('jsx-pascal-case', rule, {
110116
}, {
111117
code: '<$a />',
112118
errors: [{message: 'Imported JSX component $a must be in PascalCase'}]
119+
}, {
120+
code: '<Foo_DEPRECATED />',
121+
options: [{ignore: ['*_FOO']}],
122+
errors: [{message: 'Imported JSX component Foo_DEPRECATED must be in PascalCase'}]
113123
}]
114124
});

0 commit comments

Comments
 (0)