babel-plugin-minify-dead-code-elimination
Advanced tools
Comparing version 0.0.4 to 0.1.0
128
lib/index.js
"use strict"; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -10,4 +10,4 @@ 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); } } | ||
module.exports = function (_ref) { | ||
var t = _ref.types; | ||
var traverse = _ref.traverse; | ||
var t = _ref.types, | ||
traverse = _ref.traverse; | ||
@@ -17,2 +17,7 @@ var removeOrVoid = require("babel-helper-remove-or-void")(t); | ||
// this is used for tracking fn params that can be removed | ||
// as traversal takes place from left and | ||
// unused params can be removed only on the right | ||
var markForRemoval = Symbol("markForRemoval"); | ||
var main = { | ||
@@ -40,4 +45,4 @@ // remove side effectless statement | ||
var node = path.node; | ||
var scope = path.scope; | ||
var node = path.node, | ||
scope = path.scope; | ||
@@ -182,9 +187,54 @@ var seen = new Set(); | ||
// if the scope is created by a function, we obtain its | ||
// parameter list | ||
var paramsList = path.isFunction() ? path.get("params") : []; | ||
for (var i = paramsList.length - 1; i >= 0; i--) { | ||
var param = paramsList[i]; | ||
if (param.isIdentifier()) { | ||
var binding = scope.bindings[param.node.name]; | ||
if (binding.referenced) { | ||
// when the first binding is referenced (right to left) | ||
// exit without marking anything after this | ||
break; | ||
} | ||
binding[markForRemoval] = true; | ||
continue; | ||
} else if (param.isAssignmentPattern()) { | ||
var left = param.get("left"); | ||
var right = param.get("right"); | ||
if (left.isIdentifier() && right.isPure()) { | ||
var _binding = scope.bindings[left.node.name]; | ||
if (_binding.referenced) { | ||
// when the first binding is referenced (right to left) | ||
// exit without marking anything after this | ||
break; | ||
} | ||
_binding[markForRemoval] = true; | ||
continue; | ||
} | ||
} | ||
// other patterns - assignment, object have side-effects | ||
// and cannot be safely removed | ||
break; | ||
} | ||
var _loop3 = function _loop3(name) { | ||
var binding = scope.bindings[name]; | ||
if (!binding.referenced && binding.kind !== "param" && binding.kind !== "module") { | ||
if (!binding.referenced && binding.kind !== "module") { | ||
var _ret4 = function () { | ||
if (binding.path.isVariableDeclarator()) { | ||
if (binding.path.parentPath.parentPath && binding.path.parentPath.parentPath.isForInStatement()) { | ||
// Can't remove if in a for in statement `for (var x in wat)`. | ||
if (binding.kind === "param" && (_this.keepFnArgs || !binding[markForRemoval])) { | ||
return { | ||
v: "continue" | ||
}; | ||
} else if (binding.path.isVariableDeclarator()) { | ||
if (binding.path.parentPath.parentPath && binding.path.parentPath.parentPath.isForXStatement()) { | ||
// Can't remove if in a for-in/for-of/for-await statement `for (var x in wat)`. | ||
return { | ||
@@ -195,2 +245,3 @@ v: "continue" | ||
} else if (!scope.isPure(binding.path.node)) { | ||
// TODO: AssignmentPattern are marked as impure and unused ids aren't removed yet | ||
return { | ||
@@ -241,2 +292,8 @@ v: "continue" | ||
} | ||
// Bail out for ArrayPattern and ObjectPattern | ||
if (!binding.path.get("id").isIdentifier()) { | ||
return { | ||
v: "continue" | ||
}; | ||
} | ||
@@ -719,3 +776,3 @@ binding.path.parentPath.replaceWith(binding.path.node.init); | ||
"FunctionExpression|ClassExpression": function FunctionExpressionClassExpression(path) { | ||
if (!this.keepFnames) { | ||
if (!this.keepFnName) { | ||
removeUnreferencedId(path); | ||
@@ -770,10 +827,12 @@ } | ||
Program: function Program(path) { | ||
var _ref4 = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var _ref4 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref4$opts = _ref4.opts; | ||
var _ref4$opts = _ref4.opts; | ||
_ref4$opts = _ref4$opts === undefined ? {} : _ref4$opts; | ||
var _ref4$opts$optimizeRa = _ref4$opts.optimizeRawSize; | ||
var optimizeRawSize = _ref4$opts$optimizeRa === undefined ? false : _ref4$opts$optimizeRa; | ||
var _ref4$opts$keepFnames = _ref4$opts.keepFnames; | ||
var keepFnames = _ref4$opts$keepFnames === undefined ? false : _ref4$opts$keepFnames; | ||
var _ref4$opts$optimizeRa = _ref4$opts.optimizeRawSize, | ||
optimizeRawSize = _ref4$opts$optimizeRa === undefined ? false : _ref4$opts$optimizeRa, | ||
_ref4$opts$keepFnName = _ref4$opts.keepFnName, | ||
keepFnName = _ref4$opts$keepFnName === undefined ? false : _ref4$opts$keepFnName, | ||
_ref4$opts$keepFnArgs = _ref4$opts.keepFnArgs, | ||
keepFnArgs = _ref4$opts$keepFnArgs === undefined ? false : _ref4$opts$keepFnArgs; | ||
@@ -784,3 +843,4 @@ // We need to run this plugin in isolation. | ||
optimizeRawSize: optimizeRawSize, | ||
keepFnames: keepFnames | ||
keepFnName: keepFnName, | ||
keepFnArgs: keepFnArgs | ||
}); | ||
@@ -885,5 +945,5 @@ } | ||
function replace(path, options) { | ||
var replacement = options.replacement; | ||
var scope = options.scope; | ||
var binding = options.binding; | ||
var replacement = options.replacement, | ||
scope = options.scope, | ||
binding = options.binding; | ||
@@ -958,4 +1018,4 @@ // Same name, different binding. | ||
ReferencedIdentifier: function ReferencedIdentifier(path) { | ||
var node = path.node; | ||
var scope = path.scope; | ||
var node = path.node, | ||
scope = path.scope; | ||
@@ -991,4 +1051,4 @@ var binding = scope.getBinding(node.name); | ||
var node = path.node; | ||
var scope = path.scope; | ||
var node = path.node, | ||
scope = path.scope; | ||
@@ -1059,3 +1119,9 @@ var binding = scope.getBinding(id.name); | ||
// if they are within the case, we keep them | ||
var _isAncestor = isAncestor(path.scope.getBinding(label.node.name).path, path); | ||
var labelPath = void 0; | ||
if (path.scope.getLabel) { | ||
labelPath = getLabel(label.node.name, path); | ||
} else { | ||
labelPath = path.scope.getBinding(label.node.name).path; | ||
} | ||
var _isAncestor = isAncestor(labelPath, path); | ||
@@ -1121,2 +1187,14 @@ return { | ||
} | ||
function getLabel(name, _path) { | ||
var label = void 0, | ||
path = _path; | ||
do { | ||
label = path.scope.getLabel(name); | ||
if (label) { | ||
return label; | ||
} | ||
} while (path = path.parentPath); | ||
return null; | ||
} | ||
}; |
{ | ||
"name": "babel-plugin-minify-dead-code-elimination", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/babel/babili#readme", |
@@ -70,2 +70,3 @@ # babel-plugin-minify-dead-code-elimination | ||
+ `keepFnames` - prevent plugin from removing function names. Useful for code depending on `fn.name` | ||
+ `keepFnName` - prevent plugin from removing function name. Useful for code depending on `fn.name` | ||
+ `keepFnArgs` - prevent plugin from removing function args. Useful for code depending on `fn.length` |
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
39336
999
72