A conditional whole export keeps the members attached to its value (#4514)

* A conditional whole export keeps the members attached to its value

- A conditional whole export keeps the members attached to its value.

* Format the conditional whole-export detector change
This commit is contained in:
Chris Tate
2026-11-29 09:18:26 +00:00
committed by GitHub
parent df3b773497
commit b57c0c39ae
16 changed files with 69 additions and 27 deletions
@@ -647,14 +647,22 @@ export function cjsRuntimeWholeExportValueWritesOf(
if (!ts.isBinaryExpression(node)) return undefined;
// A member export, an `exports` rebinding, or a member write through
// `module.exports` needs the exports object itself; leave those shapes
// to the fences they already have. Comparisons read the objects and do
// not change the export, so they stay.
// to the fences they already have. The one exception is a plain member
// write through `module.exports` in a module whose whole export a
// run-time write chooses (a UMD function export with members attached,
// `module.exports.unload = unload`): the slot holds the value the
// whole-export write stored, so the member write lands on that value's
// own property. Comparisons read the objects and do not change the
// export, so they stay.
const operator = node.operatorToken.kind;
const assigns =
operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment;
const memberThroughModuleExports =
ts.isPropertyAccessExpression(node.left) && isCjsModuleExports(lowerer, node.left.expression);
if (
assigns &&
!isModuleExportsAccess(node.left) &&
!(memberThroughModuleExports && operator === ts.SyntaxKind.EqualsToken) &&
(isCjsExportsIdentifier(lowerer, node.left) ||
(ts.isPropertyAccessExpression(node.left) &&
(isCjsModuleExports(lowerer, node.left.expression) ||
@@ -0,0 +1,8 @@
{
"order": [
"<repo>/tests/corpus/cjs-conditional-whole-export-members/exit.cjs",
"<repo>/tests/corpus/cjs-conditional-whole-export-members/settings.cjs",
"<repo>/tests/corpus/cjs-conditional-whole-export-members/main.cjs"
],
"diags": []
}
@@ -0,0 +1,6 @@
{
"order": [
"<repo>/tests/corpus/cjs-umd-export-members/main.ts"
],
"diags": []
}
@@ -1,6 +0,0 @@
{
"order": [
"<repo>/tests/diagnostics/umd-export-forced-native/main.ts"
],
"diags": []
}
@@ -0,0 +1,17 @@
var verbose = true;
if (verbose) {
module.exports = function (label) {
return "exit:" + label;
};
module.exports.unload = function () {
return "unload";
};
module.exports.signals = function () {
return ["SIGINT", "SIGTERM"];
};
} else {
module.exports = function () {
return "noop";
};
}
@@ -0,0 +1,9 @@
// A module whose whole export a condition chooses, with members attached to
// the value that branch stored. Node's require answers that value, with the
// members the branch attached; a member read through the require result and
// a member the module itself reads back both see them.
const exit = require("./exit.cjs");
console.log(exit("ready"), exit.unload(), exit.signals().join(","));
const settings = require("./settings.cjs");
console.log(settings.format, settings.read(), settings.constructor === Object);
@@ -0,0 +1,10 @@
var useJson = true;
if (useJson) {
module.exports = { format: "json" };
module.exports.read = function () {
return "read:" + module.exports.format;
};
} else {
module.exports = { format: "text" };
}
@@ -1,6 +1,7 @@
// A package written as the classic UMD wrapper assigns module.exports inside
// the wrapper function. Marked native, its export has no static form: the use
// of the default import names that cause and the remedy.
// the wrapper function, then attaches a member to the value it stored. The
// run-time whole-export slot holds what the CommonJS arm chose, so both the
// default import and the member it attached answer Node's values.
import wordform from "wordform";
console.log(wordform("item", 2), wordform.singular("items"));
@@ -1,17 +0,0 @@
umd-export-forced-native/main.ts:6:13 - error SC1090: the value of 'wordform' (its module assigns module.exports inside a function or under a condition, at index.js:6, so what it exports is decided when it runs) is not supported yet
5 |
6 | console.log(wordform("item", 2), wordform.singular("items"));
| ^~~~~~~~
7 | const held = wordform;
hint: run the package in a Node island: --node-island=wordform, or leave the choice to the build with --node-islands=auto
umd-export-forced-native/main.ts:7:14 - error SC1090: the value of 'wordform' (its module assigns module.exports inside a function or under a condition, at index.js:6, so what it exports is decided when it runs) is not supported yet
6 | console.log(wordform("item", 2), wordform.singular("items"));
7 | const held = wordform;
| ^~~~~~~~
8 | console.log(typeof held);
hint: run the package in a Node island: --node-island=wordform, or leave the choice to the build with --node-islands=auto
@@ -0,0 +1 @@
{ "plain": 3500, "san": 4500 }
@@ -0,0 +1 @@
{ "plain": 3000, "san": 4000 }
+4
View File
@@ -218,6 +218,10 @@ describe.skipIf(!zigOnPath() || !wasmPackInstalled())("wasm32-wasi differential"
"buffer-codec-boundaries.ts",
"text-encode-into-boundaries.ts",
"object-key-indexes.js",
// A UMD wrapper's function export with a member attached.
"cjs-umd-export-members/main.ts",
// A conditional whole export with members attached to the chosen value.
"cjs-conditional-whole-export-members/main.cjs",
// Error.prepareStackTrace program state.
"error-prepare-stack-trace-state.js",
// A private name never answers through a string key.