Skip to content

Commit

Permalink
Fixes bug that was causing prefix to be multiplied in Immediately-Inv…
Browse files Browse the repository at this point in the history
…oked Function Expressions (IIFE) with multiple variable declarations
  • Loading branch information
joaosamouco committed Apr 20, 2016
1 parent 8afbed5 commit 96b760a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/esshorten.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion test/mangle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 96b760a

Please sign in to comment.