@@ -13,6 +13,12 @@ import isIgnored, { hasValidExtension } from 'eslint-module-utils/ignore'
13
13
import { hashObject } from 'eslint-module-utils/hash'
14
14
import * as unambiguous from 'eslint-module-utils/unambiguous'
15
15
16
+ import { tsConfigLoader } from 'tsconfig-paths/lib/tsconfig-loader'
17
+
18
+ import includes from 'array-includes'
19
+
20
+ import { parseConfigFileTextToJson } from 'typescript'
21
+
16
22
const log = debug ( 'eslint-plugin-import:ExportMap' )
17
23
18
24
const exportCache = new Map ( )
@@ -445,8 +451,23 @@ ExportMap.parse = function (path, content, context) {
445
451
446
452
const source = makeSourceCode ( content , ast )
447
453
448
- ast . body . forEach ( function ( n ) {
454
+ function isEsModuleInterop ( ) {
455
+ const tsConfigInfo = tsConfigLoader ( {
456
+ cwd : context . parserOptions && context . parserOptions . tsconfigRootDir || process . cwd ( ) ,
457
+ getEnv : ( key ) => process . env [ key ] ,
458
+ } )
459
+ try {
460
+ if ( tsConfigInfo . tsConfigPath !== undefined ) {
461
+ const jsonText = fs . readFileSync ( tsConfigInfo . tsConfigPath ) . toString ( )
462
+ const tsConfig = parseConfigFileTextToJson ( tsConfigInfo . tsConfigPath , jsonText ) . config
463
+ return tsConfig . compilerOptions . esModuleInterop
464
+ }
465
+ } catch ( e ) {
466
+ return false
467
+ }
468
+ }
449
469
470
+ ast . body . forEach ( function ( n ) {
450
471
if ( n . type === 'ExportDefaultDeclaration' ) {
451
472
const exportMeta = captureDoc ( source , docStyleParsers , n )
452
473
if ( n . declaration . type === 'Identifier' ) {
@@ -528,9 +549,14 @@ ExportMap.parse = function (path, content, context) {
528
549
} )
529
550
}
530
551
552
+ const isEsModuleInteropTrue = isEsModuleInterop ( )
553
+
554
+ const exports = [ 'TSExportAssignment' ]
555
+ isEsModuleInteropTrue && exports . push ( 'TSNamespaceExportDeclaration' )
556
+
531
557
// This doesn't declare anything, but changes what's being exported.
532
- if ( n . type === 'TSExportAssignment' ) {
533
- const exportedName = n . expression . name
558
+ if ( includes ( exports , n . type ) ) {
559
+ const exportedName = n . expression && n . expression . name || n . id . name
534
560
const declTypes = [
535
561
'VariableDeclaration' ,
536
562
'ClassDeclaration' ,
@@ -541,18 +567,17 @@ ExportMap.parse = function (path, content, context) {
541
567
'TSAbstractClassDeclaration' ,
542
568
'TSModuleDeclaration' ,
543
569
]
544
- const exportedDecls = ast . body . filter ( ( { type, id, declarations } ) =>
545
- declTypes . includes ( type ) &&
546
- (
547
- ( id && id . name === exportedName ) ||
548
- ( declarations && declarations . find ( d => d . id . name === exportedName ) )
549
- )
550
- )
570
+ const exportedDecls = ast . body . filter ( ( { type, id, declarations } ) => includes ( declTypes , type ) && (
571
+ ( id && id . name === exportedName ) || ( declarations && declarations . find ( ( d ) => d . id . name === exportedName ) )
572
+ ) )
551
573
if ( exportedDecls . length === 0 ) {
552
574
// Export is not referencing any local declaration, must be re-exporting
553
575
m . namespace . set ( 'default' , captureDoc ( source , docStyleParsers , n ) )
554
576
return
555
577
}
578
+ if ( isEsModuleInteropTrue ) {
579
+ m . namespace . set ( 'default' , { } )
580
+ }
556
581
exportedDecls . forEach ( ( decl ) => {
557
582
if ( decl . type === 'TSModuleDeclaration' ) {
558
583
if ( decl . body && decl . body . type === 'TSModuleDeclaration' ) {
0 commit comments