babel-plugin-minify-mangle-names
Advanced tools
Comparing version 0.5.0-alpha.34c3efe9 to 0.5.0-alpha.36b27d42
@@ -146,2 +146,23 @@ "use strict"; | ||
/** | ||
* This is required because after function name transformation | ||
* plugin (part of es2015), the function name is NOT added to the | ||
* scope's bindings. So to fix this issue, we simply add a hack to | ||
* handle that case - fix it to the scope tree. | ||
* | ||
* Related: | ||
* - https://github.com/babel/minify/issues/829 | ||
*/ | ||
BindingIdentifier(path) { | ||
if ( // the parent has this id as the name | ||
(path.parentPath.isFunctionExpression({ | ||
id: path.node | ||
}) || path.parentPath.isClassExpression({ | ||
id: path.node | ||
})) && // and the id isn't yet added to the scope | ||
!hop.call(path.parentPath.scope.bindings, path.node.name)) { | ||
path.parentPath.scope.registerBinding("local", path.parentPath); | ||
} | ||
}, | ||
/** | ||
* This is necessary because, in Babel, the scope.references | ||
@@ -257,2 +278,38 @@ * does NOT contain the references in that scope. Only the program | ||
/** | ||
* 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 | ||
@@ -298,2 +355,6 @@ * @param {Scope} scope | ||
* 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 | ||
*/ | ||
@@ -303,28 +364,17 @@ | ||
const oldName = names[i]; | ||
const binding = bindings.get(oldName); // Names which should NOT be mangled | ||
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 = getNext(); | ||
} while (!mangler.isValidName(next, binding, scope)); // Reset so variables which are removed can be reused | ||
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 | ||
resetNext(); // 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); | ||
} | ||
} | ||
@@ -331,0 +381,0 @@ } |
{ | ||
"name": "babel-plugin-minify-mangle-names", | ||
"version": "0.5.0-alpha.34c3efe9", | ||
"version": "0.5.0-alpha.36b27d42", | ||
"description": "", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.34c3efe9" | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.36b27d42" | ||
} | ||
} |
@@ -34,3 +34,3 @@ # babel-plugin-minify-mangle-names | ||
```sh | ||
npm install babel-plugin-minify-mangle-names | ||
npm install babel-plugin-minify-mangle-names --save-dev | ||
``` | ||
@@ -37,0 +37,0 @@ |
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
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
34302
910