Skip to content

Commit

Permalink
[Fix] no-danger: avoid a crash on a nested component name
Browse files Browse the repository at this point in the history
Fixes #3833
  • Loading branch information
ljharb committed Oct 23, 2024
1 parent b244638 commit 1c3621a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

## Unreleased

### Fixed
* [`no-danger`]: avoid a crash on a nested component name ([#3833][] @ljharb)

[#3833]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3833

## [7.37.2] - 2024.10.22

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ module.exports = {

return {
JSXAttribute(node) {
const functionName = node.parent.name.name;
const nodeName = node.parent.name;
const functionName = nodeName.name || `${nodeName.object.name}.${nodeName.property.name}`;

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

Expand Down
44 changes: 44 additions & 0 deletions tests/lib/rules/no-danger.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,49 @@ ruleTester.run('no-danger', rule, {
},
],
},
{
code: `
import type { ComponentProps } from "react";
const Comp = "div";
const Component = () => <></>;
const NestedComponent = (_props: ComponentProps<"div">) => <></>;
Component.NestedComponent = NestedComponent;
function App() {
return (
<>
<div dangerouslySetInnerHTML={{ __html: "<div>aaa</div>" }} />
<Comp dangerouslySetInnerHTML={{ __html: "<div>aaa</div>" }} />
<Component.NestedComponent
dangerouslySetInnerHTML={{ __html: '<div>aaa</div>' }}
/>
</>
);
}
`,
features: ['fragment', 'types'],
options: [{ customComponentNames: ['*'] }],
errors: [
{
messageId: 'dangerousProp',
data: { name: 'dangerouslySetInnerHTML' },
line: 14,
},
{
messageId: 'dangerousProp',
data: { name: 'dangerouslySetInnerHTML' },
line: 15,
},
{
messageId: 'dangerousProp',
data: { name: 'dangerouslySetInnerHTML' },
line: 18,
},
],
},
]),
});

0 comments on commit 1c3621a

Please sign in to comment.