Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naming collision in mangle plugin when using transformFromAST #26

Closed
kangax opened this issue Jun 3, 2016 · 2 comments
Closed

Naming collision in mangle plugin when using transformFromAST #26

kangax opened this issue Jun 3, 2016 · 2 comments
Labels
bug Confirmed bug

Comments

@kangax
Copy link
Member

kangax commented Jun 3, 2016

TLDR:

(traverse + block-scoping) → AST → traverse.clearCache() → (traverseFromAST + mangle) results in a naming conflict :)

The following code:

const babel = require('babel-core');
const traverse = require('babel-traverse').default;
const mangle = require('./packages/babel-plugin-minify-mangle-names/lib/index.js');

const srcTxt = `
  function f(x) {
    for (let i = 0; i; i++) {
      let n;
      if (n) return;
      g(() => n);
    }
  }
`;

const ast = babel.transform(srcTxt, {
  code: false,
  plugins: ['transform-es2015-block-scoping'],
}).ast;

traverse.clearCache();

const code = babel.transformFromAst(ast, null, {
  plugins: [mangle],
}).code;

console.log(code);

produces this output:

function f(a) {
  var b = function (c) {
    var b = void 0;
    if (b) return {
        v: void 0
      };
    g(() => b);
  };

  for (var c = 0; c; c++) {
    var a = b(c);
    if (typeof a === "object") return a.v;
  }
}

Notice conflict between a in arguments and a in a loop block. Removing clearCache fixes it BUT then mangling is incomplete:

function f(a) {
  var _loop = function (b) {
    var b = void 0;
    if (b) return {
        v: void 0
      };
    g(() => b);
  };

  for (var b = 0; b; b++) {
    var _ret = _loop(b);

    if (typeof _ret === "object") return _ret.v;
  }
}
@jamiebuilds
Copy link
Contributor

Sidenote: @kittens Is there any reason that babel.transform* doesn't call traverse.clearCache() before or after it runs? Seems like you'd always want that unless I'm missing something

@hzoo hzoo added the bug Confirmed bug label Jul 1, 2016
@kangax kangax closed this as completed Jul 25, 2016
@kangax
Copy link
Member Author

kangax commented Jul 25, 2016

This is now fixed in a new mangler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bug
Projects
None yet
Development

No branches or pull requests

3 participants