From 96b760a520f84daf7691ea675f799906ec16613f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Samouco?= Date: Wed, 20 Apr 2016 15:24:37 +0100 Subject: [PATCH] Fixes bug that was causing prefix to be multiplied in Immediately-Invoked Function Expressions (IIFE) with multiple variable declarations --- lib/esshorten.js | 3 +++ test/mangle.coffee | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/esshorten.js b/lib/esshorten.js index 1fb2f9f..3b88b3b 100644 --- a/lib/esshorten.js +++ b/lib/esshorten.js @@ -79,6 +79,9 @@ prefix = ''; } do { + if (tip.indexOf(prefix) === 0) { + tip = tip.substr(prefix.length); + } tip = utility.generateNextName(tip); } while (!this.passAsUnique(prefix + tip)); return prefix + tip; diff --git a/test/mangle.coffee b/test/mangle.coffee index 33347c7..cbccc9b 100644 --- a/test/mangle.coffee +++ b/test/mangle.coffee @@ -186,9 +186,17 @@ describe 'mangle:', -> expect(result.body[0].expression.id.name).to.equal 'name' describe '`renamePrefix` option:', -> - program = esprima.parse '(function name() { var i = 42; });' it 'prefixes identifier with the given value', -> + program = esprima.parse '(function name() { var i = 42; });' result = esshorten.mangle program, renamePrefix: 'foo_' expect(result.body[0].expression.id.name.indexOf('foo_')).to.equal 0 + + it 'prefixes identifier inside IIFE without multiplying prefixes', -> + program = esprima.parse '(function name() { var i = 42; var a = 5; })();' + result = esshorten.mangle program, + renamePrefix: 'foo_' + + expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_')).to.equal 0 + expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_foo_')).to.equal -1