Skip to content

Commit

Permalink
fix: update options for no-danger rule
Browse files Browse the repository at this point in the history
  • Loading branch information
akulsr0 committed May 7, 2024
1 parent a3b7044 commit d733852
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
7 changes: 1 addition & 6 deletions docs/rules/no-danger.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,14 @@ var Hello = <div>Hello World</div>;
```js
...
"react/no-danger": [<enabled>, {
"checkCustomComponents": boolean,
"customComponentNames": Array<string>,
}]
...
```

### checkCustomComponents

Defaults to `false`, when `enabled` allows the rule to check for custom components.

### customComponentNames

If you want to enable this rule for a specific set of custom components, you can pass `checkCustomComponents` as `true` and `customComponentNames` as the array of name of your custom components.
Defaults to `[]`, if you want to enable this rule for all custom components you can pass `customComponentNames` as `['*']`, or else you can pass specific components name to the array.

## When Not To Use It

Expand Down
12 changes: 3 additions & 9 deletions lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ module.exports = {
schema: [{
type: 'object',
properties: {
checkCustomComponents: {
default: false,
type: 'boolean',
},
customComponentNames: {
items: {
type: 'string',
Expand All @@ -76,16 +72,14 @@ module.exports = {

create(context) {
const configuration = context.options[0] || {};
const checkCustomComponents = configuration.checkCustomComponents;
const customComponentNames = configuration.customComponentNames;
const customComponentNames = configuration.customComponentNames || [];

return {
JSXAttribute(node) {
const functionName = node.parent.name.name;

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

if ((enableCheckingCustomComponent || jsxUtil.isDOMComponent(node.parent)) && isDangerous(node.name.name)) {
report(context, messages.dangerousProp, 'dangerousProp', {
Expand Down
11 changes: 4 additions & 7 deletions tests/lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,14 @@ ruleTester.run('no-danger', rule, {
{ code: '<App />;' },
{ code: '<App dangerouslySetInnerHTML={{ __html: "" }} />;' },
{ code: '<div className="bar"></div>;' },
{ code: '<div className="bar"></div>;', options: [{ checkCustomComponents: false }] },
{ code: '<div className="bar"></div>;', options: [{ checkCustomComponents: true }] },
{ code: '<App />;', options: [{ checkCustomComponents: false }] },
{ code: '<App dangerouslySetInnerHTML={{ __html: "" }} />;', options: [{ checkCustomComponents: false }] },
{ code: '<div className="bar"></div>;', options: [{ customComponentNames: ['*'] }] },
{
code: `
function App() {
return <Title dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
}
`,
options: [{ checkCustomComponents: true, customComponentNames: ['Home'] }],
options: [{ customComponentNames: ['Home'] }],
},
]),
invalid: parsers.all([
Expand All @@ -63,7 +60,7 @@ ruleTester.run('no-danger', rule, {
data: { name: 'dangerouslySetInnerHTML' },
},
],
options: [{ checkCustomComponents: true }],
options: [{ customComponentNames: ['*'] }],
},
{
code: `
Expand All @@ -77,7 +74,7 @@ ruleTester.run('no-danger', rule, {
data: { name: 'dangerouslySetInnerHTML' },
},
],
options: [{ checkCustomComponents: true, customComponentNames: ['Title'] }],
options: [{ customComponentNames: ['Title'] }],
},
]),
});

0 comments on commit d733852

Please sign in to comment.