Skip to content

Commit 96b760a

Browse files
committed
Fixes bug that was causing prefix to be multiplied in Immediately-Invoked Function Expressions (IIFE) with multiple variable declarations
1 parent 8afbed5 commit 96b760a

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/esshorten.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
prefix = '';
8080
}
8181
do {
82+
if (tip.indexOf(prefix) === 0) {
83+
tip = tip.substr(prefix.length);
84+
}
8285
tip = utility.generateNextName(tip);
8386
} while (!this.passAsUnique(prefix + tip));
8487
return prefix + tip;

test/mangle.coffee

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,17 @@ describe 'mangle:', ->
186186
expect(result.body[0].expression.id.name).to.equal 'name'
187187

188188
describe '`renamePrefix` option:', ->
189-
program = esprima.parse '(function name() { var i = 42; });'
190189

191190
it 'prefixes identifier with the given value', ->
191+
program = esprima.parse '(function name() { var i = 42; });'
192192
result = esshorten.mangle program,
193193
renamePrefix: 'foo_'
194194
expect(result.body[0].expression.id.name.indexOf('foo_')).to.equal 0
195+
196+
it 'prefixes identifier inside IIFE without multiplying prefixes', ->
197+
program = esprima.parse '(function name() { var i = 42; var a = 5; })();'
198+
result = esshorten.mangle program,
199+
renamePrefix: 'foo_'
200+
201+
expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_')).to.equal 0
202+
expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_foo_')).to.equal -1

0 commit comments

Comments
 (0)