tree-shaker icon indicating copy to clipboard operation
tree-shaker copied to clipboard

Abnormal phenomenon

Open XiSenao opened this issue 1 year ago • 1 comments

Input:

function Main() {
  const p = {};
  p.__proto__ = p.__proto__ || {};
  p.__proto__.a = 124;
  const m = Math.random(10);
  return m;
}


Main();

console.log(window.a);

Expected Output:

Include all statements.

Actual Output:

It seems that objects created by literals do not have a prototype chain, and references to global objects are automatically retained.

function Main() {
  const __unused_4577 = Math.random(10);
  return;
}
Main();
console.log(window.a);

XiSenao avatar Nov 29 '24 06:11 XiSenao

There are two separate problems:

  1. Math.random() not considered as pure yet. This will be solved in the future.
  2. Prototype pollution. IMO, the expected behavior should be:
function effect1(key, value) {
  ({})[key]?.x = value
}

({}).__proto__.x     // Preserved
effect("__proto__")  // Preserved
effect("" + unknown) // Although `unknown` is possible to be "__proto__", still remove it

kermanx avatar Nov 30 '24 04:11 kermanx