Polymer / tools

Polymer Tools Monorepo
BSD 3-Clause "New" or "Revised" License
430 stars 200 forks source link

Polymer js minifier reusing catch error variable inside catch block #2588

Open nachoab opened 6 years ago

nachoab commented 6 years ago

Description

This is an error of the js minifier. When the error variable is not used inside the catch statement, it is renaming it to "a", but if a variable is created inside the catch block it is also using "a". In case of const variables this throws an error because "a" has already been declared.

Versions & Environment

Steps to Reproduce

Minify this code:

try {
    // something
 } catch(e) {
    const date = new Date();
    console.log(date);
}

Expected Results

try {
    // something
} catch (a) {
    const e = new Date();
    console.log(e);
}

Actual Results

try {
    // something
} catch (a) {
    const a = new Date();
    console.log(a);
}

If the error variable is used, then it is correctly minified.

nachoab commented 6 years ago

Another uglify error.

Here "bar" can be ignored because it is never used:

async function example() {
  var foo, bar;
  try {
    [foo, bar] = await Promise.all([req(1), req(2)]);
  } catch(e) {}
  console.log(foo);
}

but the minifier ignores "foo" too:

async function example() {
  var a;
  try {
      await Promise.all([req(1), req(2)])
  } catch (a) {}
  console.log(a)
}

If both "foo" and "bar" were used, the minifier does its job well:

async function example() {
  var foo, bar;
  try {
    [foo, bar] = await Promise.all([req(1), req(2)]);
  } catch(e) {}
  console.log(foo, bar);
}

which is minified to:

async function example() {
  var a, b;
  try {
      [a, b] = await Promise.all([req(1), req(2)])
  } catch (a) {}
  console.log(a, b)
}
nachoab commented 6 years ago

Here goes another one:

Input:

function example(obj) {
  if (!!obj.foo || obj.bar) {
    return true;
  }
  return false;
}

Actual Result, which is not the same, it doesn't return a boolean:

function example(a){
  return!!a.foo||a.bar
}

Expected Result, for instance:

function example(a){
  return!!(!!a.foo||a.bar)
}
justinfagnani commented 6 years ago

@nachoab the current pre-release of the CLI includes some updated Babel packages, that may or may not effect this. Could you try npm i -g polymer@next and see if this still happens?

For your first example at least, it seems like the version of Babel minify that the Babel REPL uses isn't exhibiting the bug: https://babeljs.io/repl/#?babili=true&browsers=&build=&builtIns=false&code_lz=C4JwngBA3gUB8QPSIgZwPYFsCmwAWAlgHYDmcAvhAMYCGwVeAFNgJTRwJXpGrAQAmdbBAC8EItgDuEACJDGLANwd4XHugA22AHQb0JRoOCtl5IA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&lineWrap=false&presets=babili&prettier=false&targets=&version=6.26.0&envVersion=

justinfagnani commented 6 years ago

The last one I think we'll need to file a Babel bug for: https://babeljs.io/repl/#?babili=true&browsers=&build=&builtIns=false&code_lz=GYVwdgxgLglg9mABAUwB4EMC2AHANsgCjgCMArASkQG8AoRRGYRAgQhZNIDpg45EAffog6di6AE6Va9euORQQ4pFHEhkAbjqIAvlrkKliYOlwBnDTW1A&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&lineWrap=false&presets=babili&prettier=false&targets=&version=6.26.0&envVersion=

nachoab commented 6 years ago

The last one is fixed too in the polymer@next version:

function example(a){return!(!a.foo&&!a.bar)}

The second one is still failing: https://babeljs.io/repl/#?babili=true&browsers=&build=&builtIns=false&code_lz=IYZwngdgxgBAZgV2gFwJYHsIwKYA9gC2ADgDbYAUAlDAN4BQMMAbsAE7zroA0MARmwG4GMZKzC1hjANpxOPfqwC6MALwxgAd2CpkMAAqt0BVCGwA6YCRLkprbAEdyARko87jgEyVFlIYwC-MFDAyFAAFuTY1DT-wlCYIOhkZiToAObksui-dP5AA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&lineWrap=false&presets=babili&prettier=false&targets=&version=6.26.0&envVersion=

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.