Skip to content

[Bug]: react/no-danger β€” TypeError: Cannot read properties of undefined (reading 'split')Β #3833

Closed
@sylvainDNS

Description

@sylvainDNS

Is there an existing issue for this?

  • I have searched the existing issues and my issue is unique
  • My issue appears in the command-line and not only in the text editor

Description Overview

Hi team, thanks for your work on this lib πŸ™

Using react/no-danger rule with option ['error', { customComponentNames: ['*'] }] cause a command-line error.

Documentation about it: https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/no-danger.md

Here is how I use the rule in my eslint configuration:

    'react/no-danger': ['error', { customComponentNames: ['*'] }],

When I run eslint ./src on my project, an error occurred (only using customComponentNames option):

❯ eslint ./src

Oops! Something went wrong! :(

ESLint: 8.57.0

TypeError: Cannot read properties of undefined (reading 'split')
Occurred while linting /Users/sylvain/react-app/src/App.tsx:74
Rule: "react/no-danger"
    at Minimatch.match (/Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/minimatch/minimatch.js:743:9)
    at minimatch (/Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/minimatch/minimatch.js:125:42)
    at /Users/sylvain/react-app/node_modules/.pnpm/[email protected][email protected]/node_modules/eslint-plugin-react/lib/rules/no-danger.js:82:83
    at Array.some (<anonymous>)
    at JSXAttribute (/Users/sylvain/react-app/node_modules/.pnpm/[email protected][email protected]/node_modules/eslint-plugin-react/lib/rules/no-danger.js:82:68)
    at ruleErrorHandler (/Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/eslint/lib/linter/linter.js:1076:28)
    at /Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/eslint/lib/linter/safe-emitter.js:45:58
    at Array.forEach (<anonymous>)
    at Object.emit (/Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/eslint/lib/linter/safe-emitter.js:45:38)
    at NodeEventGenerator.applySelector (/Users/sylvain/react-app/node_modules/.pnpm/[email protected]/node_modules/eslint/lib/linter/node-event-generator.js:297:26)
/Users/sylvain/react-app:
Exit status 2

This error is triggered by this line

const enableCheckingCustomComponent = customComponentNames.some((name) => minimatch(functionName, name));

Actually I don't know why, but sometimes functionName is undefined

const functionName = node.parent.name.name;

Expected Behavior

Locally I patched eslint-plugin-react like this:

diff --git a/lib/rules/no-danger.js b/lib/rules/no-danger.js
index fb702632082068b08954ebb849d0994da6fac5b6..be0f82185cd9bc8358f29f5a19104ad3343996d7 100644
--- a/lib/rules/no-danger.js
+++ b/lib/rules/no-danger.js
@@ -77,10 +77,9 @@ module.exports = {
 
     return {
       JSXAttribute(node) {
-        const functionName = node.parent.name.name;
-
-        const enableCheckingCustomComponent = customComponentNames.some((name) => minimatch(functionName, name));
+        const functionName = node.parent.name.name ?? '';
 
+        const enableCheckingCustomComponent =  customComponentNames.some((name) => name === '*' || minimatch(functionName, name));
         if ((enableCheckingCustomComponent || jsxUtil.isDOMComponent(node.parent)) && isDangerous(node.name.name)) {
           report(context, messages.dangerousProp, 'dangerousProp', {
             node,

The first change is made to prevent functionName from being undefined (although this doesn't explain why).

The second change is made to skip the check on component names if we defined the rule on '*'.

I can make a PR if you want.

eslint-plugin-react version

v7.37.0

eslint version

v8.57.0

node version

v20.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions