@@ -13,6 +13,10 @@ 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
+
16
20
const log = debug ( 'eslint-plugin-import:ExportMap' )
17
21
18
22
const exportCache = new Map ( )
@@ -445,6 +449,21 @@ ExportMap.parse = function (path, content, context) {
445
449
446
450
const source = makeSourceCode ( content , ast )
447
451
452
+ function isEsModuleInterop ( ) {
453
+ const tsConfig = tsConfigLoader ( {
454
+ cwd : context . parserOptions && context . parserOptions . tsconfigRootDir || process . cwd ( ) ,
455
+ getEnv : ( key ) => process . env [ key ] ,
456
+ } )
457
+ try {
458
+ if ( tsConfig . tsConfigPath !== undefined ) {
459
+ const json = fs . readFileSync ( tsConfig . tsConfigPath )
460
+ return JSON . parse ( json ) . compilerOptions . esModuleInterop
461
+ }
462
+ } catch ( e ) {
463
+ return false
464
+ }
465
+ }
466
+
448
467
ast . body . forEach ( function ( n ) {
449
468
450
469
if ( n . type === 'ExportDefaultDeclaration' ) {
@@ -528,9 +547,14 @@ ExportMap.parse = function (path, content, context) {
528
547
} )
529
548
}
530
549
550
+ const isEsModuleInteropTrue = isEsModuleInterop ( )
551
+
552
+ const exports = [ 'TSExportAssignment' ]
553
+ isEsModuleInteropTrue && exports . push ( 'TSNamespaceExportDeclaration' )
554
+
531
555
// This doesn't declare anything, but changes what's being exported.
532
- if ( n . type === 'TSExportAssignment' ) {
533
- const exportedName = n . expression . name
556
+ if ( includes ( exports , n . type ) ) {
557
+ const exportedName = n . expression && n . expression . name || n . id . name
534
558
const declTypes = [
535
559
'VariableDeclaration' ,
536
560
'ClassDeclaration' ,
@@ -541,18 +565,18 @@ ExportMap.parse = function (path, content, context) {
541
565
'TSAbstractClassDeclaration' ,
542
566
'TSModuleDeclaration' ,
543
567
]
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
- )
568
+ const exportedDecls = ast . body . filter ( ( { type, id, declarations } ) => includes ( declTypes , type ) && (
569
+ ( id && id . name === exportedName ) ||
570
+ ( declarations && declarations . find ( d => d . id . name === exportedName ) )
571
+ ) )
551
572
if ( exportedDecls . length === 0 ) {
552
573
// Export is not referencing any local declaration, must be re-exporting
553
574
m . namespace . set ( 'default' , captureDoc ( source , docStyleParsers , n ) )
554
575
return
555
576
}
577
+ if ( isEsModuleInteropTrue ) {
578
+ m . namespace . set ( 'default' , { } )
579
+ }
556
580
exportedDecls . forEach ( ( decl ) => {
557
581
if ( decl . type === 'TSModuleDeclaration' ) {
558
582
if ( decl . body && decl . body . type === 'TSModuleDeclaration' ) {
0 commit comments