File tree 2 files changed +24
-8
lines changed
2 files changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -35,24 +35,30 @@ export function interopDefault(sourceModule: any): any {
35
35
if ( ! isObject ( sourceModule ) || ! ( "default" in sourceModule ) ) {
36
36
return sourceModule ;
37
37
}
38
- const newModule = sourceModule . default ;
38
+ const defaultValue = sourceModule . default ;
39
+ if ( defaultValue === undefined || defaultValue === null ) {
40
+ return sourceModule ;
41
+ }
42
+ if ( typeof defaultValue !== "object" ) {
43
+ return defaultValue ;
44
+ }
39
45
for ( const key in sourceModule ) {
40
46
if ( key === "default" ) {
41
47
try {
42
- if ( ! ( key in newModule ) ) {
43
- Object . defineProperty ( newModule , key , {
48
+ if ( ! ( key in defaultValue ) ) {
49
+ Object . defineProperty ( defaultValue , key , {
44
50
enumerable : false ,
45
51
configurable : false ,
46
52
get ( ) {
47
- return newModule ;
53
+ return defaultValue ;
48
54
} ,
49
55
} ) ;
50
56
}
51
57
} catch { }
52
58
} else {
53
59
try {
54
- if ( ! ( key in newModule ) ) {
55
- Object . defineProperty ( newModule , key , {
60
+ if ( ! ( key in defaultValue ) ) {
61
+ Object . defineProperty ( defaultValue , key , {
56
62
enumerable : true ,
57
63
configurable : true ,
58
64
get ( ) {
@@ -63,5 +69,5 @@ export function interopDefault(sourceModule: any): any {
63
69
} catch { }
64
70
}
65
71
}
66
- return newModule ;
72
+ return defaultValue ;
67
73
}
Original file line number Diff line number Diff line change @@ -16,6 +16,14 @@ const tests = [
16
16
{ named : 1 , default : { x : 2 } } ,
17
17
{ named : 1 , x : 2 } ,
18
18
] ,
19
+ [
20
+ { default : null , x : 1 } , // eslint-disable-line unicorn/no-null
21
+ { default : null , x : 1 } , // eslint-disable-line unicorn/no-null
22
+ ] ,
23
+ [
24
+ { default : undefined , x : 1 } ,
25
+ { default : undefined , x : 1 } ,
26
+ ] ,
19
27
] ;
20
28
21
29
describe ( "interopDefault" , ( ) => {
@@ -24,7 +32,9 @@ describe("interopDefault", () => {
24
32
const interop = interopDefault ( input ) ;
25
33
expect ( interop ) . to . deep . equal ( result ) ;
26
34
if ( typeof input === "object" && "default" in input ) {
27
- expect ( interop . default ) . to . deep . equal ( result ) ;
35
+ expect ( interop . default ) . to . deep . equal (
36
+ "default" in ( result as any ) ? ( result as any ) . default : result ,
37
+ ) ;
28
38
} else {
29
39
expect ( interop ) . to . deep . equal ( result ) ;
30
40
}
You can’t perform that action at this time.
0 commit comments