Skip to content

Commit 89caf30

Browse files
sandersnsnovader
authored andcommitted
Fix CJS export of typedef and class w/latebound names (microsoft#55053)
1 parent 1eea215 commit 89caf30

File tree

4 files changed

+149
-1
lines changed

4 files changed

+149
-1
lines changed

src/compiler/checker.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12822,7 +12822,24 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1282212822
}
1282312823
}
1282412824

12825-
links[resolutionKind] = combineSymbolTables(earlySymbols, lateSymbols) || emptySymbols;
12825+
let resolved = combineSymbolTables(earlySymbols, lateSymbols);
12826+
if (symbol.flags & SymbolFlags.Transient && links.cjsExportMerged && symbol.declarations) {
12827+
for (const decl of symbol.declarations) {
12828+
const original = getSymbolLinks(decl.symbol)[resolutionKind];
12829+
if (!resolved) {
12830+
resolved = original;
12831+
continue;
12832+
}
12833+
if (!original) continue;
12834+
original.forEach((s, name) => {
12835+
const existing = resolved!.get(name);
12836+
if (!existing) resolved!.set(name, s);
12837+
else if (existing === s) return;
12838+
else resolved!.set(name, mergeSymbol(existing, s));
12839+
});
12840+
}
12841+
}
12842+
links[resolutionKind] = resolved || emptySymbols;
1282612843
}
1282712844

1282812845
return links[resolutionKind]!;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypedefAndLatebound.ts] ////
2+
3+
=== index.js ===
4+
// from #53967, based on webpack/webpack#16957
5+
6+
const LazySet = require("./LazySet");
7+
>LazySet : Symbol(LazySet, Decl(index.js, 2, 5))
8+
>require : Symbol(require)
9+
>"./LazySet" : Symbol("LazySet", Decl(LazySet.js, 0, 0))
10+
11+
/** @type {LazySet} */
12+
const stringSet = undefined;
13+
>stringSet : Symbol(stringSet, Decl(index.js, 5, 5))
14+
>undefined : Symbol(undefined)
15+
16+
stringSet.addAll(stringSet);
17+
>stringSet.addAll : Symbol(LazySet.addAll, Decl(LazySet.js, 4, 15))
18+
>stringSet : Symbol(stringSet, Decl(index.js, 5, 5))
19+
>addAll : Symbol(LazySet.addAll, Decl(LazySet.js, 4, 15))
20+
>stringSet : Symbol(stringSet, Decl(index.js, 5, 5))
21+
22+
23+
=== LazySet.js ===
24+
// Comment out this JSDoc, and note that the errors index.js go away.
25+
/**
26+
* @typedef {Object} SomeObject
27+
*/
28+
class LazySet {
29+
>LazySet : Symbol(LazySet, Decl(LazySet.js, 0, 0))
30+
31+
/**
32+
* @param {LazySet} iterable
33+
*/
34+
addAll(iterable) {}
35+
>addAll : Symbol(LazySet.addAll, Decl(LazySet.js, 4, 15))
36+
>iterable : Symbol(iterable, Decl(LazySet.js, 8, 11))
37+
38+
[Symbol.iterator]() {}
39+
>[Symbol.iterator] : Symbol(LazySet[Symbol.iterator], Decl(LazySet.js, 8, 23))
40+
>Symbol.iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
41+
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
42+
>iterator : Symbol(SymbolConstructor.iterator, Decl(lib.es2015.iterable.d.ts, --, --))
43+
}
44+
45+
module.exports = LazySet;
46+
>module.exports : Symbol(module.exports, Decl(LazySet.js, 0, 0))
47+
>module : Symbol(export=, Decl(LazySet.js, 10, 1))
48+
>exports : Symbol(export=, Decl(LazySet.js, 10, 1))
49+
>LazySet : Symbol(LazySet, Decl(LazySet.js, 0, 0))
50+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//// [tests/cases/conformance/jsdoc/declarations/jsDeclarationsTypedefAndLatebound.ts] ////
2+
3+
=== index.js ===
4+
// from #53967, based on webpack/webpack#16957
5+
6+
const LazySet = require("./LazySet");
7+
>LazySet : typeof LazySet
8+
>require("./LazySet") : typeof LazySet
9+
>require : any
10+
>"./LazySet" : "./LazySet"
11+
12+
/** @type {LazySet} */
13+
const stringSet = undefined;
14+
>stringSet : LazySet
15+
>undefined : undefined
16+
17+
stringSet.addAll(stringSet);
18+
>stringSet.addAll(stringSet) : void
19+
>stringSet.addAll : (iterable: LazySet) => void
20+
>stringSet : LazySet
21+
>addAll : (iterable: LazySet) => void
22+
>stringSet : LazySet
23+
24+
25+
=== LazySet.js ===
26+
// Comment out this JSDoc, and note that the errors index.js go away.
27+
/**
28+
* @typedef {Object} SomeObject
29+
*/
30+
class LazySet {
31+
>LazySet : LazySet
32+
33+
/**
34+
* @param {LazySet} iterable
35+
*/
36+
addAll(iterable) {}
37+
>addAll : (iterable: LazySet) => void
38+
>iterable : import("LazySet")
39+
40+
[Symbol.iterator]() {}
41+
>[Symbol.iterator] : () => void
42+
>Symbol.iterator : unique symbol
43+
>Symbol : SymbolConstructor
44+
>iterator : unique symbol
45+
}
46+
47+
module.exports = LazySet;
48+
>module.exports = LazySet : typeof LazySet
49+
>module.exports : typeof LazySet
50+
>module : { exports: typeof LazySet; }
51+
>exports : typeof LazySet
52+
>LazySet : typeof LazySet
53+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// @lib: es2017
2+
// @allowJs: true
3+
// @checkJs: true
4+
// @noEmit: true
5+
// from #53967, based on webpack/webpack#16957
6+
7+
// @filename: index.js
8+
const LazySet = require("./LazySet");
9+
10+
/** @type {LazySet} */
11+
const stringSet = undefined;
12+
stringSet.addAll(stringSet);
13+
14+
15+
// @filename: LazySet.js
16+
// Comment out this JSDoc, and note that the errors index.js go away.
17+
/**
18+
* @typedef {Object} SomeObject
19+
*/
20+
class LazySet {
21+
/**
22+
* @param {LazySet} iterable
23+
*/
24+
addAll(iterable) {}
25+
[Symbol.iterator]() {}
26+
}
27+
28+
module.exports = LazySet;

0 commit comments

Comments
 (0)