babel-plugin-minify-mangle-names
Advanced tools
Comparing version 0.4.0-alpha.6546ad11 to 0.4.0-alpha.caaefb4c
@@ -15,11 +15,10 @@ "use strict"; | ||
} | ||
var visitor = traverse.explode(_visitor); | ||
var queue = [path]; | ||
var current = void 0; | ||
var current; | ||
while (queue.length > 0) { | ||
current = queue.shift(); | ||
current = queue.shift(); // call | ||
// call | ||
if (visitor && visitor[current.type] && Array.isArray(visitor[current.type].enter)) { | ||
@@ -33,5 +32,4 @@ var fns = visitor[current.type].enter; | ||
for (var _iterator = fns[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var fn = _step.value; | ||
if (typeof fn === "function") fn(current); | ||
var _fn = _step.value; | ||
if (typeof _fn === "function") _fn(current); | ||
} | ||
@@ -43,3 +41,3 @@ } catch (err) { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
@@ -56,3 +54,2 @@ } | ||
var fields = getFields(current); | ||
var _iteratorNormalCompletion2 = true; | ||
@@ -64,6 +61,5 @@ var _didIteratorError2 = false; | ||
for (var _iterator2 = fields[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var field = _step2.value; | ||
var _field = _step2.value; | ||
var child = current.get(_field); | ||
var child = current.get(field); | ||
if (Array.isArray(child)) { | ||
@@ -77,5 +73,4 @@ // visit container left to right | ||
for (var _iterator3 = child[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var c = _step3.value; | ||
if (c.node) queue.push(c); | ||
var _c = _step3.value; | ||
if (_c.node) queue.push(_c); | ||
} | ||
@@ -87,3 +82,3 @@ } catch (err) { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return != null) { | ||
_iterator3.return(); | ||
@@ -106,3 +101,3 @@ } | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
@@ -109,0 +104,0 @@ } |
"use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var CHARSET = ("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ$_").split(""); | ||
module.exports = function () { | ||
module.exports = | ||
/*#__PURE__*/ | ||
function () { | ||
function Charset(shouldConsider) { | ||
@@ -61,2 +65,3 @@ var _this = this; | ||
num++; | ||
do { | ||
@@ -67,2 +72,3 @@ num--; | ||
} while (num > 0); | ||
return ret; | ||
@@ -69,0 +75,0 @@ } |
@@ -1,11 +0,13 @@ | ||
"use strict"; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
// Set that counts | ||
module.exports = function () { | ||
module.exports = | ||
/*#__PURE__*/ | ||
function () { | ||
function CountedSet() { | ||
@@ -21,3 +23,3 @@ _classCallCheck(this, CountedSet); | ||
value: function keys() { | ||
return [].concat(_toConsumableArray(this.map.keys())); | ||
return _toConsumableArray(this.map.keys()); | ||
} | ||
@@ -35,2 +37,3 @@ }, { | ||
} | ||
this.map.set(value, this.map.get(value) + 1); | ||
@@ -43,2 +46,3 @@ } | ||
var count = this.map.get(value); | ||
if (count <= 1) { | ||
@@ -45,0 +49,0 @@ this.map.delete(value); |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
// this fixes a bug where converting let to var | ||
@@ -12,2 +10,3 @@ // doesn't change the binding's scope to function scope | ||
} | ||
var fnScope = path.scope.getFunctionParent() || path.scope.getProgramParent(); | ||
@@ -17,16 +16,13 @@ var bindingIds = path.getOuterBindingIdentifierPaths(); | ||
for (var name in bindingIds) { | ||
var binding = path.scope.getBinding(name); | ||
var binding = path.scope.getBinding(name); // var isn't hoisted to fnScope | ||
// var isn't hoisted to fnScope | ||
if (binding.scope !== fnScope) { | ||
var existingBinding = fnScope.bindings[name]; | ||
// make sure we are clear that the fnScope doesn't already have | ||
var existingBinding = fnScope.bindings[name]; // make sure we are clear that the fnScope doesn't already have | ||
// an existing binding | ||
if (!existingBinding) { | ||
// move binding to the function scope | ||
// update our scopeTracker first before | ||
// we mutate the scope | ||
mangler.scopeTracker.moveBinding(binding, fnScope); | ||
fnScope.bindings[name] = binding; | ||
@@ -38,13 +34,9 @@ binding.scope = fnScope; | ||
// binding.scope and fnScope | ||
var newName = fnScope.generateUid(binding.scope.generateUid(name)); | ||
var newName = fnScope.generateUid(binding.scope.generateUid(name)); // rename binding in the original scope | ||
// rename binding in the original scope | ||
mangler.rename(binding.scope, binding, name, newName); | ||
// move binding to fnScope as newName | ||
mangler.rename(binding.scope, binding, name, newName); // move binding to fnScope as newName | ||
// update our scopeTracker first before | ||
// we mutate the scope | ||
mangler.scopeTracker.moveBinding(binding, fnScope); | ||
fnScope.bindings[newName] = binding; | ||
@@ -57,3 +49,4 @@ binding.scope = fnScope; | ||
} | ||
}); | ||
}; |
284
lib/index.js
"use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var Charset = require("./charset"); | ||
var ScopeTracker = require("./scope-tracker"); | ||
var isLabelIdentifier = require("./is-label-identifier"); | ||
var bfsTraverseCreator = require("./bfs-traverse"); | ||
var fixupVarScoping = require("./fixup-var-scoping"); | ||
@@ -25,19 +31,20 @@ | ||
traverse = babel.traverse; | ||
var bfsTraverse = bfsTraverseCreator(babel); | ||
var hop = Object.prototype.hasOwnProperty; | ||
var Mangler = function () { | ||
var Mangler = | ||
/*#__PURE__*/ | ||
function () { | ||
function Mangler(charset, program) { | ||
var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, | ||
_ref$exclude = _ref.exclude, | ||
exclude = _ref$exclude === undefined ? {} : _ref$exclude, | ||
exclude = _ref$exclude === void 0 ? {} : _ref$exclude, | ||
_ref$keepFnName = _ref.keepFnName, | ||
keepFnName = _ref$keepFnName === undefined ? false : _ref$keepFnName, | ||
keepFnName = _ref$keepFnName === void 0 ? false : _ref$keepFnName, | ||
_ref$keepClassName = _ref.keepClassName, | ||
keepClassName = _ref$keepClassName === undefined ? false : _ref$keepClassName, | ||
keepClassName = _ref$keepClassName === void 0 ? false : _ref$keepClassName, | ||
_ref$eval = _ref.eval, | ||
_eval = _ref$eval === undefined ? false : _ref$eval, | ||
_eval = _ref$eval === void 0 ? false : _ref$eval, | ||
_ref$topLevel = _ref.topLevel, | ||
topLevel = _ref$topLevel === undefined ? false : _ref$topLevel; | ||
topLevel = _ref$topLevel === void 0 ? false : _ref$topLevel; | ||
@@ -47,5 +54,4 @@ _classCallCheck(this, Mangler); | ||
this.charset = charset; | ||
this.program = program; | ||
this.program = program; // user passed options | ||
// user passed options | ||
this.exclude = toObject(exclude); | ||
@@ -55,5 +61,4 @@ this.keepFnName = keepFnName; | ||
this.topLevel = topLevel; | ||
this.eval = _eval; | ||
this.eval = _eval; // tracking | ||
// tracking | ||
this.visitedScopes = new Set(); | ||
@@ -63,3 +68,2 @@ this.scopeTracker = new ScopeTracker(); | ||
} | ||
/** | ||
@@ -79,3 +83,2 @@ * Run the mangler | ||
} | ||
/** | ||
@@ -91,3 +94,2 @@ * Tells if a variable name is excluded | ||
} | ||
/** | ||
@@ -108,3 +110,2 @@ * Clears traverse cache and recrawls the AST | ||
} | ||
/** | ||
@@ -127,3 +128,2 @@ * Re-crawling comes with a side-effect that let->var conversion | ||
} | ||
/** | ||
@@ -144,6 +144,3 @@ * A single pass through the AST to collect info for | ||
var scopeTracker = mangler.scopeTracker; | ||
scopeTracker.addScope(this.program.scope); | ||
/** | ||
@@ -156,3 +153,2 @@ * Same usage as in DCE, whichever runs first | ||
} | ||
/** | ||
@@ -166,9 +162,9 @@ * The visitors to be used in traversal. | ||
*/ | ||
var collectVisitor = { | ||
Scopable(_ref2) { | ||
var scope = _ref2.scope; | ||
scopeTracker.addScope(scope); // Collect bindings defined in the scope | ||
scopeTracker.addScope(scope); | ||
// Collect bindings defined in the scope | ||
Object.keys(scope.bindings).forEach(function (name) { | ||
@@ -178,2 +174,3 @@ scopeTracker.addBinding(scope.bindings[name]); | ||
}, | ||
/** | ||
@@ -189,13 +186,19 @@ * This is necessary because, in Babel, the scope.references | ||
ReferencedIdentifier(path) { | ||
if (isLabelIdentifier(path)) return; | ||
if (isLabelIdentifier(path)) { | ||
return; | ||
} | ||
var scope = path.scope, | ||
name = path.node.name; | ||
var binding = scope.getBinding(name); | ||
var binding = scope.getBinding(name); | ||
if (!binding) { | ||
// Do not collect globals as they are already available via | ||
// babel's API | ||
if (scope.hasGlobal(name)) return; | ||
// This should NOT happen ultimately. Panic if this code block is | ||
if (scope.hasGlobal(name)) { | ||
return; | ||
} // This should NOT happen ultimately. Panic if this code block is | ||
// reached | ||
throw new Error(`Binding not found for ReferencedIdentifier "${name}" ` + `present in "${path.parentPath.type}". ` + `Please report this at ${newIssueUrl}`); | ||
@@ -206,55 +209,5 @@ } else { | ||
} | ||
}, | ||
} | ||
/** | ||
* This is useful to detect binding ids and add them to the | ||
* scopeTracker's bindings | ||
* | ||
* TODO: | ||
* | ||
* This visitor is probably unnecessary. It was added to capture the | ||
* bindings that was not present in scope.bindings. But, now, it looks | ||
* like the unit and smoke tests pass without this. | ||
*/ | ||
BindingIdentifier(path) { | ||
if (isLabelIdentifier(path)) return; | ||
var scope = path.scope, | ||
name = path.node.name; | ||
var binding = scope.getBinding(name); | ||
/** | ||
* We have already captured the bindings when traversing through | ||
* Scopables, if a binding identifier doesn't have a binding, it | ||
* probably means that another transformation created a new binding, | ||
* refer https://github.com/babel/minify/issues/549 for example - | ||
* binding created by plugin transform-es2015-function-name | ||
* | ||
* So we just don't care about bindings that do not exist | ||
* | ||
* TODO: | ||
* | ||
* this deopts in DCE as this name can be removed for this particular | ||
* case (es2015-function-name) | ||
*/ | ||
if (!binding) return; | ||
/** | ||
* Detect constant violations | ||
* | ||
* If it's a constant violation, then add the Identifier Path as | ||
* a Reference instead of Binding - This is because the ScopeTracker | ||
* tracks these Re-declaration and mutation of variables as References | ||
* as it is simple to rename them | ||
*/ | ||
if (binding.identifier === path.node) { | ||
scopeTracker.addBinding(binding); | ||
} else { | ||
// constant violation | ||
scopeTracker.addReference(scope, binding, name); | ||
} | ||
} | ||
}; | ||
/** | ||
@@ -265,24 +218,26 @@ * These visitors are for collecting the Characters used in the program | ||
*/ | ||
if (this.charset.shouldConsider) { | ||
collectVisitor.Identifier = function Identifer(path) { | ||
var node = path.node; | ||
// We don't mangle properties, so we collect them as they contribute | ||
var node = path.node; // We don't mangle properties, so we collect them as they contribute | ||
// to the frequency of characters | ||
if (path.parentPath.isMemberExpression({ property: node }) || path.parentPath.isObjectProperty({ key: node })) { | ||
if (path.parentPath.isMemberExpression({ | ||
property: node | ||
}) || path.parentPath.isObjectProperty({ | ||
key: node | ||
})) { | ||
mangler.charset.consider(node.name); | ||
} | ||
}; | ||
collectVisitor.Literal = function Literal(_ref3) { | ||
var node = _ref3.node; | ||
mangler.charset.consider(String(node.value)); | ||
}; | ||
} | ||
} // Traverse the AST | ||
// Traverse the AST | ||
bfsTraverse(mangler.program, collectVisitor); | ||
} | ||
/** | ||
@@ -304,3 +259,2 @@ * Tells if a binding is exported as a NamedExport - so as to NOT mangle | ||
var refs = binding.referencePaths; | ||
var _iteratorNormalCompletion = true; | ||
@@ -312,10 +266,9 @@ var _didIteratorError = false; | ||
for (var _iterator = refs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var ref = _step.value; | ||
var _ref4 = _step.value; | ||
if (ref.isExportNamedDeclaration()) { | ||
if (_ref4.isExportNamedDeclaration()) { | ||
return true; | ||
} | ||
} | ||
} // default | ||
// default | ||
} catch (err) { | ||
@@ -326,3 +279,3 @@ _didIteratorError = true; | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
@@ -339,3 +292,2 @@ } | ||
} | ||
/** | ||
@@ -350,9 +302,7 @@ * Mangle the scope | ||
var mangler = this; | ||
var scopeTracker = mangler.scopeTracker; | ||
var scopeTracker = mangler.scopeTracker; // Unsafe Scope | ||
// Unsafe Scope | ||
if (!mangler.eval && hasEval(scope)) return; | ||
// Already visited | ||
if (!mangler.eval && hasEval(scope)) { | ||
return; | ||
} // Already visited | ||
// This is because for a function, in Babel, the function and | ||
@@ -362,10 +312,16 @@ // the function body's BlockStatement has the same scope, and will | ||
// it only once | ||
if (mangler.visitedScopes.has(scope)) return; | ||
mangler.visitedScopes.add(scope); | ||
// Helpers to generate names | ||
if (mangler.visitedScopes.has(scope)) { | ||
return; | ||
} | ||
mangler.visitedScopes.add(scope); // Helpers to generate names | ||
var i = 0; | ||
function getNext() { | ||
return mangler.charset.getIdentifier(i++); | ||
} | ||
function resetNext() { | ||
@@ -376,4 +332,4 @@ i = 0; | ||
var bindings = scopeTracker.bindings.get(scope); | ||
var names = [].concat(_toConsumableArray(bindings.keys())); | ||
var names = _toConsumableArray(bindings.keys()); | ||
/** | ||
@@ -384,23 +340,17 @@ * 1. Iterate through the list of BindingIdentifiers | ||
*/ | ||
for (var _i = 0; _i < names.length; _i++) { | ||
var oldName = names[_i]; | ||
var binding = bindings.get(oldName); | ||
var binding = bindings.get(oldName); // Names which should NOT be mangled | ||
// Names which should NOT be mangled | ||
if ( | ||
// arguments - for non-strict mode | ||
oldName === "arguments" || | ||
// labels | ||
binding.path.isLabeledStatement() || | ||
// ClassDeclaration has binding in two scopes | ||
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 | ||
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)) { | ||
@@ -411,15 +361,14 @@ continue; | ||
var next = void 0; | ||
do { | ||
next = getNext(); | ||
} while (!t.isValidIdentifier(next) || scopeTracker.hasBinding(scope, next) || scope.hasGlobal(next) || scopeTracker.hasReference(scope, next) || !scopeTracker.canUseInReferencedScopes(binding, next)); | ||
} 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 | ||
// Reset so variables which are removed can be reused | ||
resetNext(); | ||
// Once we detected a valid `next` Identifier which could be used, | ||
resetNext(); // Once we detected a valid `next` Identifier which could be used, | ||
// call the renamer | ||
mangler.rename(scope, binding, oldName, next); | ||
} | ||
} | ||
/** | ||
@@ -434,3 +383,2 @@ * The mangle function that traverses through all the Scopes in a BFS | ||
var mangler = this; | ||
bfsTraverse(this.program, { | ||
@@ -440,5 +388,5 @@ Scopable(path) { | ||
} | ||
}); | ||
} | ||
/** | ||
@@ -463,4 +411,4 @@ * Given a NodePath, collects all the Identifiers which are BindingIdentifiers | ||
}; | ||
var bindingIds = path.getBindingIdentifierPaths(true, false); | ||
var bindingIds = path.getBindingIdentifierPaths(true, false); | ||
for (var name in bindingIds) { | ||
@@ -474,8 +422,9 @@ if (name !== oldName) continue; | ||
for (var _iterator2 = bindingIds[name][Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var idPath = _step2.value; | ||
var _idPath = _step2.value; | ||
if (predicate(idPath)) { | ||
this.renamedNodes.add(idPath.node); | ||
idPath.replaceWith(t.identifier(newName)); | ||
this.renamedNodes.add(idPath.node); | ||
if (predicate(_idPath)) { | ||
_idPath.node.name = newName; // babel-7 don't requeue | ||
// idPath.replaceWith(t.identifier(newName)); | ||
this.renamedNodes.add(_idPath.node); | ||
} | ||
@@ -488,3 +437,3 @@ } | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
@@ -500,3 +449,2 @@ } | ||
} | ||
/** | ||
@@ -522,30 +470,25 @@ * The Renamer: | ||
var mangler = this; | ||
var scopeTracker = mangler.scopeTracker; | ||
var scopeTracker = mangler.scopeTracker; // rename at the declaration level | ||
// rename at the declaration level | ||
this.renameBindingIds(binding.path, oldName, newName, function (idPath) { | ||
return idPath.node === binding.identifier; | ||
}); | ||
}); // update mangler's ScopeTracker | ||
// update mangler's ScopeTracker | ||
scopeTracker.renameBinding(scope, oldName, newName); | ||
scopeTracker.renameBinding(scope, oldName, newName); // update all constant violations | ||
// update all constant violations | ||
var violations = binding.constantViolations; | ||
for (var i = 0; i < violations.length; i++) { | ||
if (violations[i].isLabeledStatement()) continue; | ||
this.renameBindingIds(violations[i], oldName, newName); | ||
scopeTracker.updateReference(violations[i].scope, binding, oldName, newName); | ||
} | ||
} // update all referenced places | ||
// update all referenced places | ||
var refs = binding.referencePaths; | ||
for (var _i2 = 0; _i2 < refs.length; _i2++) { | ||
var path = refs[_i2]; | ||
var node = path.node; | ||
if (!path.isIdentifier()) { | ||
@@ -565,18 +508,23 @@ // Ideally, this should not happen | ||
} | ||
var actualBinding = refPath.scope.getBinding(oldName); | ||
if (actualBinding !== binding) { | ||
return; | ||
} | ||
refPath.node.name = newName; // babel-7 don't requeue | ||
// refPath.replaceWith(t.identifier(newName)); | ||
mangler.renamedNodes.add(refPath.node); | ||
refPath.replaceWith(t.identifier(newName)); | ||
mangler.renamedNodes.add(refPath.node); | ||
scopeTracker.updateReference(refPath.scope, binding, oldName, newName); | ||
} | ||
}); | ||
} else if (!isLabelIdentifier(path)) { | ||
if (path.node.name === oldName) { | ||
path.node.name = newName; // babel-7 don't requeue | ||
// path.replaceWith(t.identifier(newName)); | ||
mangler.renamedNodes.add(path.node); | ||
path.replaceWith(t.identifier(newName)); | ||
mangler.renamedNodes.add(path.node); | ||
scopeTracker.updateReference(path.scope, binding, oldName, newName); | ||
@@ -590,9 +538,10 @@ } else if (mangler.renamedNodes.has(path.node)) { | ||
} | ||
} | ||
// else label identifier - silently ignore | ||
} | ||
} // else label identifier - silently ignore | ||
// update babel's scope tracking | ||
} // update babel's internal tracking | ||
binding.identifier.name = newName; // update babel's internal scope tracking | ||
var bindings = scope.bindings; | ||
bindings[newName] = binding; | ||
@@ -619,14 +568,13 @@ delete bindings[oldName]; | ||
var shouldConsiderSource = path.getSource().length > 70000; | ||
var charset = new Charset(shouldConsiderSource); | ||
var mangler = new Mangler(charset, path, this.opts); | ||
mangler.run(); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
}; // convert value to object | ||
// convert value to object | ||
function toObject(value) { | ||
@@ -638,16 +586,18 @@ if (!Array.isArray(value)) { | ||
var map = {}; | ||
for (var i = 0; i < value.length; i++) { | ||
map[value[i]] = true; | ||
} | ||
return map; | ||
} | ||
} // for keepFnName | ||
// for keepFnName | ||
function isFunction(path) { | ||
return path.isFunctionExpression() || path.isFunctionDeclaration(); | ||
} | ||
} // for keepClassName | ||
// for keepClassName | ||
function isClass(path) { | ||
return path.isClassExpression() || path.isClassDeclaration(); | ||
} |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
module.exports = isLabelIdentifier; | ||
@@ -7,4 +5,9 @@ | ||
var node = path.node; | ||
return path.parentPath.isLabeledStatement({ label: node }) || path.parentPath.isBreakStatement({ label: node }) || path.parentPath.isContinueStatement({ label: node }); | ||
return path.parentPath.isLabeledStatement({ | ||
label: node | ||
}) || path.parentPath.isBreakStatement({ | ||
label: node | ||
}) || path.parentPath.isContinueStatement({ | ||
label: node | ||
}); | ||
} |
"use strict"; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var CountedSet = require("./counted-set"); | ||
var isLabelIdentifier = require("./is-label-identifier"); | ||
var newIssueUrl = "https://github.com/babel/minify/issues/new"; | ||
/** | ||
@@ -17,3 +19,6 @@ * ScopeTracker | ||
*/ | ||
module.exports = function () { | ||
module.exports = | ||
/*#__PURE__*/ | ||
function () { | ||
function ScopeTracker() { | ||
@@ -25,3 +30,2 @@ _classCallCheck(this, ScopeTracker); | ||
} | ||
/** | ||
@@ -39,2 +43,3 @@ * Register a new Scope and initiliaze it with empty sets | ||
} | ||
if (!this.bindings.has(scope)) { | ||
@@ -44,3 +49,2 @@ this.bindings.set(scope, new Map()); | ||
} | ||
/** | ||
@@ -58,11 +62,13 @@ * Add reference to all Scopes between and including the ReferencedScope | ||
var parent = scope; | ||
do { | ||
this.references.get(parent).add(name); | ||
if (!binding) { | ||
throw new Error(`Binding Not Found for ${name} during scopeTracker.addRefernce. ` + `Please report at ${newIssueUrl}`); | ||
} | ||
if (binding.scope === parent) break; | ||
} while (parent = parent.parent); | ||
} | ||
/** | ||
@@ -83,3 +89,2 @@ * has a Reference in the given {Scope} or a child Scope | ||
} | ||
/** | ||
@@ -99,5 +104,5 @@ * Update reference count in all scopes between and including the | ||
var parent = scope; | ||
do { | ||
var ref = this.references.get(parent); | ||
ref.delete(oldName); | ||
@@ -114,3 +119,2 @@ ref.add(newName); | ||
} | ||
/** | ||
@@ -128,3 +132,2 @@ * has either a Binding or a Reference | ||
} | ||
/** | ||
@@ -149,5 +152,3 @@ * For a Binding visit all places where the Binding is used and detect | ||
return false; | ||
} | ||
// Safari raises a syntax error for a `let` or `const` declaration in a | ||
} // Safari raises a syntax error for a `let` or `const` declaration in a | ||
// `for` loop initialization that shadows a parent function's parameter. | ||
@@ -157,13 +158,30 @@ // https://github.com/babel/minify/issues/559 | ||
// https://trac.webkit.org/changeset/217200/webkit/trunk/Source | ||
var maybeDecl = binding.path.parentPath; | ||
var isBlockScoped = maybeDecl.isVariableDeclaration({ kind: "let" }) || maybeDecl.isVariableDeclaration({ kind: "const" }); | ||
var isBlockScoped = maybeDecl.isVariableDeclaration({ | ||
kind: "let" | ||
}) || maybeDecl.isVariableDeclaration({ | ||
kind: "const" | ||
}); | ||
if (isBlockScoped) { | ||
var maybeFor = maybeDecl.parentPath; | ||
var isForLoopBinding = maybeFor.isForStatement({ init: maybeDecl.node }) || maybeFor.isForXStatement({ left: maybeDecl.node }); | ||
var isForLoopBinding = maybeFor.isForStatement({ | ||
init: maybeDecl.node | ||
}) || maybeFor.isForXStatement({ | ||
left: maybeDecl.node | ||
}); | ||
if (isForLoopBinding) { | ||
var fnParent = maybeFor.getFunctionParent(); | ||
if (fnParent.isFunction({ body: maybeFor.parent })) { | ||
var fnParent = getFunctionParent(maybeFor); | ||
if (fnParent.isFunction({ | ||
body: maybeFor.parent | ||
})) { | ||
var parentFunctionBinding = this.bindings.get(fnParent.scope).get(next); | ||
if (parentFunctionBinding) { | ||
var parentFunctionHasParamBinding = parentFunctionBinding.kind === "param"; | ||
if (parentFunctionHasParamBinding) { | ||
@@ -179,2 +197,3 @@ return false; | ||
var violation = binding.constantViolations[i]; | ||
if (tracker.hasBindingOrReference(violation.scope, binding, next)) { | ||
@@ -187,2 +206,3 @@ return false; | ||
var ref = binding.referencePaths[_i]; | ||
if (!ref.isIdentifier()) { | ||
@@ -193,2 +213,3 @@ var canUse = true; | ||
if (path.node.name !== next) return; | ||
if (tracker.hasBindingOrReference(path.scope, binding, next)) { | ||
@@ -198,3 +219,5 @@ canUse = false; | ||
} | ||
}); | ||
if (!canUse) { | ||
@@ -212,3 +235,2 @@ return canUse; | ||
} | ||
/** | ||
@@ -235,5 +257,4 @@ * Add a binding to Tracker in binding's own Scope | ||
} | ||
/** | ||
* Moves Binding from it's own Scope to {toScope} | ||
* Moves Binding from it's own Scope to {@param toScope} | ||
* | ||
@@ -252,3 +273,2 @@ * required for fixup-var-scope | ||
} | ||
/** | ||
@@ -265,3 +285,2 @@ * has a Binding in the current {Scope} | ||
} | ||
/** | ||
@@ -284,2 +303,11 @@ * Update the ScopeTracker on rename | ||
return ScopeTracker; | ||
}(); | ||
}(); | ||
/** | ||
* Babel-7 returns null if there is no function parent | ||
* and uses getProgramParent to get Program | ||
*/ | ||
function getFunctionParent(path) { | ||
return (path.scope.getFunctionParent() || path.scope.getProgramParent()).path; | ||
} |
{ | ||
"name": "babel-plugin-minify-mangle-names", | ||
"version": "0.4.0-alpha.6546ad11", | ||
"version": "0.4.0-alpha.caaefb4c", | ||
"description": "", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-mark-eval-scopes": "^0.4.0-alpha.6546ad11" | ||
"babel-helper-mark-eval-scopes": "^0.4.0-alpha.caaefb4c" | ||
} | ||
} |
@@ -68,3 +68,3 @@ # babel-plugin-minify-mangle-names | ||
```javascript | ||
require("babel-core").transform("code", { | ||
require("@babel/core").transform("code", { | ||
plugins: ["minify-mangle-names"] | ||
@@ -71,0 +71,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
39007
962