@@ -86,6 +86,7 @@ export default createEslintRule<
8686 const testKeywordWithinDescribe = withinDescribe || fn || TestCaseName . it
8787 const testFnDisabled =
8888 testFnKeyWork === testKeywordWithinDescribe ? testFnKeyWork : undefined
89+ const { sourceCode } = context
8990
9091 let describeNestingLevel = 0
9192
@@ -99,33 +100,43 @@ export default createEslintRule<
99100 for ( const specifier of node . specifiers ) {
100101 if ( specifier . type !== 'ImportSpecifier' ) continue
101102 if ( specifier . imported . type !== 'Identifier' ) continue
102- if ( specifier . local . name !== specifier . imported . name ) continue
103- if ( specifier . local . name === oppositeTestKeyword ) {
104- context . report ( {
105- node : specifier ,
106- data : { testFnKeyWork, oppositeTestKeyword } ,
107- messageId : 'consistentMethod' ,
108- fix : ( fixer ) => {
109- const remainingSpecifiers = node . specifiers . filter (
110- ( spec ) => spec . local . name !== oppositeTestKeyword ,
103+ if ( specifier . imported . name !== oppositeTestKeyword ) continue
104+
105+ context . report ( {
106+ node : specifier ,
107+ data : { testFnKeyWork, oppositeTestKeyword } ,
108+ messageId : 'consistentMethod' ,
109+ fix : ( fixer ) => {
110+ const remainingSpecifiers = node . specifiers . filter (
111+ ( spec ) => spec !== specifier ,
112+ )
113+ if ( remainingSpecifiers . length > 0 ) {
114+ const hasPreferredSpecifier = remainingSpecifiers . some (
115+ ( spec ) =>
116+ spec . type === AST_NODE_TYPES . ImportSpecifier &&
117+ spec . imported . type === AST_NODE_TYPES . Identifier &&
118+ spec . imported . name === testFnDisabled ,
119+ )
120+ const importNames = remainingSpecifiers . map ( ( spec ) =>
121+ sourceCode . getText ( spec ) ,
111122 )
112- if ( remainingSpecifiers . length > 0 ) {
113- const importText = remainingSpecifiers
114- . map ( ( spec ) => spec . local . name )
115- . join ( ', ' )
116- const lastSpecifierRange = node . specifiers . at ( - 1 ) ?. range
117- if ( ! lastSpecifierRange ) return null
118-
119- return fixer . replaceTextRange (
120- [ node . specifiers [ 0 ] . range [ 0 ] , lastSpecifierRange [ 1 ] ] ,
121- importText ,
122- )
123+ if ( ! hasPreferredSpecifier ) {
124+ importNames . push ( testFnDisabled )
123125 }
124126
125- return fixer . replaceText ( specifier . local , testFnDisabled )
126- } ,
127- } )
128- }
127+ const importText = importNames . join ( ', ' )
128+ const lastSpecifierRange = node . specifiers . at ( - 1 ) ?. range
129+ if ( ! lastSpecifierRange ) return null
130+
131+ return fixer . replaceTextRange (
132+ [ node . specifiers [ 0 ] . range [ 0 ] , lastSpecifierRange [ 1 ] ] ,
133+ importText ,
134+ )
135+ }
136+
137+ return fixer . replaceText ( specifier , testFnDisabled )
138+ } ,
139+ } )
129140 }
130141 } ,
131142 CallExpression ( node : TSESTree . CallExpression ) {
0 commit comments