bublejs / buble

https://buble.surge.sh
MIT License
871 stars 64 forks source link

Loops create redundant "arguments" variables #208

Open wojdyr opened 5 years ago

wojdyr commented 5 years ago

Like #120, but for "arguments" instead of "this". Let me use the same example:

[Minimal example](https://buble.surge.sh/#function%20bar()%20%7B%0A%20%20while%20(true)%20arguments%3B%0A%7D):

function bar() {
  while (true) arguments;
}

Produces:

function bar() {
  var arguments$1 = arguments;

  while (true) { arguments$1; }
}

Expected output:

function bar() {
  while (true) { arguments; }
}

120 was fixed in #121, but I don't feel confident enough to propose a PR myself.

bhousel commented 4 years ago

Whoa, I just came across this bug today.. It's a bit worse than reported, because buble is trying to hoist arguments as if it were a normal variable. Check it out:

[example link](https://buble.surge.sh/#function%20myModule()%20%7B%0A%20%20myModule.hello%20%3D%20()%20%3D%3E%20arguments.length%3B%0A%20%20myModule.goodbye%20%3D%20()%20%3D%3E%20arguments.length%3B%0A%20%20%0A%20%20return%20myModule%3B%0A%7D)

source:

function myModule() {
  myModule.hello = () => arguments.length;
  myModule.goodbye = () => arguments.length;

  return myModule;
}

produces:

function myModule() {
  var arguments$1 = arguments;

  myModule.hello = function () { return arguments$1.length; };
  myModule.goodbye = function () { return arguments$1.length; };

  return myModule;
}

Those really need to stay as arguments and work locally for the methods.