@@ -37,7 +37,7 @@ const DSIH = "dangerouslySetInnerHTML";
3737 * @param node The JSX child node to check.
3838 * @returns `true` if the node is significant, `false` otherwise.
3939 */
40- function isSignificantChildren ( node : TSESTree . JSXElement [ "children" ] [ number ] ) : boolean {
40+ function isSignificantChildren ( node : TSESTree . JSXElement [ "children" ] [ number ] ) {
4141 if ( ! isJsxText ( node ) ) {
4242 return true ;
4343 }
@@ -48,26 +48,6 @@ function isSignificantChildren(node: TSESTree.JSXElement["children"][number]): b
4848 return ! isFormattingWhitespace ;
4949}
5050
51- /**
52- * Checks if a JSX element has children, either through the `children` prop or as JSX children.
53- * @param context The rule context.
54- * @param node The JSX element to check.
55- * @returns `true` if the element has children, `false` otherwise.
56- */
57- function hasChildren ( context : RuleContext , node : TSESTree . JSXElement ) : boolean {
58- const findJsxAttribute = getJsxAttribute (
59- context ,
60- node . openingElement . attributes ,
61- context . sourceCode . getScope ( node ) ,
62- ) ;
63-
64- if ( findJsxAttribute ( "children" ) != null ) {
65- return true ;
66- }
67-
68- return node . children . some ( isSignificantChildren ) ;
69- }
70-
7151export function create ( context : RuleContext < MessageID , [ ] > ) : RuleListener {
7252 // Fast path: skip if `dangerouslySetInnerHTML` is not present in the file
7353 if ( ! context . sourceCode . text . includes ( DSIH ) ) {
@@ -81,18 +61,13 @@ export function create(context: RuleContext<MessageID, []>): RuleListener {
8161 node . openingElement . attributes ,
8262 context . sourceCode . getScope ( node ) ,
8363 ) ;
84-
85- const DSIHAttr = findJsxAttribute ( DSIH ) ;
86- if ( DSIHAttr == null ) {
87- return ;
88- }
89-
90- if ( hasChildren ( context , node ) ) {
91- context . report ( {
92- messageId : "noDangerouslySetInnerhtmlWithChildren" ,
93- node : DSIHAttr ,
94- } ) ;
95- }
64+ if ( findJsxAttribute ( DSIH ) == null ) return ;
65+ const children = findJsxAttribute ( "children" ) ?? node . children . find ( isSignificantChildren ) ;
66+ if ( children == null ) return ;
67+ context . report ( {
68+ messageId : "noDangerouslySetInnerhtmlWithChildren" ,
69+ node : children ,
70+ } ) ;
9671 } ,
9772 } ;
9873}
0 commit comments