Skip to content

Commit b8514e1

Browse files
committed
chore: use glob patterns instead of regex
1 parent e956319 commit b8514e1

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

docs/rules/forbid-component-props.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Examples of **correct** code for this rule:
4444
### `forbid`
4545

4646
An array specifying the names of props that are forbidden. The default value of this option is `['className', 'style']`.
47-
Each array element can either be a string with the property name or object specifying the property name or regex, an optional
47+
Each array element can either be a string with the property name or object specifying the property name or glob string, an optional
4848
custom message, and a component allowlist:
4949

5050
```js
@@ -55,11 +55,11 @@ custom message, and a component allowlist:
5555
}
5656
```
5757

58-
For regex:
58+
For glob string patterns:
5959

6060
```js
6161
{
62-
"propNameRegex": '.-.',
62+
"propNamePattern": '**-**',
6363
"allowedFor": ['div'],
6464
"message": "Avoid using kebab-case except div"
6565
}
@@ -75,11 +75,11 @@ Use `disallowedFor` as an exclusion list to warn on props for specific component
7575
}
7676
```
7777

78-
For regex:
78+
For glob string patterns:
7979

8080
```js
8181
{
82-
"propNameRegex": ".-.",
82+
"propNamePattern": "**-**",
8383
"disallowedFor": ["MyComponent"],
8484
"message": "Avoid using kebab-case for MyComponent"
8585
}

lib/rules/forbid-component-props.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
'use strict';
77

8+
const minimatch = require('minimatch');
89
const docsUrl = require('../util/docsUrl');
910
const report = require('../util/report');
1011

@@ -74,7 +75,7 @@ module.exports = {
7475
{
7576
type: 'object',
7677
properties: {
77-
propNameRegex: { type: 'string' },
78+
propNamePattern: { type: 'string' },
7879
allowedFor: {
7980
type: 'array',
8081
uniqueItems: true,
@@ -87,7 +88,7 @@ module.exports = {
8788
{
8889
type: 'object',
8990
properties: {
90-
propNameRegex: { type: 'string' },
91+
propNamePattern: { type: 'string' },
9192
disallowedFor: {
9293
type: 'array',
9394
uniqueItems: true,
@@ -110,26 +111,26 @@ module.exports = {
110111
const configuration = context.options[0] || {};
111112
const forbid = new Map((configuration.forbid || DEFAULTS).map((value) => {
112113
const propName = typeof value === 'string' ? value : value.propName;
113-
const propRegex = value.propNameRegex;
114-
const prop = propName || propRegex;
114+
const propPattern = value.propNamePattern;
115+
const prop = propName || propPattern;
115116
const options = {
116117
allowList: typeof value === 'string' ? [] : (value.allowedFor || []),
117118
disallowList: typeof value === 'string' ? [] : (value.disallowedFor || []),
118119
message: typeof value === 'string' ? null : value.message,
119-
isRegex: !!value.propNameRegex,
120+
isPattern: !!value.propNamePattern,
120121
};
121122
return [prop, options];
122123
}));
123124

124125
function getPropOptions(prop) {
125-
// Get config options having regex
126-
const propNameRegexArray = [...forbid.entries()].filter((propRegexEntry) => propRegexEntry[1].isRegex);
127-
// Match current prop with regex options, return if matched
128-
const propNameRegex = propNameRegexArray.find(([propRegex]) => new RegExp(propRegex).test(prop));
129-
// Get options for matched propNameRegex
130-
const propNameRegexOptions = propNameRegex && propNameRegex[1];
131-
132-
const options = forbid.get(prop) || propNameRegexOptions;
126+
// Get config options having pattern
127+
const propNamePatternArray = Array.from(forbid.entries()).filter((propEntry) => propEntry[1].isPattern);
128+
// Match current prop with pattern options, return if matched
129+
const propNamePattern = propNamePatternArray.find(([propPattern]) => minimatch(prop, propPattern));
130+
// Get options for matched propNamePattern
131+
const propNamePatternOptions = propNamePattern && propNamePattern[1];
132+
133+
const options = forbid.get(prop) || propNamePatternOptions;
133134
return options;
134135
}
135136

tests/lib/rules/forbid-component-props.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ ruleTester.run('forbid-component-props', rule, {
258258
{
259259
forbid: [
260260
{
261-
propNameRegex: '.-.',
261+
propNamePattern: '**-**',
262262
allowedFor: ['div'],
263263
},
264264
],
@@ -595,7 +595,7 @@ ruleTester.run('forbid-component-props', rule, {
595595
{
596596
forbid: [
597597
{
598-
propNameRegex: '.-.',
598+
propNamePattern: '**-**',
599599
},
600600
],
601601
},
@@ -620,7 +620,7 @@ ruleTester.run('forbid-component-props', rule, {
620620
{
621621
forbid: [
622622
{
623-
propNameRegex: '.-.',
623+
propNamePattern: '**-**',
624624
message: 'Avoid using kebab-case',
625625
},
626626
],
@@ -648,7 +648,7 @@ ruleTester.run('forbid-component-props', rule, {
648648
{
649649
forbid: [
650650
{
651-
propNameRegex: '.-.',
651+
propNamePattern: '**-**',
652652
allowedFor: ['div'],
653653
},
654654
],
@@ -678,7 +678,7 @@ ruleTester.run('forbid-component-props', rule, {
678678
{
679679
forbid: [
680680
{
681-
propNameRegex: '.-.',
681+
propNamePattern: '**-**',
682682
disallowedFor: ['Foo'],
683683
},
684684
],

0 commit comments

Comments
 (0)