Skip to content

Commit a8e1185

Browse files
committed
chore: use minmatch to match pattern
1 parent d733852 commit a8e1185

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/rules/no-danger.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const has = require('object.hasown/polyfill')();
99
const fromEntries = require('object.fromentries/polyfill')();
10+
const minimatch = require('minimatch');
1011

1112
const docsUrl = require('../util/docsUrl');
1213
const jsxUtil = require('../util/jsx');
@@ -79,7 +80,7 @@ module.exports = {
7980
const functionName = node.parent.name.name;
8081

8182
const enableCheckingCustomComponent = customComponentNames.length > 0
82-
&& (customComponentNames[0] === '*' || customComponentNames.some((name) => functionName === name));
83+
&& customComponentNames.some((name) => minimatch(functionName, name));
8384

8485
if ((enableCheckingCustomComponent || jsxUtil.isDOMComponent(node.parent)) && isDangerous(node.name.name)) {
8586
report(context, messages.dangerousProp, 'dangerousProp', {

tests/lib/rules/no-danger.js

+40-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ ruleTester.run('no-danger', rule, {
3232
{ code: '<App />;' },
3333
{ code: '<App dangerouslySetInnerHTML={{ __html: "" }} />;' },
3434
{ code: '<div className="bar"></div>;' },
35-
{ code: '<div className="bar"></div>;', options: [{ customComponentNames: ['*'] }] },
35+
{
36+
code: '<div className="bar"></div>;',
37+
options: [{ customComponentNames: ['*'] }],
38+
},
3639
{
3740
code: `
3841
function App() {
@@ -41,6 +44,14 @@ ruleTester.run('no-danger', rule, {
4144
`,
4245
options: [{ customComponentNames: ['Home'] }],
4346
},
47+
{
48+
code: `
49+
function App() {
50+
return <TextMUI dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
51+
}
52+
`,
53+
options: [{ customComponentNames: ['MUI*'] }],
54+
},
4455
]),
4556
invalid: parsers.all([
4657
{
@@ -76,5 +87,33 @@ ruleTester.run('no-danger', rule, {
7687
],
7788
options: [{ customComponentNames: ['Title'] }],
7889
},
90+
{
91+
code: `
92+
function App() {
93+
return <TextFoo dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
94+
}
95+
`,
96+
errors: [
97+
{
98+
messageId: 'dangerousProp',
99+
data: { name: 'dangerouslySetInnerHTML' },
100+
},
101+
],
102+
options: [{ customComponentNames: ['*Foo'] }],
103+
},
104+
{
105+
code: `
106+
function App() {
107+
return <FooText dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
108+
}
109+
`,
110+
errors: [
111+
{
112+
messageId: 'dangerousProp',
113+
data: { name: 'dangerouslySetInnerHTML' },
114+
},
115+
],
116+
options: [{ customComponentNames: ['Foo*'] }],
117+
},
79118
]),
80119
});

0 commit comments

Comments
 (0)