@@ -183,6 +183,13 @@ module.exports = {
183
183
}
184
184
}
185
185
186
+ function replaceImportPath ( source , importPath ) {
187
+ return source . replace (
188
+ / ^ ( [ ' " ] ) ( .+ ) \1$ / ,
189
+ ( _ , quote ) => `${ quote } ${ importPath } ${ quote } ` ,
190
+ )
191
+ }
192
+
186
193
function checkFileExtension ( source , node ) {
187
194
// bail if the declaration doesn't have a source, e.g. "export { foo };", or if it's only partially typed like in an editor
188
195
if ( ! source || ! source . value ) { return ; }
@@ -227,6 +234,19 @@ module.exports = {
227
234
const extensionRequired = isUseOfExtensionRequired ( extension , ! overrideAction && isPackage ) ;
228
235
const extensionForbidden = isUseOfExtensionForbidden ( extension ) ;
229
236
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
+ } )
230
250
context . report ( {
231
251
node : source ,
232
252
message :
@@ -235,7 +255,7 @@ module.exports = {
235
255
fix ( fixer ) {
236
256
return fixer . replaceText (
237
257
source ,
238
- JSON . stringify ( ` ${ importPathWithQueryString } . ${ extension } ` ) ,
258
+ replaceImportPath ( source . raw , fixedImportPath ) ,
239
259
) ;
240
260
} ,
241
261
} : { } ,
0 commit comments