@@ -21,38 +21,21 @@ export const noLabelSlotInCheckbox: Rule.RuleModule = {
2121 const jsxNode = node as JSXOpeningElement ;
2222 if ( jsxNode . name . type === "JSXIdentifier" && jsxNode . name . name === "dnn-checkbox" ) {
2323 const parent = context . getAncestors ( ) . find ( ancestor => ancestor . type === "JSXElement" ) ;
24- if ( parent ) {
25- const hasLabel = parent . children . some ( child => {
26- return child . type === "JSXElement" &&
27- child . openingElement . name . type === "JSXIdentifier" &&
28- child . openingElement . name . name === "label" ;
29- } ) ;
30- if ( hasLabel ) {
24+ if ( parent && parent . type === "JSXElement" ) {
25+ const parentElement = parent as any ; // Cast to access children
26+ const innerContent = parentElement . children
27+ . map ( ( child : any ) => context . sourceCode . getText ( child ) )
28+ . join ( "" )
29+ . trim ( ) ;
30+
31+ if ( innerContent ) {
3132 context . report ( {
3233 node : node ,
3334 messageId : "noLabelSlotInCheckbox" ,
3435 fix : ( fixer ) => {
35- if ( parent && parent . type === "JSXElement" && Array . isArray ( parent . children ) ) {
36- const label = parent . children . find ( child =>
37- child &&
38- child . type === "JSXElement" &&
39- child . openingElement &&
40- child . openingElement . name &&
41- child . openingElement . name . type === "JSXIdentifier" &&
42- child . openingElement . name . name === "label" ) ;
43-
44- if ( label && label . type === "JSXElement" ) {
45- const labelText = context . sourceCode . getText ( label ) . replace ( / < l a b e l > | < \/ l a b e l > / g, '' ) . trim ( ) ;
46- const checkboxText = context . sourceCode . getText ( node ) ;
47-
48- // Combine the label text and checkbox into a label wrapper with multiline formatting
49- const fixedText = `<label>\n${ checkboxText } </dnn-checkbox>\n${ labelText } \n</label>` ;
50-
51- return fixer . replaceText ( parent , fixedText ) ;
52- }
53- }
54-
55- return null ;
36+ const checkboxText = context . sourceCode . getText ( node ) ;
37+ const fixedText = `<label>\n${ checkboxText . replace ( innerContent , "" ) . trim ( ) } </dnn-checkbox>\n${ innerContent } \n</label>` ;
38+ return fixer . replaceText ( parent , fixedText ) ;
5639 }
5740 } ) ;
5841 }
0 commit comments