diff options
Diffstat (limited to 'node_modules/babel-plugin-minify-mangle-names/lib')
7 files changed, 1082 insertions, 0 deletions
diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js b/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js new file mode 100644 index 00000000..5d381d31 --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/bfs-traverse.js @@ -0,0 +1,83 @@ +"use strict"; + +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } + +module.exports = function bfsTraverseCreator({ + types: t, + traverse +}) { + function getFields(path) { + return t.VISITOR_KEYS[path.type]; + } + + return function bfsTraverse(path, _visitor) { + if (!path.node) { + throw new Error("Not a valid path"); + } + + const visitor = traverse.explode(_visitor); + const queue = [path]; + let current; + + while (queue.length > 0) { + current = queue.shift(); // call + + if (visitor && visitor[current.type] && Array.isArray(visitor[current.type].enter)) { + const fns = visitor[current.type].enter; + + var _iterator = _createForOfIteratorHelper(fns), + _step; + + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + const fn = _step.value; + if (typeof fn === "function") fn(current); + } + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } + } + + const fields = getFields(current); + + var _iterator2 = _createForOfIteratorHelper(fields), + _step2; + + try { + for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { + const field = _step2.value; + const child = current.get(field); + + if (Array.isArray(child)) { + // visit container left to right + var _iterator3 = _createForOfIteratorHelper(child), + _step3; + + try { + for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { + const c = _step3.value; + if (c.node) queue.push(c); + } + } catch (err) { + _iterator3.e(err); + } finally { + _iterator3.f(); + } + } else { + if (child.node) queue.push(child); + } + } + } catch (err) { + _iterator2.e(err); + } finally { + _iterator2.f(); + } + } + }; +};
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/charset.js b/node_modules/babel-plugin-minify-mangle-names/lib/charset.js new file mode 100644 index 00000000..812f7a44 --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/charset.js @@ -0,0 +1,52 @@ +"use strict"; + +const CHARSET = ("abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ$_").split(""); +module.exports = class Charset { + constructor(shouldConsider) { + this.shouldConsider = shouldConsider; + this.chars = CHARSET.slice(); + this.frequency = {}; + this.chars.forEach(c => { + this.frequency[c] = 0; + }); + this.finalized = false; + } + + consider(str) { + if (!this.shouldConsider) { + return; + } + + str.split("").forEach(c => { + if (this.frequency[c] != null) { + this.frequency[c]++; + } + }); + } + + sort() { + if (this.shouldConsider) { + this.chars = this.chars.sort((a, b) => this.frequency[b] - this.frequency[a]); + } + + this.finalized = true; + } + + getIdentifier(num) { + if (!this.finalized) { + throw new Error("Should sort first"); + } + + let ret = ""; + num++; + + do { + num--; + ret += this.chars[num % this.chars.length]; + num = Math.floor(num / this.chars.length); + } while (num > 0); + + return ret; + } + +};
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/counted-set.js b/node_modules/babel-plugin-minify-mangle-names/lib/counted-set.js new file mode 100644 index 00000000..f9f3a364 --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/counted-set.js @@ -0,0 +1,37 @@ +"use strict"; + +// Set that counts +module.exports = class CountedSet { + constructor() { + // because you can't simply extend Builtins yet + this.map = new Map(); + } + + keys() { + return [...this.map.keys()]; + } + + has(value) { + return this.map.has(value); + } + + add(value) { + if (!this.has(value)) { + this.map.set(value, 0); + } + + this.map.set(value, this.map.get(value) + 1); + } + + delete(value) { + if (!this.has(value)) return; + const count = this.map.get(value); + + if (count <= 1) { + this.map.delete(value); + } else { + this.map.set(value, count - 1); + } + } + +};
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js b/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js new file mode 100644 index 00000000..2945315d --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/fixup-var-scoping.js @@ -0,0 +1,50 @@ +"use strict"; + +// this fixes a bug where converting let to var +// doesn't change the binding's scope to function scope +// https://github.com/babel/babel/issues/4818 +module.exports = function (mangler) { + mangler.program.traverse({ + VariableDeclaration(path) { + if (path.node.kind !== "var") { + return; + } + + const fnScope = path.scope.getFunctionParent() || path.scope.getProgramParent(); + const bindingIds = path.getOuterBindingIdentifierPaths(); + + for (const name in bindingIds) { + const binding = path.scope.getBinding(name); // var isn't hoisted to fnScope + + if (binding.scope !== fnScope) { + const 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; + binding.scope = fnScope; + delete binding.scope.bindings[name]; + } else { + // we need a new binding that's valid in both the scopes + // binding.scope and fnScope + const newName = fnScope.generateUid(binding.scope.generateUid(name)); // rename binding in the original scope + + 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; + binding.scope = fnScope; + delete binding.scope.bindings[newName]; + } + } + } + } + + }); +};
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/index.js b/node_modules/babel-plugin-minify-mangle-names/lib/index.js new file mode 100644 index 00000000..98d9c74f --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/index.js @@ -0,0 +1,577 @@ +"use strict"; + +function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } + +function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } + +function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } + +const Charset = require("./charset"); + +const ScopeTracker = require("./scope-tracker"); + +const isLabelIdentifier = require("./is-label-identifier"); + +const bfsTraverseCreator = require("./bfs-traverse"); + +const fixupVarScoping = require("./fixup-var-scoping"); + +const _require = require("babel-helper-mark-eval-scopes"), + markEvalScopes = _require.markEvalScopes, + isEvalScopesMarked = _require.isMarked, + hasEval = _require.hasEval; + +const newIssueUrl = "https://github.com/babel/minify/issues/new"; + +module.exports = babel => { + const t = babel.types, + traverse = babel.traverse; + const bfsTraverse = bfsTraverseCreator(babel); + const hop = Object.prototype.hasOwnProperty; + + class Mangler { + constructor(charset, program, { + exclude = {}, + keepFnName = false, + keepClassName = false, + eval: _eval = false, + topLevel = false + } = {}) { + this.charset = charset; + this.program = program; // user passed options + + this.exclude = toObject(exclude); + this.keepFnName = keepFnName; + this.keepClassName = keepClassName; + this.topLevel = topLevel; + this.eval = _eval; // tracking + + this.visitedScopes = new Set(); + this.scopeTracker = new ScopeTracker(); + this.renamedNodes = new Set(); + } + /** + * Run the mangler + */ + + + run() { + this.crawlScope(); + this.collect(); + this.fixup(); + this.charset.sort(); + this.mangle(); + } + /** + * Tells if a variable name is excluded + * @param {String} name + */ + + + isExcluded(name) { + return hop.call(this.exclude, name) && this.exclude[name]; + } + /** + * Clears traverse cache and recrawls the AST + * + * to recompute the bindings, references, other scope information + * and paths because the other transformations in the same pipeline + * (other plugins and presets) changes the AST and does NOT update + * the scope objects + */ + + + crawlScope() { + (traverse.clearCache || traverse.cache.clear)(); + this.program.scope.crawl(); + } + /** + * Re-crawling comes with a side-effect that let->var conversion + * reverts the update of the binding information (block to fn scope). + * This function takes care of it by updating it again. + * + * TODO: This is unnecessary work and needs to be fixed in babel. + * https://github.com/babel/babel/issues/4818 + * + * When this is removed, remember to remove fixup's dependency in + * ScopeTracker + */ + + + fixup() { + fixupVarScoping(this); + } + /** + * A single pass through the AST to collect info for + * + * 1. Scope Tracker + * 2. Unsafe Scopes (direct eval scopes) + * 3. Charset considerations for better gzip compression + * + * Traversed in the same fashion(BFS) the mangling is done + */ + + + collect() { + const mangler = this; + const scopeTracker = mangler.scopeTracker; + scopeTracker.addScope(this.program.scope); + /** + * Same usage as in DCE, whichever runs first + */ + + if (!isEvalScopesMarked(mangler.program)) { + markEvalScopes(mangler.program); + } + /** + * The visitors to be used in traversal. + * + * Note: BFS traversal supports only the `enter` handlers, `exit` + * handlers are simply dropped without Errors + * + * Collects items defined in the ScopeTracker + */ + + + const collectVisitor = { + Scopable({ + scope + }) { + scopeTracker.addScope(scope); // Collect bindings defined in the scope + + Object.keys(scope.bindings).forEach(name => { + scopeTracker.addBinding(scope.bindings[name]); // add all constant violations as references + + scope.bindings[name].constantViolations.forEach(() => { + scopeTracker.addReference(scope, scope.bindings[name], name); + }); + }); + }, + + /** + * 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 + * does NOT contain the references in that scope. Only the program + * scope (top most level) contains all the references. + * + * We collect the references in a fashion where all the scopes between + * and including the referenced scope and scope where it is declared + * is considered as scope referencing that identifier + */ + ReferencedIdentifier(path) { + if (isLabelIdentifier(path)) { + return; + } + + const scope = path.scope, + name = path.node.name; + const 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 + // reached + + + throw new Error(`Binding not found for ReferencedIdentifier "${name}" ` + `present in "${path.parentPath.type}". ` + `Please report this at ${newIssueUrl}`); + } else { + // Add it to our scope tracker if everything is fine + scopeTracker.addReference(scope, binding, name); + } + } + + }; + /** + * These visitors are for collecting the Characters used in the program + * to measure the frequency and generate variable names for mangling so + * as to improve the gzip compression - as gzip likes repetition + */ + + if (this.charset.shouldConsider) { + collectVisitor.Identifier = function Identifer(path) { + const 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 + })) { + mangler.charset.consider(node.name); + } + }; + + collectVisitor.Literal = function Literal({ + node + }) { + mangler.charset.consider(String(node.value)); + }; + } // Traverse the AST + + + bfsTraverse(mangler.program, collectVisitor); + } + /** + * Tells if a binding is exported as a NamedExport - so as to NOT mangle + * + * Babel treats NamedExports as a binding referenced by this NamedExport decl + * @param {Binding} binding + */ + + + isExportedWithName(binding) { + // short circuit + if (!this.topLevel) { + return false; + } + + const refs = binding.referencePaths; + + var _iterator = _createForOfIteratorHelper(refs), + _step; + + try { + for (_iterator.s(); !(_step = _iterator.n()).done;) { + const ref = _step.value; + + if (ref.isExportNamedDeclaration()) { + return true; + } + } // default + + } catch (err) { + _iterator.e(err); + } finally { + _iterator.f(); + } + + return false; + } + /** + * 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 + * @param {Scope} scope + */ + + + mangleScope(scope) { + const mangler = this; + const scopeTracker = mangler.scopeTracker; // Unsafe Scope + + if (!mangler.eval && hasEval(scope)) { + return; + } // Already visited + // This is because for a function, in Babel, the function and + // the function body's BlockStatement has the same scope, and will + // be visited twice by the Scopable handler, and we want to mangle + // it only once + + + if (mangler.visitedScopes.has(scope)) { + return; + } + + 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 counter = 0; + /** + * 1. Iterate through the list of BindingIdentifiers + * 2. Rename each of them in-place + * 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 (var _i = 0, _names = names; _i < _names.length; _i++) { + const oldName = _names[_i]; + const binding = bindings.get(oldName); + + if (mangler.canMangle(oldName, binding, scope)) { + 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 + + + if (oldName.length < 3) { + counter = 0; + } // Once we detected a valid `next` Identifier which could be used, + // call the renamer + + + mangler.rename(scope, binding, oldName, next); + } + } + } + /** + * The mangle function that traverses through all the Scopes in a BFS + * fashion - calls mangleScope + */ + + + mangle() { + const mangler = this; + bfsTraverse(this.program, { + Scopable(path) { + if (!path.isProgram() || mangler.topLevel) mangler.mangleScope(path.scope); + } + + }); + } + /** + * Given a NodePath, collects all the Identifiers which are BindingIdentifiers + * and replaces them with the new name + * + * For example, + * var a = 1, { b } = c; // a and b are BindingIdentifiers + * + * @param {NodePath} path + * @param {String} oldName + * @param {String} newName + * @param {Function} predicate + */ + + + renameBindingIds(path, oldName, newName, predicate = () => true) { + const bindingIds = path.getBindingIdentifierPaths(true, false); + + for (const name in bindingIds) { + if (name !== oldName) continue; + + var _iterator2 = _createForOfIteratorHelper(bindingIds[name]), + _step2; + + try { + for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { + const idPath = _step2.value; + + if (predicate(idPath)) { + idPath.node.name = newName; // babel-7 don't requeue + // idPath.replaceWith(t.identifier(newName)); + + this.renamedNodes.add(idPath.node); + } + } + } catch (err) { + _iterator2.e(err); + } finally { + _iterator2.f(); + } + } + } + /** + * The Renamer: + * Renames the following for one Binding in a Scope + * + * 1. Binding in that Scope + * 2. All the Binding's constant violations + * 3. All its References + * 4. Updates mangler.scopeTracker + * 5. Updates Babel's Scope tracking + * + * @param {Scope} scope + * @param {Binding} binding + * @param {String} oldName + * @param {String} newName + */ + + + rename(scope, binding, oldName, newName) { + const mangler = this; + const scopeTracker = mangler.scopeTracker; // rename at the declaration level + + this.renameBindingIds(binding.path, oldName, newName, idPath => idPath.node === binding.identifier); // update mangler's ScopeTracker + + scopeTracker.renameBinding(scope, oldName, newName); // update all constant violations + + const violations = binding.constantViolations; + + for (let 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 + + + const refs = binding.referencePaths; + + for (let i = 0; i < refs.length; i++) { + const path = refs[i]; + const node = path.node; + + if (!path.isIdentifier()) { + // Ideally, this should not happen + // it happens in these places now - + // case 1: Export Statements + // This is a bug in babel + // https://github.com/babel/babel/pull/3629 + // case 2: Replacements in other plugins + // eg: https://github.com/babel/minify/issues/122 + // replacement in dce from `x` to `!x` gives referencePath as `!x` + path.traverse({ + ReferencedIdentifier(refPath) { + if (refPath.node.name !== oldName) { + return; + } + + const 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); + 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); + scopeTracker.updateReference(path.scope, binding, oldName, newName); + } else if (mangler.renamedNodes.has(path.node)) { + // already renamed, + // just update the references + scopeTracker.updateReference(path.scope, binding, oldName, newName); + } else { + throw new Error(`Unexpected Rename Error: ` + `Trying to replace "${node.name}": from "${oldName}" to "${newName}". ` + `Please report it at ${newIssueUrl}`); + } + } // else label identifier - silently ignore + + } // update babel's internal tracking + + + binding.identifier.name = newName; // update babel's internal scope tracking + + const bindings = scope.bindings; + bindings[newName] = binding; + delete bindings[oldName]; + } + + } + + return { + name: "minify-mangle-names", + visitor: { + /** + * Mangler is run as a single pass. It's the same pattern as used in DCE + */ + Program: { + exit(path) { + // If the source code is small then we're going to assume that the user + // is running on this on single files before bundling. Therefore we + // need to achieve as much determinisim and we will not do any frequency + // sorting on the character set. Currently the number is pretty arbitrary. + const shouldConsiderSource = path.getSource().length > 70000; + const charset = new Charset(shouldConsiderSource); + const mangler = new Mangler(charset, path, this.opts); + mangler.run(); + } + + } + } + }; +}; // convert value to object + + +function toObject(value) { + if (!Array.isArray(value)) { + return value; + } + + const map = {}; + + for (let i = 0; i < value.length; i++) { + map[value[i]] = true; + } + + return map; +} // for keepFnName + + +function isFunction(path) { + return path.isFunctionExpression() || path.isFunctionDeclaration(); +} // for keepClassName + + +function isClass(path) { + return path.isClassExpression() || path.isClassDeclaration(); +}
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/is-label-identifier.js b/node_modules/babel-plugin-minify-mangle-names/lib/is-label-identifier.js new file mode 100644 index 00000000..04ccb7d8 --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/is-label-identifier.js @@ -0,0 +1,14 @@ +"use strict"; + +module.exports = isLabelIdentifier; + +function isLabelIdentifier(path) { + const node = path.node; + return path.parentPath.isLabeledStatement({ + label: node + }) || path.parentPath.isBreakStatement({ + label: node + }) || path.parentPath.isContinueStatement({ + label: node + }); +}
\ No newline at end of file diff --git a/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js b/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js new file mode 100644 index 00000000..ac51afdf --- /dev/null +++ b/node_modules/babel-plugin-minify-mangle-names/lib/scope-tracker.js @@ -0,0 +1,269 @@ +"use strict"; + +const CountedSet = require("./counted-set"); + +const isLabelIdentifier = require("./is-label-identifier"); + +const newIssueUrl = "https://github.com/babel/minify/issues/new"; +/** + * ScopeTracker + * references: Map<Scope, CountedSet<String> > + * bindings: Map<Scope, Map<String, Binding> > + */ + +module.exports = class ScopeTracker { + constructor() { + this.references = new Map(); + this.bindings = new Map(); + } + /** + * Register a new Scope and initiliaze it with empty sets + * @param {Scope} scope + */ + + + addScope(scope) { + if (!this.references.has(scope)) { + this.references.set(scope, new CountedSet()); + } + + if (!this.bindings.has(scope)) { + this.bindings.set(scope, new Map()); + } + } + /** + * Add reference to all Scopes between and including the ReferencedScope + * and Binding's Scope + * @param {Scope} scope + * @param {Binding} binding + * @param {String} name + */ + + + addReference(scope, binding, name) { + let 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); + } + /** + * has a Reference in the given {Scope} or a child Scope + * + * Refer {addReference} to know why the following call will be valid + * for detecting references in child Scopes + * + * @param {Scope} scope + * @param {String} name + */ + + + hasReference(scope, name) { + return this.references.get(scope).has(name); + } + /** + * Update reference count in all scopes between and including the + * Referenced Scope and the Binding's Scope + * + * @param {Scope} scope + * @param {Binding} binding + * @param {String} oldName + * @param {String} newName + */ + + + updateReference(scope, binding, oldName, newName) { + let parent = scope; + + do { + const ref = this.references.get(parent); + ref.delete(oldName); + ref.add(newName); + + if (!binding) { + // Something went wrong - panic + throw new Error("Binding Not Found during scopeTracker.updateRefernce " + `while updating "${oldName}" to "${newName}". ` + `Please report at ${newIssueUrl}`); + } + + if (binding.scope === parent) break; + } while (parent = parent.parent); + } + /** + * has either a Binding or a Reference + * @param {Scope} scope + * @param {Binding} binding + * @param {String} name + */ + + + hasBindingOrReference(scope, binding, name) { + return this.hasReference(scope, name) || this.hasBinding(scope, name); + } + /** + * For a Binding visit all places where the Binding is used and detect + * if the newName {next} can be used in all these places + * + * 1. binding's own scope + * 2. constant violations' scopes + * 3. referencePaths' scopes + * + * @param {Binding} binding + * @param {String} next + */ + + + canUseInReferencedScopes(binding, next) { + const tracker = this; + + if (tracker.hasBindingOrReference(binding.scope, binding, next)) { + return false; + } // Safari raises a syntax error for a `let` or `const` declaration in a + // `for` loop initialization that shadows a parent function's parameter. + // https://github.com/babel/minify/issues/559 + // https://bugs.webkit.org/show_bug.cgi?id=171041 + // https://trac.webkit.org/changeset/217200/webkit/trunk/Source + + + const maybeDecl = binding.path.parentPath; + const isBlockScoped = maybeDecl.isVariableDeclaration({ + kind: "let" + }) || maybeDecl.isVariableDeclaration({ + kind: "const" + }); + + if (isBlockScoped) { + const maybeFor = maybeDecl.parentPath; + const isForLoopBinding = maybeFor.isForStatement({ + init: maybeDecl.node + }) || maybeFor.isForXStatement({ + left: maybeDecl.node + }); + + if (isForLoopBinding) { + const fnParent = getFunctionParent(maybeFor); + + if (fnParent.isFunction({ + body: maybeFor.parent + })) { + const parentFunctionBinding = this.bindings.get(fnParent.scope).get(next); + + if (parentFunctionBinding) { + const parentFunctionHasParamBinding = parentFunctionBinding.kind === "param"; + + if (parentFunctionHasParamBinding) { + return false; + } + } + } + } + } + + for (let i = 0; i < binding.constantViolations.length; i++) { + const violation = binding.constantViolations[i]; + + if (tracker.hasBindingOrReference(violation.scope, binding, next)) { + return false; + } + } + + for (let i = 0; i < binding.referencePaths.length; i++) { + const ref = binding.referencePaths[i]; + + if (!ref.isIdentifier()) { + let canUse = true; + ref.traverse({ + ReferencedIdentifier(path) { + if (path.node.name !== next) return; + + if (tracker.hasBindingOrReference(path.scope, binding, next)) { + canUse = false; + } + } + + }); + + if (!canUse) { + return canUse; + } + } else if (!isLabelIdentifier(ref)) { + if (tracker.hasBindingOrReference(ref.scope, binding, next)) { + return false; + } + } + } + + return true; + } + /** + * Add a binding to Tracker in binding's own Scope + * @param {Binding} binding + */ + + + addBinding(binding) { + if (!binding) { + return; + } + + const bindings = this.bindings.get(binding.scope); + const existingBinding = bindings.get(binding.identifier.name); + + if (existingBinding && existingBinding !== binding) { + throw new Error(`scopeTracker.addBinding: ` + `Binding "${existingBinding.identifier.name}" already exists. ` + `Trying to add "${binding.identifier.name}" again.`); + } + + bindings.set(binding.identifier.name, binding); + } + /** + * Moves Binding from it's own Scope to {@param toScope} + * + * required for fixup-var-scope + * + * @param {Binding} binding + * @param {Scope} toScope + */ + + + moveBinding(binding, toScope) { + this.bindings.get(binding.scope).delete(binding.identifier.name); + this.bindings.get(toScope).set(binding.identifier.name, binding); + } + /** + * has a Binding in the current {Scope} + * @param {Scope} scope + * @param {String} name + */ + + + hasBinding(scope, name) { + return this.bindings.get(scope).has(name); + } + /** + * Update the ScopeTracker on rename + * @param {Scope} scope + * @param {String} oldName + * @param {String} newName + */ + + + renameBinding(scope, oldName, newName) { + const bindings = this.bindings.get(scope); + bindings.set(newName, bindings.get(oldName)); + bindings.delete(oldName); + } + +}; +/** + * 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; +}
\ No newline at end of file |
