@@ -183,6 +183,33 @@ 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
+
193
+ const parsePath = ( path ) => {
194
+ const hashIndex = path . indexOf ( '#' )
195
+ const queryIndex = path . indexOf ( '?' )
196
+ const hasHash = hashIndex !== - 1
197
+ const hash = hasHash ? path . slice ( hashIndex ) : ''
198
+ const hasQuery = queryIndex !== - 1 && ( ! hasHash || queryIndex < hashIndex )
199
+ const query = hasQuery
200
+ ? path . slice ( queryIndex , hasHash ? hashIndex : undefined )
201
+ : ''
202
+ const pathname = hasQuery
203
+ ? path . slice ( 0 , queryIndex )
204
+ : hasHash
205
+ ? path . slice ( 0 , hashIndex )
206
+ : path
207
+ return { pathname, query, hash }
208
+ }
209
+
210
+ const stringifyPath = ( { pathname, query, hash } ) =>
211
+ pathname + query + hash
212
+
186
213
function checkFileExtension ( source , node ) {
187
214
// 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
215
if ( ! source || ! source . value ) { return ; }
@@ -202,7 +229,11 @@ module.exports = {
202
229
// don't enforce anything on builtins
203
230
if ( ! overrideAction && isBuiltIn ( importPathWithQueryString , context . settings ) ) { return ; }
204
231
205
- const importPath = importPathWithQueryString . replace ( / \? ( .* ) $ / , '' ) ;
232
+ const {
233
+ pathname : importPath ,
234
+ query,
235
+ hash,
236
+ } = parsePath ( importPathWithQueryString )
206
237
207
238
// don't enforce in root external packages as they may have names with `.js`.
208
239
// Like `import Decimal from decimal.js`)
@@ -227,6 +258,19 @@ module.exports = {
227
258
const extensionRequired = isUseOfExtensionRequired ( extension , ! overrideAction && isPackage ) ;
228
259
const extensionForbidden = isUseOfExtensionForbidden ( extension ) ;
229
260
if ( extensionRequired && ! extensionForbidden ) {
261
+ const fixedImportPath = stringifyPath ( {
262
+ pathname : `${
263
+ / ( [ \\ / ] | [ \\ / ] ? \. ? \. ) $ / . test ( importPath )
264
+ ? `${
265
+ importPath . endsWith ( '/' )
266
+ ? importPath . slice ( 0 , - 1 )
267
+ : importPath
268
+ } /index.${ extension } `
269
+ : `${ importPath } .${ extension } `
270
+ } `,
271
+ query,
272
+ hash,
273
+ } )
230
274
context . report ( {
231
275
node : source ,
232
276
message :
@@ -235,7 +279,7 @@ module.exports = {
235
279
fix ( fixer ) {
236
280
return fixer . replaceText (
237
281
source ,
238
- JSON . stringify ( ` ${ importPathWithQueryString } . ${ extension } ` ) ,
282
+ replaceImportPath ( source . raw , fixedImportPath ) ,
239
283
) ;
240
284
} ,
241
285
} : { } ,
@@ -251,9 +295,14 @@ module.exports = {
251
295
fix ( fixer ) {
252
296
return fixer . replaceText (
253
297
source ,
254
- JSON . stringify (
255
- importPath . slice ( 0 , - ( extension . length + 1 ) ) ,
256
- ) ,
298
+ replaceImportPath (
299
+ source . raw ,
300
+ stringifyPath ( {
301
+ pathname : importPath . slice ( 0 , - ( extension . length + 1 ) ) ,
302
+ query,
303
+ hash,
304
+ } ) ,
305
+ ) ,
257
306
) ;
258
307
} ,
259
308
} : { } ,
0 commit comments