Skip to content

Commit b77cb2a

Browse files
authored
Fix commonjs export= merging (#27368)
I'm surprised we haven't seen more of this; I suspect it's because the mixed `module.exports=` + `export.foo=` pattern isn't that common. However, it'll happen any time that the exported symbol is unknown; getCommonJsExportEquals blithely clones unknownSymbol and proceeds to stick the `exports.foo=` properties onto it. This causes problems later, because the compiler checks for unknownSymbol with `===`. The fix is to not stick properties onto a clone of unknownSymbol. This makes the correct errors appear and removes the crash.
1 parent f4a643f commit b77cb2a

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2340,7 +2340,7 @@ namespace ts {
23402340
}
23412341

23422342
function getCommonJsExportEquals(exported: Symbol | undefined, moduleSymbol: Symbol): Symbol | undefined {
2343-
if (!exported || moduleSymbol.exports!.size === 1) {
2343+
if (!exported || exported === unknownSymbol || moduleSymbol.exports!.size === 1) {
23442344
return exported;
23452345
}
23462346
const merged = cloneSymbol(exported);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/salsa/bug27025.js(1,25): error TS2339: Property 'nonprop' does not exist on type 'Window'.
2+
tests/cases/conformance/salsa/bug27025.js(2,15): error TS2304: Cannot find name 'bar'.
3+
4+
5+
==== tests/cases/conformance/salsa/bug27025.js (2 errors) ====
6+
module.exports = window.nonprop;
7+
~~~~~~~
8+
!!! error TS2339: Property 'nonprop' does not exist on type 'Window'.
9+
exports.foo = bar;
10+
~~~
11+
!!! error TS2304: Cannot find name 'bar'.
12+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/salsa/bug27025.js ===
2+
module.exports = window.nonprop;
3+
>module.exports : Symbol("tests/cases/conformance/salsa/bug27025", Decl(bug27025.js, 0, 0))
4+
>module : Symbol(export=, Decl(bug27025.js, 0, 0))
5+
>exports : Symbol(export=, Decl(bug27025.js, 0, 0))
6+
>window : Symbol(window, Decl(lib.dom.d.ts, --, --))
7+
8+
exports.foo = bar;
9+
>exports : Symbol(foo, Decl(bug27025.js, 0, 32))
10+
>foo : Symbol(foo, Decl(bug27025.js, 0, 32))
11+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/conformance/salsa/bug27025.js ===
2+
module.exports = window.nonprop;
3+
>module.exports = window.nonprop : any
4+
>module.exports : any
5+
>module : { "tests/cases/conformance/salsa/bug27025": any; }
6+
>exports : any
7+
>window.nonprop : any
8+
>window : Window
9+
>nonprop : any
10+
11+
exports.foo = bar;
12+
>exports.foo = bar : any
13+
>exports.foo : any
14+
>exports : any
15+
>foo : any
16+
>bar : any
17+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
// @checkJs: true
4+
// @Filename: bug27025.js
5+
module.exports = window.nonprop;
6+
exports.foo = bar;

0 commit comments

Comments
 (0)