babel-plugin-minify-mangle-names
Advanced tools
Comparing version 0.4.3 to 0.5.0-alpha.9
102
lib/index.js
@@ -277,2 +277,38 @@ "use strict"; | ||
/** | ||
* Tells if the name can be mangled in the current observed scope with | ||
* the input binding | ||
* | ||
* @param {string} oldName the old name that needs to be mangled | ||
* @param {Binding} binding Binding of the name | ||
* @param {Scope} scope The current scope the mangler is run | ||
*/ | ||
canMangle(oldName, binding, scope) { | ||
const cannotMangle = // arguments - for non-strict mode | ||
oldName === "arguments" || // labels | ||
binding.path.isLabeledStatement() || // ClassDeclaration has binding in two scopes | ||
// 1. The scope in which it is declared | ||
// 2. The class's own scope | ||
binding.path.isClassDeclaration() && binding.path === scope.path || // excluded | ||
this.isExcluded(oldName) || ( // function names | ||
this.keepFnName ? isFunction(binding.path) : false) || ( // class names | ||
this.keepClassName ? isClass(binding.path) : false) || // named export | ||
this.isExportedWithName(binding); | ||
return !cannotMangle; | ||
} | ||
/** | ||
* Tells if the newName can be used as a valid name for the input binding | ||
* in the input scope | ||
* | ||
* @param {string} newName the old name that needs to be mangled | ||
* @param {Binding} binding Binding of the name that this new name will replace | ||
* @param {Scope} scope The current scope the mangler is run | ||
*/ | ||
isValidName(newName, binding, scope) { | ||
return t.isValidIdentifier(newName) && !this.scopeTracker.hasBinding(scope, newName) && !scope.hasGlobal(newName) && !this.scopeTracker.hasReference(scope, newName) && this.scopeTracker.canUseInReferencedScopes(binding, newName); | ||
} | ||
/** | ||
* Mangle the scope | ||
@@ -300,16 +336,8 @@ * @param {Scope} scope | ||
mangler.visitedScopes.add(scope); // Helpers to generate names | ||
mangler.visitedScopes.add(scope); | ||
const bindings = scopeTracker.bindings.get(scope); | ||
const names = [...bindings.keys()]; // A counter to generate names and reset | ||
// so we can reuse removed names | ||
let i = 0; | ||
function getNext() { | ||
return mangler.charset.getIdentifier(i++); | ||
} | ||
function resetNext() { | ||
i = 0; | ||
} | ||
const bindings = scopeTracker.bindings.get(scope); | ||
const names = [...bindings.keys()]; | ||
let counter = 0; | ||
/** | ||
@@ -319,32 +347,36 @@ * 1. Iterate through the list of BindingIdentifiers | ||
* 3. Update the scope tree. | ||
* | ||
* We cannot use a for..of loop over bindings.keys() | ||
* because (2) we rename in place and update the bindings | ||
* as we traverse through the keys | ||
*/ | ||
for (let i = 0; i < names.length; i++) { | ||
const oldName = names[i]; | ||
const binding = bindings.get(oldName); // Names which should NOT be mangled | ||
for (var _i = 0; _i < names.length; _i++) { | ||
const oldName = names[_i]; | ||
const binding = bindings.get(oldName); | ||
if ( // arguments - for non-strict mode | ||
oldName === "arguments" || // labels | ||
binding.path.isLabeledStatement() || // ClassDeclaration has binding in two scopes | ||
// 1. The scope in which it is declared | ||
// 2. The class's own scope | ||
binding.path.isClassDeclaration() && binding.path === scope.path || // excluded | ||
mangler.isExcluded(oldName) || ( // function names | ||
mangler.keepFnName ? isFunction(binding.path) : false) || ( // class names | ||
mangler.keepClassName ? isClass(binding.path) : false) || // named export | ||
mangler.isExportedWithName(binding)) { | ||
continue; | ||
} | ||
if (mangler.canMangle(oldName, binding, scope)) { | ||
let next; | ||
let next; | ||
do { | ||
next = mangler.charset.getIdentifier(counter++); | ||
} while (!mangler.isValidName(next, binding, scope)); // Reset so variables which are removed can be reused | ||
// | ||
// the following is an assumtion (for perf) | ||
// the length 3 is an assumption that if the oldName isn't | ||
// 1 or 2 characters, then probably we are not going to find | ||
// a name - because for almost all usecases we have 1 or 2 | ||
// character new names only. And for the edge cases where | ||
// one scope has lots and lots of variables, it's okay to | ||
// name something with 3 characters instead of 1 | ||
do { | ||
next = getNext(); | ||
} while (!t.isValidIdentifier(next) || scopeTracker.hasBinding(scope, next) || scope.hasGlobal(next) || scopeTracker.hasReference(scope, next) || !scopeTracker.canUseInReferencedScopes(binding, next)); // Reset so variables which are removed can be reused | ||
if (oldName.length < 3) { | ||
counter = 0; | ||
} // Once we detected a valid `next` Identifier which could be used, | ||
// call the renamer | ||
resetNext(); // Once we detected a valid `next` Identifier which could be used, | ||
// call the renamer | ||
mangler.rename(scope, binding, oldName, next); | ||
mangler.rename(scope, binding, oldName, next); | ||
} | ||
} | ||
@@ -351,0 +383,0 @@ } |
{ | ||
"name": "babel-plugin-minify-mangle-names", | ||
"version": "0.4.3", | ||
"version": "0.5.0-alpha.9+7002b8c", | ||
"description": "", | ||
@@ -15,4 +15,5 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-mark-eval-scopes": "^0.4.3" | ||
} | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.9+7002b8c" | ||
}, | ||
"gitHead": "7002b8cb72b9adf05eb80de4b3114bcf3c20da19" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35931
10
915
1
+ Addedbabel-helper-mark-eval-scopes@0.5.0-alpha.e9f96ffe(transitive)
- Removedbabel-helper-mark-eval-scopes@0.4.3(transitive)