@@ -183,6 +183,13 @@ module.exports = {
183183 }
184184 }
185185
186+ function replaceImportPath ( source , importPath ) {
187+ return source . replace (
188+ / ^ ( [ ' " ] ) ( .+ ) \1$ / ,
189+ ( _ , quote ) => `${ quote } ${ importPath } ${ quote } ` ,
190+ )
191+ }
192+
186193 function checkFileExtension ( source , node ) {
187194 // bail if the declaration doesn't have a source, e.g. "export { foo };", or if it's only partially typed like in an editor
188195 if ( ! source || ! source . value ) { return ; }
@@ -227,6 +234,19 @@ module.exports = {
227234 const extensionRequired = isUseOfExtensionRequired ( extension , ! overrideAction && isPackage ) ;
228235 const extensionForbidden = isUseOfExtensionForbidden ( extension ) ;
229236 if ( extensionRequired && ! extensionForbidden ) {
237+ const fixedImportPath = stringifyPath ( {
238+ pathname : `${
239+ / ( [ \\ / ] | [ \\ / ] ? \. ? \. ) $ / . test ( importPath )
240+ ? `${
241+ importPath . endsWith ( '/' )
242+ ? importPath . slice ( 0 , - 1 )
243+ : importPath
244+ } /index.${ extension } `
245+ : `${ importPath } .${ extension } `
246+ } `,
247+ query,
248+ hash,
249+ } )
230250 context . report ( {
231251 node : source ,
232252 message :
@@ -235,7 +255,7 @@ module.exports = {
235255 fix ( fixer ) {
236256 return fixer . replaceText (
237257 source ,
238- JSON . stringify ( ` ${ importPathWithQueryString } . ${ extension } ` ) ,
258+ replaceImportPath ( source . raw , fixedImportPath ) ,
239259 ) ;
240260 } ,
241261 } : { } ,
0 commit comments