Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
sudo: false
language: node_js
node_js:
- "5"
- "4"
- "6"
- "8"
- "10"
- "11"
11 changes: 9 additions & 2 deletions lib/esshorten.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@
let generator = new NameGenerator(scope, options);
let prefix = options.renamePrefix;

const shouldRename = options && options.shouldRename || () => true;
const shouldRename = options && options.shouldRename || (() => true);

if (scope.isStatic()) {
let name = '9';
let variableIndexMap = new Map();

scope.variables.forEach((variable, index) => {
variableIndexMap.set(variable, index);
});

scope.variables.sort((a, b) => {
if (a.tainted) {
Expand All @@ -101,7 +106,9 @@
if (b.tainted) {
return -1;
}
return (b.identifiers.length + b.references.length) - (a.identifiers.length + a.references.length);

let diff = (b.identifiers.length + b.references.length) - (a.identifiers.length + a.references.length);
return diff === 0 ? variableIndexMap.get(a) - variableIndexMap.get(b) : diff;
});

for (let variable of scope.variables) {
Expand Down
8 changes: 8 additions & 0 deletions test/mangle.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ describe 'mangle:', ->
expect(f.params[0].name).not.to.equal a.body.body[0].expression.name
expect(a.id.name).not.to.equal a.body.body[0].expression.name

it 'sort for node11', ->
program = esprima.parse 'function f(a1, a2, a3, a4, a5) { var b1, b2, b3, b4, b5 }'

result = esshorten.mangle program
expect(result.body[0].params[0].name).to.equal 'a'
expect(result.body[0].body.body[0].declarations[0].id.name).to.equal 'f'


describe 'nested scope handling:', ->
it 'shortens nested function names', ->
program = esprima.parse 'function f() { function g() {} }'
Expand Down