Skip to content

Commit e769725

Browse files
authored
Fxied declaration emit for JS files when module.exports is assigned a non-alias value and when it has extra type members (#56243)
1 parent 7398b8e commit e769725

5 files changed

+102
-3
lines changed

src/compiler/checker.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -8758,7 +8758,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
87588758
});
87598759
let addingDeclare = !bundled;
87608760
const exportEquals = symbolTable.get(InternalSymbolName.ExportEquals);
8761-
if (exportEquals && symbolTable.size > 1 && exportEquals.flags & SymbolFlags.Alias) {
8761+
if (exportEquals && symbolTable.size > 1 && exportEquals.flags & (SymbolFlags.Alias | SymbolFlags.Module)) {
87628762
symbolTable = createSymbolTable();
87638763
// Remove extraneous elements from root symbol table (they'll be mixed back in when the target of the `export=` is looked up)
87648764
symbolTable.set(InternalSymbolName.ExportEquals, exportEquals);
@@ -9291,8 +9291,18 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
92919291
}
92929292

92939293
function getNamespaceMembersForSerialization(symbol: Symbol) {
9294-
const exports = getExportsOfSymbol(symbol);
9295-
return !exports ? [] : filter(arrayFrom(exports.values()), m => isNamespaceMember(m) && isIdentifierText(m.escapedName as string, ScriptTarget.ESNext));
9294+
let exports = arrayFrom(getExportsOfSymbol(symbol).values());
9295+
const merged = getMergedSymbol(symbol);
9296+
if (merged !== symbol) {
9297+
const membersSet = new Set(exports);
9298+
for (const exported of getExportsOfSymbol(merged).values()) {
9299+
if (!(getSymbolFlags(resolveSymbol(exported)) & SymbolFlags.Value)) {
9300+
membersSet.add(exported);
9301+
}
9302+
}
9303+
exports = arrayFrom(membersSet);
9304+
}
9305+
return filter(exports, m => isNamespaceMember(m) && isIdentifierText(m.escapedName as string, ScriptTarget.ESNext));
92969306
}
92979307

92989308
function isTypeOnlyNamespace(symbol: Symbol) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitExportAssignedFunctionWithExtraTypedefsMembers.ts] ////
2+
3+
//// [index.js]
4+
/**
5+
* @typedef Options
6+
* @property {string} opt
7+
*/
8+
9+
/**
10+
* @param {Options} options
11+
*/
12+
module.exports = function loader(options) {}
13+
14+
15+
//// [index.js]
16+
"use strict";
17+
/**
18+
* @typedef Options
19+
* @property {string} opt
20+
*/
21+
/**
22+
* @param {Options} options
23+
*/
24+
module.exports = function loader(options) { };
25+
26+
27+
//// [index.d.ts]
28+
declare namespace _exports {
29+
export { Options };
30+
}
31+
declare function _exports(options: Options): void;
32+
export = _exports;
33+
type Options = {
34+
opt: string;
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitExportAssignedFunctionWithExtraTypedefsMembers.ts] ////
2+
3+
=== index.js ===
4+
/**
5+
* @typedef Options
6+
* @property {string} opt
7+
*/
8+
9+
/**
10+
* @param {Options} options
11+
*/
12+
module.exports = function loader(options) {}
13+
>module.exports : Symbol(module.exports, Decl(index.js, 0, 0))
14+
>module : Symbol(export=, Decl(index.js, 0, 0))
15+
>exports : Symbol(export=, Decl(index.js, 0, 0))
16+
>loader : Symbol(loader, Decl(index.js, 8, 16))
17+
>options : Symbol(options, Decl(index.js, 8, 33))
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//// [tests/cases/compiler/jsDeclarationEmitExportAssignedFunctionWithExtraTypedefsMembers.ts] ////
2+
3+
=== index.js ===
4+
/**
5+
* @typedef Options
6+
* @property {string} opt
7+
*/
8+
9+
/**
10+
* @param {Options} options
11+
*/
12+
module.exports = function loader(options) {}
13+
>module.exports = function loader(options) {} : (options: Options) => void
14+
>module.exports : (options: Options) => void
15+
>module : { exports: (options: Options) => void; }
16+
>exports : (options: Options) => void
17+
>function loader(options) {} : (options: Options) => void
18+
>loader : (options: Options) => void
19+
>options : Options
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// @strict: true
2+
// @checkJs: true
3+
// @declaration: true
4+
// @outDir: out
5+
6+
// @filename: index.js
7+
8+
/**
9+
* @typedef Options
10+
* @property {string} opt
11+
*/
12+
13+
/**
14+
* @param {Options} options
15+
*/
16+
module.exports = function loader(options) {}

0 commit comments

Comments
 (0)