@@ -28,12 +28,12 @@ module.exports = {
2828 } ,
2929 create ( context ) {
3030 const sourceCode = context . sourceCode ?? context . getSourceCode ( )
31-
31+
3232 // Helper function to check if a node is in a text block
33- const isNodeInTextBlock = ( node ) => {
33+ const isNodeInTextBlock = node => {
3434 let siblings = node . parent . children
3535 if ( ! siblings || siblings . length === 0 ) return false
36-
36+
3737 // Filter out whitespace nodes
3838 siblings = siblings . filter ( childNode => {
3939 return (
@@ -46,17 +46,17 @@ module.exports = {
4646 ! ( childNode . type === 'Literal' && / ^ \s + $ / . test ( childNode . value ) )
4747 )
4848 } )
49-
49+
5050 const index = siblings . findIndex ( childNode => {
5151 return childNode . range === node . range
5252 } )
53-
53+
5454 const prevSibling = siblings [ index - 1 ]
5555 const nextSibling = siblings [ index + 1 ]
56-
56+
5757 const prevSiblingIsText = prevSibling && prevSibling . type === 'JSXText'
5858 const nextSiblingIsText = nextSibling && nextSibling . type === 'JSXText'
59-
59+
6060 // If there's text on either side
6161 if ( prevSiblingIsText || nextSiblingIsText ) {
6262 // Skip if the only text adjacent to the link is a period
@@ -65,55 +65,49 @@ module.exports = {
6565 }
6666 return true
6767 }
68-
68+
6969 return false
7070 }
71-
71+
7272 return {
7373 JSXElement ( node ) {
7474 const name = getJSXOpeningElementName ( node . openingElement )
7575 const parentName = node . parent . openingElement ?. name ?. name
7676 const parentsToSkip = [ 'Heading' , 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ]
77-
77+
7878 // Check for HTML anchor elements
79- if (
80- isHTMLElement ( node . openingElement ) &&
81- name === 'a' &&
82- node . parent . children
83- ) {
79+ if ( isHTMLElement ( node . openingElement ) && name === 'a' && node . parent . children ) {
8480 // Skip if anchor is nested inside of a heading
8581 if ( parentsToSkip . includes ( parentName ) ) return
86-
82+
8783 // Skip if anchor has className (might have distinguishing styles)
8884 const classNameAttribute = getJSXOpeningElementAttribute ( node . openingElement , 'className' )
8985 if ( classNameAttribute ) return
90-
86+
9187 // Check for anchor in text block
9288 if ( isNodeInTextBlock ( node ) ) {
9389 // Skip if anchor child is a JSX element
9490 const jsxElementChildren = node . children . filter ( child => child . type === 'JSXElement' )
9591 if ( jsxElementChildren . length > 0 ) return
96-
92+
9793 // Report and autofix
9894 context . report ( {
9995 node,
10096 messageId : 'htmlAnchorInTextBlock' ,
10197 fix ( fixer ) {
10298 // Get all attributes from the anchor to transfer to Link
103- const attributes = node . openingElement . attributes
104- . map ( attr => sourceCode . getText ( attr ) )
105- . join ( ' ' )
106-
99+ const attributes = node . openingElement . attributes . map ( attr => sourceCode . getText ( attr ) ) . join ( ' ' )
100+
107101 // Create the Link component opening and closing tags
108102 const openingTag = `<Link ${ attributes } >`
109103 const closingTag = '</Link>'
110-
104+
111105 // Apply fixes to the opening and closing tags
112106 const openingFix = fixer . replaceText ( node . openingElement , openingTag )
113107 const closingFix = fixer . replaceText ( node . closingElement , closingTag )
114-
108+
115109 return [ openingFix , closingFix ]
116- }
110+ } ,
117111 } )
118112 }
119113 }
0 commit comments