Skip to content

Commit ad51902

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 ad51902

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

lib/esshorten.js

+3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
if (!prefix) {
7979
prefix = '';
8080
}
81+
if (tip.indexOf(prefix) === 0) {
82+
tip = tip.substr(prefix.length);
83+
}
8184
do {
8285
tip = utility.generateNextName(tip);
8386
} while (!this.passAsUnique(prefix + tip));

test/mangle.coffee

+9-1
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)