From d733852808f705bee0332716d2a498b7e6d12515 Mon Sep 17 00:00:00 2001 From: akulsr0 Date: Tue, 7 May 2024 07:13:08 +0530 Subject: [PATCH] fix: update options for no-danger rule --- docs/rules/no-danger.md | 7 +------ lib/rules/no-danger.js | 12 +++--------- tests/lib/rules/no-danger.js | 11 ++++------- 3 files changed, 8 insertions(+), 22 deletions(-) diff --git a/docs/rules/no-danger.md b/docs/rules/no-danger.md index 0b16dc4847..6ffe8b9a93 100644 --- a/docs/rules/no-danger.md +++ b/docs/rules/no-danger.md @@ -29,19 +29,14 @@ var Hello =
Hello World
; ```js ... "react/no-danger": [, { - "checkCustomComponents": boolean, "customComponentNames": Array, }] ... ``` -### 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 diff --git a/lib/rules/no-danger.js b/lib/rules/no-danger.js index 2dfcc118ca..760a565ec5 100644 --- a/lib/rules/no-danger.js +++ b/lib/rules/no-danger.js @@ -58,10 +58,6 @@ module.exports = { schema: [{ type: 'object', properties: { - checkCustomComponents: { - default: false, - type: 'boolean', - }, customComponentNames: { items: { type: 'string', @@ -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', { diff --git a/tests/lib/rules/no-danger.js b/tests/lib/rules/no-danger.js index c6392bc841..2c463de4d4 100644 --- a/tests/lib/rules/no-danger.js +++ b/tests/lib/rules/no-danger.js @@ -32,17 +32,14 @@ ruleTester.run('no-danger', rule, { { code: ';' }, { code: ';' }, { code: '
;' }, - { code: '
;', options: [{ checkCustomComponents: false }] }, - { code: '
;', options: [{ checkCustomComponents: true }] }, - { code: ';', options: [{ checkCustomComponents: false }] }, - { code: ';', options: [{ checkCustomComponents: false }] }, + { code: '
;', options: [{ customComponentNames: ['*'] }] }, { code: ` function App() { return hello</span>" }} />; } `, - options: [{ checkCustomComponents: true, customComponentNames: ['Home'] }], + options: [{ customComponentNames: ['Home'] }], }, ]), invalid: parsers.all([ @@ -63,7 +60,7 @@ ruleTester.run('no-danger', rule, { data: { name: 'dangerouslySetInnerHTML' }, }, ], - options: [{ checkCustomComponents: true }], + options: [{ customComponentNames: ['*'] }], }, { code: ` @@ -77,7 +74,7 @@ ruleTester.run('no-danger', rule, { data: { name: 'dangerouslySetInnerHTML' }, }, ], - options: [{ checkCustomComponents: true, customComponentNames: ['Title'] }], + options: [{ customComponentNames: ['Title'] }], }, ]), });