Skip to content

Commit ce540b6

Browse files
Maxim-Mazurokljharb
authored andcommittedAug 9, 2021
[Fix] ExportMap: Add default export when esModuleInterop is true and anything is exported
Fixes #2183. See #1689.
1 parent 202e5e0 commit ce540b6

File tree

5 files changed

+55
-6
lines changed

5 files changed

+55
-6
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
66

77
## [Unreleased]
88

9+
### Fixe
10+
- `ExportMap`: Add default export when esModuleInterop is true and anything is exported ([#2184], thanks [@Maxim-Mazurok])
11+
912
## [2.24.0] - 2021-08-08
1013

1114
### Added
@@ -822,6 +825,7 @@ for info on changes for earlier releases.
822825

823826
[`memo-parser`]: ./memo-parser/README.md
824827

828+
[#2184]: https://github.com/import-js/eslint-plugin-import/pull/2184
825829
[#2179]: https://github.com/import-js/eslint-plugin-import/pull/2179
826830
[#2160]: https://github.com/import-js/eslint-plugin-import/pull/2160
827831
[#2158]: https://github.com/import-js/eslint-plugin-import/pull/2158

‎src/ExportMap.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ ExportMap.for = function (context) {
351351

352352
ExportMap.parse = function (path, content, context) {
353353
const m = new ExportMap(path);
354+
const isEsModuleInteropTrue = isEsModuleInterop();
354355

355356
let ast;
356357
try {
@@ -576,8 +577,6 @@ ExportMap.parse = function (path, content, context) {
576577
});
577578
}
578579

579-
const isEsModuleInteropTrue = isEsModuleInterop();
580-
581580
const exports = ['TSExportAssignment'];
582581
if (isEsModuleInteropTrue) {
583582
exports.push('TSNamespaceExportDeclaration');
@@ -606,8 +605,11 @@ ExportMap.parse = function (path, content, context) {
606605
m.namespace.set('default', captureDoc(source, docStyleParsers, n));
607606
return;
608607
}
609-
if (isEsModuleInteropTrue) {
610-
m.namespace.set('default', {});
608+
if (
609+
isEsModuleInteropTrue // esModuleInterop is on in tsconfig
610+
&& !m.namespace.has('default') // and default isn't added already
611+
) {
612+
m.namespace.set('default', {}); // add default export
611613
}
612614
exportedDecls.forEach((decl) => {
613615
if (decl.type === 'TSModuleDeclaration') {
@@ -627,8 +629,8 @@ ExportMap.parse = function (path, content, context) {
627629
namespaceDecl.declarations.forEach((d) =>
628630
recursivePatternCapture(d.id, (id) => m.namespace.set(
629631
id.name,
630-
captureDoc(source, docStyleParsers, decl, namespaceDecl, moduleBlockNode)
631-
))
632+
captureDoc(source, docStyleParsers, decl, namespaceDecl, moduleBlockNode),
633+
)),
632634
);
633635
} else {
634636
m.namespace.set(
@@ -645,6 +647,14 @@ ExportMap.parse = function (path, content, context) {
645647
}
646648
});
647649

650+
if (
651+
isEsModuleInteropTrue // esModuleInterop is on in tsconfig
652+
&& m.namespace.size > 0 // anything is exported
653+
&& !m.namespace.has('default') // and default isn't added already
654+
) {
655+
m.namespace.set('default', {}); // add default export
656+
}
657+
648658
return m;
649659
};
650660

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// case from @types/react-test-renderer
2+
3+
export {};
4+
5+
export interface ReactTestRendererJSON {
6+
type: string;
7+
props: { [propName: string]: any };
8+
children: null | ReactTestRendererNode[];
9+
}
10+
export type ReactTestRendererNode = ReactTestRendererJSON | string;
11+
export interface ReactTestRendererTree extends ReactTestRendererJSON {
12+
nodeType: 'component' | 'host';
13+
instance: any;
14+
rendered: null | ReactTestRendererTree | ReactTestRendererTree[];
15+
}
16+
17+
export function create(nextElement: any, options?: any): any;
18+
19+
export function act(callback: () => Promise<any>): Promise<undefined>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"compilerOptions": {
3+
"esModuleInterop": true
4+
}
5+
}

‎tests/src/rules/default.js

+11
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,17 @@ context('TypeScript', function () {
220220
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-export-as-default-namespace/'),
221221
},
222222
}),
223+
test({
224+
code: `import Foo from "./typescript-export-react-test-renderer"`,
225+
parser: parser,
226+
settings: {
227+
'import/parsers': { [parser]: ['.ts'] },
228+
'import/resolver': { 'eslint-import-resolver-typescript': true },
229+
},
230+
parserOptions: {
231+
tsconfigRootDir: path.resolve(__dirname, '../../files/typescript-export-react-test-renderer/'),
232+
},
233+
}),
223234
test({
224235
code: `import foobar from "./typescript-export-assign-property"`,
225236
parser: parser,

0 commit comments

Comments
 (0)
Please sign in to comment.