Skip to content

Commit d572dcb

Browse files
authored
Fix crash intersecting dynamic import w/esModuleInterop (microsoft#40249)
* Fix crash intersecting dynamic import w/esModuleInterop The dynamic import shim creates a symbol without some properties that the intersection-creating code assumes are present as of microsoft#38673. This PR adds the smallest possible set of properties to avoid the crash. I'm not sure what others would be good to add. * Use symbol's declarations instead * Fix getResolvedMembersOrExportsOfSymbol instead * comment from code review
1 parent 378083f commit d572dcb

File tree

5 files changed

+76
-1
lines changed

5 files changed

+76
-1
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9714,7 +9714,7 @@ namespace ts {
97149714

97159715
// fill in any as-yet-unresolved late-bound members.
97169716
const lateSymbols = createSymbolTable() as UnderscoreEscapedMap<TransientSymbol>;
9717-
for (const decl of symbol.declarations) {
9717+
for (const decl of symbol.declarations || emptyArray) {
97189718
const members = getMembersOfDeclaration(decl);
97199719
if (members) {
97209720
for (const member of members) {

tests/baselines/reference/intersectionsAndEmptyObjects.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//// [tests/cases/conformance/types/intersection/intersectionsAndEmptyObjects.ts] ////
2+
13
//// [intersectionsAndEmptyObjects.ts]
24
// Empty object type literals are removed from intersections types
35
// that contain other object types
@@ -80,11 +82,38 @@ var unknownChoicesAndEmpty: choices<IUnknownChoiceList & {}>;
8082

8183
type Foo1 = { x: string } & { [x: number]: Foo1 };
8284
type Foo2 = { x: string } & { [K in number]: Foo2 };
85+
86+
// Repro from #40239
87+
88+
declare function mock<M>(_: Promise<M>): {} & M;
89+
mock(import('./ex'))
90+
91+
//// [ex.d.ts]
92+
export {}
8393

8494

8595
//// [intersectionsAndEmptyObjects.js]
8696
// Empty object type literals are removed from intersections types
8797
// that contain other object types
98+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
99+
if (k2 === undefined) k2 = k;
100+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
101+
}) : (function(o, m, k, k2) {
102+
if (k2 === undefined) k2 = k;
103+
o[k2] = m[k];
104+
}));
105+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
106+
Object.defineProperty(o, "default", { enumerable: true, value: v });
107+
}) : function(o, v) {
108+
o["default"] = v;
109+
});
110+
var __importStar = (this && this.__importStar) || function (mod) {
111+
if (mod && mod.__esModule) return mod;
112+
var result = {};
113+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
114+
__setModuleDefault(result, mod);
115+
return result;
116+
};
88117
let x01;
89118
let x02;
90119
let x03;
@@ -121,3 +150,4 @@ var myChoices;
121150
var myChoicesAndEmpty;
122151
var unknownChoices;
123152
var unknownChoicesAndEmpty;
153+
mock(Promise.resolve().then(() => __importStar(require('./ex'))));

tests/baselines/reference/intersectionsAndEmptyObjects.symbols

+18
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,21 @@ type Foo2 = { x: string } & { [K in number]: Foo2 };
260260
>K : Symbol(K, Decl(intersectionsAndEmptyObjects.ts, 80, 31))
261261
>Foo2 : Symbol(Foo2, Decl(intersectionsAndEmptyObjects.ts, 79, 50))
262262

263+
// Repro from #40239
264+
265+
declare function mock<M>(_: Promise<M>): {} & M;
266+
>mock : Symbol(mock, Decl(intersectionsAndEmptyObjects.ts, 80, 52))
267+
>M : Symbol(M, Decl(intersectionsAndEmptyObjects.ts, 84, 22))
268+
>_ : Symbol(_, Decl(intersectionsAndEmptyObjects.ts, 84, 25))
269+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
270+
>M : Symbol(M, Decl(intersectionsAndEmptyObjects.ts, 84, 22))
271+
>M : Symbol(M, Decl(intersectionsAndEmptyObjects.ts, 84, 22))
272+
273+
mock(import('./ex'))
274+
>mock : Symbol(mock, Decl(intersectionsAndEmptyObjects.ts, 80, 52))
275+
>'./ex' : Symbol("tests/cases/conformance/types/intersection/ex", Decl(ex.d.ts, 0, 0))
276+
277+
=== tests/cases/conformance/types/intersection/ex.d.ts ===
278+
export {}
279+
No type information for this code.
280+
No type information for this code.

tests/baselines/reference/intersectionsAndEmptyObjects.types

+16
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,19 @@ type Foo2 = { x: string } & { [K in number]: Foo2 };
217217
>Foo2 : Foo2
218218
>x : string
219219

220+
// Repro from #40239
221+
222+
declare function mock<M>(_: Promise<M>): {} & M;
223+
>mock : <M>(_: Promise<M>) => {} & M
224+
>_ : Promise<M>
225+
226+
mock(import('./ex'))
227+
>mock(import('./ex')) : {}
228+
>mock : <M>(_: Promise<M>) => {} & M
229+
>import('./ex') : Promise<{ default: typeof import("tests/cases/conformance/types/intersection/ex"); }>
230+
>'./ex' : "./ex"
231+
232+
=== tests/cases/conformance/types/intersection/ex.d.ts ===
233+
export {}
234+
No type information for this code.
235+
No type information for this code.

tests/cases/conformance/types/intersection/intersectionsAndEmptyObjects.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// @target: es2015
2+
// @module: commonjs
3+
// @esModuleInterop: true
4+
// @filename: intersectionsAndEmptyObjects.ts
25

36
// Empty object type literals are removed from intersections types
47
// that contain other object types
@@ -81,3 +84,11 @@ var unknownChoicesAndEmpty: choices<IUnknownChoiceList & {}>;
8184

8285
type Foo1 = { x: string } & { [x: number]: Foo1 };
8386
type Foo2 = { x: string } & { [K in number]: Foo2 };
87+
88+
// Repro from #40239
89+
90+
declare function mock<M>(_: Promise<M>): {} & M;
91+
mock(import('./ex'))
92+
93+
// @filename: ex.d.ts
94+
export {}

0 commit comments

Comments
 (0)