@@ -239,133 +239,129 @@ module.exports = {
239
239
create ( context ) {
240
240
return {
241
241
ImportDeclaration ( node ) {
242
- try {
243
- if ( ! node . source . value . startsWith ( '@primer/react/lib-esm' ) ) {
244
- return
245
- }
242
+ if ( ! node . source . value . startsWith ( '@primer/react/lib-esm' ) ) {
243
+ return
244
+ }
245
+
246
+ const wildcardImportMigrations = wildcardImports . get ( node . source . value )
247
+ if ( ! wildcardImportMigrations ) {
248
+ context . report ( {
249
+ node,
250
+ messageId : 'unknownWildcardImport' ,
251
+ } )
252
+ return
253
+ }
254
+
255
+ /**
256
+ * Maps entrypoint to array of changes. This tuple contains the new
257
+ * imported name from the entrypoint along with the existing local name
258
+ * @type {Map<string, Array<[string, string]>> }
259
+ */
260
+ const changes = new Map ( )
246
261
247
- const wildcardImportMigrations = wildcardImports . get ( node . source . value )
248
- if ( ! wildcardImportMigrations ) {
262
+ for ( const specifier of node . specifiers ) {
263
+ const migration = wildcardImportMigrations . find ( migration => {
264
+ if ( specifier . type === 'ImportDefaultSpecifier' ) {
265
+ return migration . name === 'default'
266
+ }
267
+ return specifier . imported . name === migration . name
268
+ } )
269
+
270
+ // If we do not have a migration, we should report an error even if we
271
+ // cannot autofix it
272
+ if ( ! migration ) {
249
273
context . report ( {
250
274
node,
251
275
messageId : 'unknownWildcardImport' ,
252
276
} )
253
277
return
254
278
}
255
279
256
- /**
257
- * Maps entrypoint to array of changes. This tuple contains the new
258
- * imported name from the entrypoint along with the existing local name
259
- * @type {Map<string, Array<[string, string]>> }
260
- */
261
- const changes = new Map ( )
262
-
263
- for ( const specifier of node . specifiers ) {
264
- const migration = wildcardImportMigrations . find ( migration => {
265
- if ( specifier . type === 'ImportDefaultSpecifier' ) {
266
- return migration . name === 'default'
267
- }
268
- return specifier . imported . name === migration . name
269
- } )
270
-
271
- // If we do not have a migration, we should report an error even if we
272
- // cannot autofix it
273
- if ( ! migration ) {
274
- context . report ( {
275
- node,
276
- messageId : 'unknownWildcardImport' ,
277
- } )
278
- return
279
- }
280
-
281
- if ( ! changes . has ( migration . from ) ) {
282
- changes . set ( migration . from , [ ] )
283
- }
284
-
285
- if ( migration . as ) {
286
- changes . get ( migration . from ) . push ( [ migration . as , migration . as , migration . type ] )
287
- } else {
288
- changes . get ( migration . from ) . push ( [ migration . name , specifier . local . name , migration . type ] )
289
- }
280
+ if ( ! changes . has ( migration . from ) ) {
281
+ changes . set ( migration . from , [ ] )
290
282
}
291
283
292
- if ( changes . length === 0 ) {
293
- return
284
+ if ( migration . as ) {
285
+ changes . get ( migration . from ) . push ( [ migration . as , migration . as , migration . type ] )
286
+ } else {
287
+ changes . get ( migration . from ) . push ( [ migration . name , specifier . local . name , migration . type ] )
294
288
}
289
+ }
295
290
296
- context . report ( {
297
- node,
298
- messageId : 'wildcardMigration' ,
299
- data : {
300
- wildcardEntrypoint : node . source . value ,
301
- } ,
302
- * fix ( fixer ) {
303
- for ( const [ entrypoint , importSpecifiers ] of changes ) {
304
- const typeSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
305
- return type === 'type'
291
+ if ( changes . length === 0 ) {
292
+ return
293
+ }
294
+
295
+ context . report ( {
296
+ node,
297
+ messageId : 'wildcardMigration' ,
298
+ data : {
299
+ wildcardEntrypoint : node . source . value ,
300
+ } ,
301
+ * fix ( fixer ) {
302
+ for ( const [ entrypoint , importSpecifiers ] of changes ) {
303
+ const typeSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
304
+ return type === 'type'
305
+ } )
306
+
307
+ // If all imports are type imports, emit emit as `import type {specifier} from '...'`
308
+ if ( typeSpecifiers . length === importSpecifiers . length ) {
309
+ const namedSpecifiers = typeSpecifiers . filter ( ( [ imported ] ) => {
310
+ return imported !== 'default'
311
+ } )
312
+ const defaultSpecifier = typeSpecifiers . find ( ( [ imported ] ) => {
313
+ return imported === 'default'
306
314
} )
307
315
308
- // If all imports are type imports, emit emit as `import type {specifier} from '...'`
309
- if ( typeSpecifiers . length === importSpecifiers . length ) {
310
- const namedSpecifiers = typeSpecifiers . filter ( ( [ imported ] ) => {
311
- return imported !== 'default'
312
- } )
313
- const defaultSpecifier = typeSpecifiers . find ( ( [ imported ] ) => {
314
- return imported === 'default'
316
+ if ( namedSpecifiers . length > 0 && ! defaultSpecifier ) {
317
+ const specifiers = namedSpecifiers . map ( ( [ imported , local ] ) => {
318
+ if ( imported !== local ) {
319
+ return `${ imported } as ${ local } `
320
+ }
321
+ return imported
315
322
} )
323
+ yield fixer . replaceText ( node , `import type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
324
+ } else if ( namedSpecifiers . length > 0 && defaultSpecifier ) {
325
+ yield fixer . replaceText (
326
+ node ,
327
+ `import type ${ defaultSpecifier [ 1 ] } , ${ specifiers . join ( ', ' ) } from '${ entrypoint } '` ,
328
+ )
329
+ } else if ( defaultSpecifier && namedSpecifiers . length === 0 ) {
330
+ yield fixer . replaceText ( node , `import type ${ defaultSpecifier [ 1 ] } from '${ entrypoint } '` )
331
+ }
316
332
317
- if ( namedSpecifiers . length > 0 && ! defaultSpecifier ) {
318
- const specifiers = namedSpecifiers . map ( ( [ imported , local ] ) => {
319
- if ( imported !== local ) {
320
- return `${ imported } as ${ local } `
321
- }
322
- return imported
323
- } )
324
- yield fixer . replaceText ( node , `import type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
325
- } else if ( namedSpecifiers . length > 0 && defaultSpecifier ) {
326
- yield fixer . replaceText (
327
- node ,
328
- `import type ${ defaultSpecifier [ 1 ] } , ${ specifiers . join ( ', ' ) } from '${ entrypoint } '` ,
329
- )
330
- } else if ( defaultSpecifier && namedSpecifiers . length === 0 ) {
331
- yield fixer . replaceText ( node , `import type ${ defaultSpecifier [ 1 ] } from '${ entrypoint } '` )
332
- }
333
+ return
334
+ }
333
335
334
- return
335
- }
336
+ // Otherwise, we have a mix of type and value imports to emit
337
+ const valueSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
338
+ return type !== 'type'
339
+ } )
336
340
337
- // Otherwise, we have a mix of type and value imports to emit
338
- const valueSpecifiers = importSpecifiers . filter ( ( [ , , type ] ) => {
339
- return type !== 'type'
340
- } )
341
+ if ( valueSpecifiers . length === 0 ) {
342
+ return
343
+ }
341
344
342
- if ( valueSpecifiers . length === 0 ) {
343
- return
345
+ const specifiers = valueSpecifiers . map ( ( [ imported , local ] ) => {
346
+ if ( imported !== local ) {
347
+ return `${ imported } as ${ local } `
344
348
}
349
+ return imported
350
+ } )
351
+ yield fixer . replaceText ( node , `import {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
345
352
353
+ if ( typeSpecifiers . length > 0 ) {
346
354
const specifiers = valueSpecifiers . map ( ( [ imported , local ] ) => {
347
355
if ( imported !== local ) {
348
356
return `${ imported } as ${ local } `
349
357
}
350
358
return imported
351
359
} )
352
- yield fixer . replaceText ( node , `import {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
353
-
354
- if ( typeSpecifiers . length > 0 ) {
355
- const specifiers = valueSpecifiers . map ( ( [ imported , local ] ) => {
356
- if ( imported !== local ) {
357
- return `${ imported } as ${ local } `
358
- }
359
- return imported
360
- } )
361
- yield fixer . insertTextAfter ( node , `import type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
362
- }
360
+ yield fixer . insertTextAfter ( node , `import type {${ specifiers . join ( ', ' ) } } from '${ entrypoint } '` )
363
361
}
364
- } ,
365
- } )
366
- } catch ( error ) {
367
- console . log ( error )
368
- }
362
+ }
363
+ } ,
364
+ } )
369
365
} ,
370
366
}
371
367
} ,
0 commit comments