@@ -23579,9 +23579,18 @@ const installImprove = (modules) => {
2357923579 compressor.option("reduce_vars") &&
2358023580 node.definition.assignments === 0 &&
2358123581 node.definition.patternWrites === 0 &&
23582- !reboundGlobals.has (node.name) &&
23582+ !isReboundGlobal (node.name) &&
2358323583 !globalMayBeRebound(compressor);
2358423584
23585+ /**
23586+ * Whether the program may assign the global through the global object, as
23587+ * `globalThis.Number = n` or `globalThis[key] = n`.
23588+ * @param {string} name a global's name
23589+ * @returns {boolean} true where it may
23590+ */
23591+ const isReboundGlobal = (name) =>
23592+ reboundGlobals.has(name) || reboundGlobals.has(ANY_GLOBAL);
23593+
2358523594 /**
2358623595 * The value an argument stands for: a literal, or an array literal of such
2358723596 * values without holes, or `NOT_CONSTANT`.
@@ -24399,6 +24408,7 @@ const installImprove = (modules) => {
2439924408 object.name === "globalThis" &&
2440024409 isUndeclaredRef(object) &&
2440124410 compressor.option("ecma") >= 2020 &&
24411+ !isReboundGlobal("globalThis") &&
2440224412 !globalMayBeRebound(compressor)
2440324413 ) {
2440424414 return true;
@@ -25348,23 +25358,56 @@ const installImprove = (modules) => {
2534825358 "self",
2534925359 "global"
2535025360 ]);
25361+ // Stands for every global where a write's key through the global object
25362+ // is not known.
25363+ const ANY_GLOBAL = "*";
25364+ /**
25365+ * The global a read names, `Array` or `globalThis.Array`, with
25366+ * `ANY_GLOBAL` for a key through the global object that is not known.
25367+ * @param {Node} node a read
25368+ * @returns {string | undefined} the global's name, if it names one
25369+ */
25370+ const globalNameOf = (node) => {
25371+ if (A.isSymbolRefNode(node)) return node.name;
25372+ if (
25373+ !A.isPropAccessNode(node) ||
25374+ node.optional ||
25375+ !A.isSymbolRefNode(node.object) ||
25376+ !GLOBAL_OBJECT_NAMES.has(node.object.name)
25377+ ) {
25378+ return undefined;
25379+ }
25380+ const name = A.isSubNode(node)
25381+ ? constantOf(node.property)
25382+ : A.terserProperty(node);
25383+ return typeof name === "string" ? name : ANY_GLOBAL;
25384+ };
2535125385 // `...[1]` and `"" + [1]` run what the program puts on `Array.prototype`.
2535225386 modules.webpackInline.arrayPrototypePatched = () =>
2535325387 improvements.enabled && patchedPrototypes.has("Array");
2535425388 /**
25355- * Whether `Array.prototype `, `Object.prototype ` or `String. prototype` is
25356- * written through where it stands, or handed to a call that may write it.
25389+ * The built-in, `Array`, `Object` or `String`, whose ` prototype` is written
25390+ * through where it stands, or handed to a call that may write it.
2535725391 * @param {Node} node a property read
2535825392 * @param {Walker} walker the walk holding its parents
25359- * @returns {boolean} true where it is
25393+ * @returns {string | undefined} its name, or `ANY_GLOBAL` where not known
2536025394 */
25361- const writesPrototype = (node, walker) =>
25362- !node.optional &&
25363- (A.isSubNode(node) ? constantOf(node.property) : A.terserProperty(node)) ===
25364- "prototype" &&
25365- A.isSymbolRefNode(node.object) &&
25366- BUILT_IN_PROTOTYPES_NAMES.has(node.object.name) &&
25367- writesThrough(node, walker);
25395+ const writtenPrototypeOf = (node, walker) => {
25396+ if (
25397+ node.optional ||
25398+ (A.isSubNode(node)
25399+ ? constantOf(node.property)
25400+ : A.terserProperty(node)) !== "prototype"
25401+ ) {
25402+ return undefined;
25403+ }
25404+ const name = globalNameOf(node.object);
25405+ return name !== undefined &&
25406+ (name === ANY_GLOBAL || BUILT_IN_PROTOTYPES_NAMES.has(name)) &&
25407+ writesThrough(node, walker)
25408+ ? name
25409+ : undefined;
25410+ };
2536825411 /**
2536925412 * Whether a property of the object is written or deleted where it stands,
2537025413 * or the object handed to a call that may write one.
@@ -25400,26 +25443,36 @@ const installImprove = (modules) => {
2540025443 let depth = 0;
2540125444 const walker = A.createWalker(
2540225445 (/** @type {Node} */ node, /** @type {() => void} */ descend) => {
25403- if (A.isPropAccessNode(node) && writesPrototype(node, walker)) {
25404- patchedPrototypes.add(node.object.name);
25405- } else if (
25406- A.isSymbolRefNode(node) &&
25407- pureGlobals.has(node.name) &&
25408- writesThrough(node, walker)
25409- ) {
25410- patchedStatics.add(node.name);
25446+ const prototypeOf = A.isPropAccessNode(node)
25447+ ? writtenPrototypeOf(node, walker)
25448+ : undefined;
25449+ if (prototypeOf !== undefined) {
25450+ for (const name of prototypeOf === ANY_GLOBAL
25451+ ? BUILT_IN_PROTOTYPES_NAMES
25452+ : [prototypeOf]) {
25453+ patchedPrototypes.add(name);
25454+ }
25455+ } else {
25456+ const name = globalNameOf(node);
25457+ if (
25458+ name !== undefined &&
25459+ (name === ANY_GLOBAL || pureGlobals.has(name)) &&
25460+ writesThrough(node, walker)
25461+ ) {
25462+ for (const global of name === ANY_GLOBAL ? pureGlobals : [name]) {
25463+ patchedStatics.add(global);
25464+ }
25465+ }
2541125466 }
2541225467 if (
2541325468 A.isSymbolRefNode(node) &&
2541425469 GLOBAL_OBJECT_NAMES.has(node.name) &&
2541525470 A.isPropAccessNode(walker.parent(0)) &&
2541625471 writesThrough(node, walker)
2541725472 ) {
25418- const access = walker.parent(0);
25419- const name = A.isSubNode(access)
25420- ? constantOf(access.property)
25421- : A.terserProperty(access);
25422- if (typeof name === "string") reboundGlobals.add(name);
25473+ reboundGlobals.add(
25474+ /** @type {string} */ (globalNameOf(walker.parent(0)))
25475+ );
2542325476 }
2542425477 if (depth > 0 && A.isSymbolRefNode(node)) {
2542525478 probedReads.add(node);
@@ -25531,6 +25584,7 @@ const installImprove = (modules) => {
2553125584 !A.isSymbolRefNode(global) ||
2553225585 // The writes are counted only while `reduce_vars` reads the tree.
2553325586 !compressor.option("reduce_vars") ||
25587+ isReboundGlobal(global.name) ||
2553425588 globalMayBeRebound(compressor) ||
2553525589 probesInTry(global, compressor)
2553625590 ) {
@@ -28321,6 +28375,7 @@ const installImprove = (modules) => {
2832128375 inner.arguments.length !== 1 ||
2832228376 A.isExpansionNode(inner.arguments[0]) ||
2832328377 !isUndeclaredRef(inner.callee) ||
28378+ isReboundGlobal("Object") ||
2832428379 globalMayBeRebound(compressor)
2832528380 ) {
2832628381 return undefined;
@@ -29693,6 +29748,7 @@ const installImprove = (modules) => {
2969329748 args.length > 1 ||
2969429749 (args.length === 1 && A.isExpansionNode(args[0])) ||
2969529750 !isUndeclaredRef(callee) ||
29751+ isReboundGlobal(callee.name) ||
2969629752 globalMayBeRebound(compressor)
2969729753 ) {
2969829754 return undefined;
0 commit comments