Skip to content

Commit d733852

Browse files
committed
fix: update options for no-danger rule
1 parent a3b7044 commit d733852

File tree

3 files changed

+8
-22
lines changed

3 files changed

+8
-22
lines changed

docs/rules/no-danger.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,14 @@ var Hello = <div>Hello World</div>;
2929
```js
3030
...
3131
"react/no-danger": [<enabled>, {
32-
"checkCustomComponents": boolean,
3332
"customComponentNames": Array<string>,
3433
}]
3534
...
3635
```
3736

38-
### checkCustomComponents
39-
40-
Defaults to `false`, when `enabled` allows the rule to check for custom components.
41-
4237
### customComponentNames
4338

44-
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.
39+
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.
4540

4641
## When Not To Use It
4742

lib/rules/no-danger.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ module.exports = {
5858
schema: [{
5959
type: 'object',
6060
properties: {
61-
checkCustomComponents: {
62-
default: false,
63-
type: 'boolean',
64-
},
6561
customComponentNames: {
6662
items: {
6763
type: 'string',
@@ -76,16 +72,14 @@ module.exports = {
7672

7773
create(context) {
7874
const configuration = context.options[0] || {};
79-
const checkCustomComponents = configuration.checkCustomComponents;
80-
const customComponentNames = configuration.customComponentNames;
75+
const customComponentNames = configuration.customComponentNames || [];
8176

8277
return {
8378
JSXAttribute(node) {
8479
const functionName = node.parent.name.name;
8580

86-
const enableCheckingCustomComponent = customComponentNames
87-
? checkCustomComponents && customComponentNames.some((name) => functionName === name)
88-
: checkCustomComponents;
81+
const enableCheckingCustomComponent = customComponentNames.length > 0
82+
&& (customComponentNames[0] === '*' || customComponentNames.some((name) => functionName === name));
8983

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

tests/lib/rules/no-danger.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ 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: [{ checkCustomComponents: false }] },
36-
{ code: '<div className="bar"></div>;', options: [{ checkCustomComponents: true }] },
37-
{ code: '<App />;', options: [{ checkCustomComponents: false }] },
38-
{ code: '<App dangerouslySetInnerHTML={{ __html: "" }} />;', options: [{ checkCustomComponents: false }] },
35+
{ code: '<div className="bar"></div>;', options: [{ customComponentNames: ['*'] }] },
3936
{
4037
code: `
4138
function App() {
4239
return <Title dangerouslySetInnerHTML={{ __html: "<span>hello</span>" }} />;
4340
}
4441
`,
45-
options: [{ checkCustomComponents: true, customComponentNames: ['Home'] }],
42+
options: [{ customComponentNames: ['Home'] }],
4643
},
4744
]),
4845
invalid: parsers.all([
@@ -63,7 +60,7 @@ ruleTester.run('no-danger', rule, {
6360
data: { name: 'dangerouslySetInnerHTML' },
6461
},
6562
],
66-
options: [{ checkCustomComponents: true }],
63+
options: [{ customComponentNames: ['*'] }],
6764
},
6865
{
6966
code: `
@@ -77,7 +74,7 @@ ruleTester.run('no-danger', rule, {
7774
data: { name: 'dangerouslySetInnerHTML' },
7875
},
7976
],
80-
options: [{ checkCustomComponents: true, customComponentNames: ['Title'] }],
77+
options: [{ customComponentNames: ['Title'] }],
8178
},
8279
]),
8380
});

0 commit comments

Comments
 (0)