Skip to content

Commit 1c3621a

Browse files
committed
[Fix] no-danger: avoid a crash on a nested component name
Fixes #3833
1 parent b244638 commit 1c3621a

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
66

77
## Unreleased
88

9+
### Fixed
10+
* [`no-danger`]: avoid a crash on a nested component name ([#3833][] @ljharb)
11+
12+
[#3833]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3833
13+
914
## [7.37.2] - 2024.10.22
1015

1116
### Fixed

lib/rules/no-danger.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ module.exports = {
7777

7878
return {
7979
JSXAttribute(node) {
80-
const functionName = node.parent.name.name;
80+
const nodeName = node.parent.name;
81+
const functionName = nodeName.name || `${nodeName.object.name}.${nodeName.property.name}`;
8182

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

tests/lib/rules/no-danger.js

+44
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,49 @@ ruleTester.run('no-danger', rule, {
129129
},
130130
],
131131
},
132+
{
133+
code: `
134+
import type { ComponentProps } from "react";
135+
136+
const Comp = "div";
137+
const Component = () => <></>;
138+
139+
const NestedComponent = (_props: ComponentProps<"div">) => <></>;
140+
141+
Component.NestedComponent = NestedComponent;
142+
143+
function App() {
144+
return (
145+
<>
146+
<div dangerouslySetInnerHTML={{ __html: "<div>aaa</div>" }} />
147+
<Comp dangerouslySetInnerHTML={{ __html: "<div>aaa</div>" }} />
148+
149+
<Component.NestedComponent
150+
dangerouslySetInnerHTML={{ __html: '<div>aaa</div>' }}
151+
/>
152+
</>
153+
);
154+
}
155+
`,
156+
features: ['fragment', 'types'],
157+
options: [{ customComponentNames: ['*'] }],
158+
errors: [
159+
{
160+
messageId: 'dangerousProp',
161+
data: { name: 'dangerouslySetInnerHTML' },
162+
line: 14,
163+
},
164+
{
165+
messageId: 'dangerousProp',
166+
data: { name: 'dangerouslySetInnerHTML' },
167+
line: 15,
168+
},
169+
{
170+
messageId: 'dangerousProp',
171+
data: { name: 'dangerouslySetInnerHTML' },
172+
line: 18,
173+
},
174+
],
175+
},
132176
]),
133177
});

0 commit comments

Comments
 (0)