Skip to content

Commit

Permalink
chore: use minmatch to match pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed May 7, 2024
1 parent d733852 commit a8e1185
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

const has = require('object.hasown/polyfill')();
const fromEntries = require('object.fromentries/polyfill')();
const minimatch = require('minimatch');

const docsUrl = require('../util/docsUrl');
const jsxUtil = require('../util/jsx');
Expand Down Expand Up @@ -79,7 +80,7 @@ module.exports = {
const functionName = node.parent.name.name;

const enableCheckingCustomComponent = customComponentNames.length > 0
&& (customComponentNames[0] === '*' || customComponentNames.some((name) => functionName === name));
&& customComponentNames.some((name) => minimatch(functionName, name));

if ((enableCheckingCustomComponent || jsxUtil.isDOMComponent(node.parent)) && isDangerous(node.name.name)) {
report(context, messages.dangerousProp, 'dangerousProp', {
Expand Down
41 changes: 40 additions & 1 deletion tests/lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ ruleTester.run('no-danger', rule, {
{ code: '<App />;' },
{ code: '<App dangerouslySetInnerHTML={{ __html: "" }} />;' },
{ code: '<div className="bar"></div>;' },
{ code: '<div className="bar"></div>;', options: [{ customComponentNames: ['*'] }] },
{
code: '<div className="bar"></div>;',
options: [{ customComponentNames: ['*'] }],
},
{
code: `
function App() {
Expand All @@ -41,6 +44,14 @@ ruleTester.run('no-danger', rule, {
`,
options: [{ customComponentNames: ['Home'] }],
},
{
code: `
function App() {
return <TextMUI dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
}
`,
options: [{ customComponentNames: ['MUI*'] }],
},
]),
invalid: parsers.all([
{
Expand Down Expand Up @@ -76,5 +87,33 @@ ruleTester.run('no-danger', rule, {
],
options: [{ customComponentNames: ['Title'] }],
},
{
code: `
function App() {
return <TextFoo dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
}
`,
errors: [
{
messageId: 'dangerousProp',
data: { name: 'dangerouslySetInnerHTML' },
},
],
options: [{ customComponentNames: ['*Foo'] }],
},
{
code: `
function App() {
return <FooText dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
}
`,
errors: [
{
messageId: 'dangerousProp',
data: { name: 'dangerouslySetInnerHTML' },
},
],
options: [{ customComponentNames: ['Foo*'] }],
},
]),
});

0 comments on commit a8e1185

Please sign in to comment.