babel-plugin-minify-dead-code-elimination
Advanced tools
Comparing version 0.5.0-alpha.7b176463 to 0.5.0-alpha.7ba5821f
630
lib/index.js
"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); } } | ||
const some = require("lodash.some"); | ||
var some = require("lodash.some"); | ||
const _require = require("babel-helper-mark-eval-scopes"), | ||
markEvalScopes = _require.markEvalScopes, | ||
hasEval = _require.hasEval; | ||
var _require = require("babel-helper-mark-eval-scopes"), | ||
markEvalScopes = _require.markEvalScopes, | ||
hasEval = _require.hasEval; | ||
const removeUseStrict = require("./remove-use-strict"); | ||
var removeUseStrict = require("./remove-use-strict"); | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
var evaluate = require("babel-helper-evaluate-path"); | ||
function evaluateTruthy(path) { | ||
const res = evaluate(path); | ||
if (res.confident) return !!res.value; | ||
} | ||
function prevSiblings(path) { | ||
var parentPath = path.parentPath; | ||
var siblings = []; | ||
var key = parentPath.key; | ||
const parentPath = path.parentPath; | ||
const siblings = []; | ||
let key = parentPath.key; | ||
@@ -33,14 +36,14 @@ while ((path = parentPath.getSibling(--key)).type) { | ||
module.exports = function (_ref) { | ||
var t = _ref.types, | ||
traverse = _ref.traverse; | ||
module.exports = ({ | ||
types: t, | ||
traverse | ||
}) => { | ||
const removeOrVoid = require("babel-helper-remove-or-void")(t); | ||
var removeOrVoid = require("babel-helper-remove-or-void")(t); | ||
var shouldRevisit = Symbol("shouldRevisit"); // this is used for tracking fn params that can be removed | ||
const shouldRevisit = Symbol("shouldRevisit"); // 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 = { | ||
const markForRemoval = Symbol("markForRemoval"); | ||
const main = { | ||
// remove side effectless statement | ||
@@ -65,19 +68,19 @@ ExpressionStatement(path) { | ||
var node = path.node, | ||
scope = path.scope; | ||
var seen = new Set(); | ||
var declars = []; | ||
var mutations = []; | ||
const node = path.node, | ||
scope = path.scope; | ||
const seen = new Set(); | ||
const declars = []; | ||
const mutations = []; | ||
var _loop = function _loop(name) { | ||
var binding = scope.bindings[name]; | ||
for (const name in scope.bindings) { | ||
const binding = scope.bindings[name]; | ||
if (!binding.path.isVariableDeclarator()) { | ||
return "continue"; | ||
continue; | ||
} | ||
var declarPath = binding.path.parentPath; | ||
const declarPath = binding.path.parentPath; | ||
if (seen.has(declarPath)) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -88,47 +91,41 @@ | ||
if (declarPath.parentPath.isForInStatement()) { | ||
return "continue"; | ||
continue; | ||
} | ||
if (declarPath.parentPath.parentPath.isFunction()) { | ||
return "continue"; | ||
continue; | ||
} | ||
if (!declarPath.node || !declarPath.node.declarations) { | ||
return "continue"; | ||
continue; | ||
} | ||
var assignmentSequence = []; | ||
const assignmentSequence = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
var _loop2 = function _loop2(declar) { | ||
declars.push(declar); | ||
if (declar.init) { | ||
assignmentSequence.push(t.assignmentExpression("=", declar.id, declar.init)); | ||
mutations.push(function () { | ||
declar.init = null; | ||
}); | ||
} | ||
}; | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = declarPath.node.declarations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var declar = _step2.value; | ||
for (var _iterator = declarPath.node.declarations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const declar = _step.value; | ||
declars.push(declar); | ||
_loop2(declar); | ||
if (declar.init) { | ||
assignmentSequence.push(t.assignmentExpression("=", declar.id, declar.init)); | ||
mutations.push(() => { | ||
declar.init = null; | ||
}); | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
@@ -139,35 +136,20 @@ } | ||
if (assignmentSequence.length) { | ||
mutations.push(function () { | ||
return declarPath.replaceWith(t.sequenceExpression(assignmentSequence)); | ||
}); | ||
mutations.push(() => declarPath.replaceWith(t.sequenceExpression(assignmentSequence))); | ||
} else { | ||
mutations.push(function () { | ||
return removeOrVoid(declarPath); | ||
}); | ||
mutations.push(() => removeOrVoid(declarPath)); | ||
} | ||
}; | ||
for (var name in scope.bindings) { | ||
var _ret = _loop(name); | ||
if (_ret === "continue") continue; | ||
} | ||
if (declars.length) { | ||
mutations.forEach(function (f) { | ||
return f(); | ||
}); | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
mutations.forEach(f => f()); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator = node.body.body[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _statement = _step.value; | ||
for (var _iterator2 = node.body.body[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
const statement = _step2.value; | ||
if (t.isVariableDeclaration(_statement)) { | ||
var _statement$declaratio; | ||
(_statement$declaratio = _statement.declarations).push.apply(_statement$declaratio, declars); | ||
if (t.isVariableDeclaration(statement)) { | ||
statement.declarations.push(...declars); | ||
return; | ||
@@ -177,12 +159,12 @@ } | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
@@ -192,3 +174,3 @@ } | ||
var varDecl = t.variableDeclaration("var", declars); | ||
const varDecl = t.variableDeclaration("var", declars); | ||
node.body.body.unshift(varDecl); | ||
@@ -209,4 +191,2 @@ } | ||
enter(path) { | ||
var _this = this; | ||
if (path.isProgram()) { | ||
@@ -220,13 +200,13 @@ return; | ||
var scope = path.scope; // if the scope is created by a function, we obtain its | ||
const scope = path.scope; // if the scope is created by a function, we obtain its | ||
// parameter list | ||
var canRemoveParams = path.isFunction() && path.node.kind !== "set"; | ||
var paramsList = canRemoveParams ? path.get("params") : []; | ||
const canRemoveParams = path.isFunction() && path.node.kind !== "set"; | ||
const paramsList = canRemoveParams ? path.get("params") : []; | ||
for (var i = paramsList.length - 1; i >= 0; i--) { | ||
var param = paramsList[i]; | ||
for (let i = paramsList.length - 1; i >= 0; i--) { | ||
const param = paramsList[i]; | ||
if (param.isIdentifier()) { | ||
var binding = scope.bindings[param.node.name]; | ||
const binding = scope.bindings[param.node.name]; | ||
if (!binding) continue; | ||
@@ -243,9 +223,9 @@ | ||
} else if (param.isAssignmentPattern()) { | ||
var left = param.get("left"); | ||
var right = param.get("right"); | ||
const left = param.get("left"); | ||
const right = param.get("right"); | ||
if (left.isIdentifier() && right.isPure()) { | ||
var _binding = scope.bindings[left.node.name]; | ||
const binding = scope.bindings[left.node.name]; | ||
if (_binding.referenced) { | ||
if (binding.referenced) { | ||
// when the first binding is referenced (right to left) | ||
@@ -256,3 +236,3 @@ // exit without marking anything after this | ||
_binding[markForRemoval] = true; | ||
binding[markForRemoval] = true; | ||
continue; | ||
@@ -267,11 +247,11 @@ } | ||
var _loop3 = function _loop3(name) { | ||
var binding = scope.bindings[name]; | ||
for (const name in scope.bindings) { | ||
const binding = scope.bindings[name]; | ||
if (!binding.referenced && binding.kind !== "module") { | ||
if (binding.kind === "param" && (_this.keepFnArgs || !binding[markForRemoval])) { | ||
return "continue"; | ||
if (binding.kind === "param" && (this.keepFnArgs || !binding[markForRemoval])) { | ||
continue; | ||
} else if (binding.path.isVariableDeclarator()) { | ||
var declaration = binding.path.parentPath; | ||
var maybeBlockParent = declaration.parentPath; | ||
const declaration = binding.path.parentPath; | ||
const maybeBlockParent = declaration.parentPath; | ||
@@ -282,10 +262,10 @@ if (maybeBlockParent && maybeBlockParent.isForXStatement({ | ||
// Can't remove if in a for-in/for-of/for-await statement `for (var x in wat)`. | ||
return "continue"; | ||
continue; | ||
} | ||
} else if (!scope.isPure(binding.path.node)) { | ||
// TODO: AssignmentPattern are marked as impure and unused ids aren't removed yet | ||
return "continue"; | ||
continue; | ||
} else if (binding.path.isFunctionExpression() || binding.path.isClassExpression()) { | ||
// `bar(function foo() {})` foo is not referenced but it's used. | ||
return "continue"; | ||
continue; | ||
} else if ( // ClassDeclaration has binding in two scopes | ||
@@ -295,9 +275,9 @@ // 1. The scope in which it is declared | ||
binding.path.isClassDeclaration() && binding.path === scope.path) { | ||
return "continue"; | ||
continue; | ||
} | ||
var mutations = []; | ||
var bail = false; // Make sure none of the assignments value is used | ||
const mutations = []; | ||
let bail = false; // Make sure none of the assignments value is used | ||
binding.constantViolations.forEach(function (p) { | ||
binding.constantViolations.forEach(p => { | ||
if (bail || p === binding.path) { | ||
@@ -312,9 +292,5 @@ return; | ||
if (p.isAssignmentExpression() && !p.get("right").isPure()) { | ||
mutations.push(function () { | ||
return p.replaceWith(p.get("right")); | ||
}); | ||
mutations.push(() => p.replaceWith(p.get("right"))); | ||
} else { | ||
mutations.push(function () { | ||
return removeOrVoid(p); | ||
}); | ||
mutations.push(() => removeOrVoid(p)); | ||
} | ||
@@ -324,3 +300,3 @@ }); | ||
if (bail) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -331,3 +307,3 @@ | ||
// deopt for object and array pattern | ||
return "continue"; | ||
continue; | ||
} // if declarator has some impure init expression | ||
@@ -341,3 +317,3 @@ // var x = foo(); | ||
if (binding.path.parentPath.node.declarations.length !== 1) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -347,73 +323,63 @@ | ||
} else { | ||
updateReferences(binding.path, _this); | ||
updateReferences(binding.path, this); | ||
removeOrVoid(binding.path); | ||
} | ||
} else { | ||
updateReferences(binding.path, _this); | ||
updateReferences(binding.path, this); | ||
removeOrVoid(binding.path); | ||
} | ||
mutations.forEach(function (f) { | ||
return f(); | ||
}); | ||
mutations.forEach(f => f()); | ||
scope.removeBinding(name); | ||
} else if (binding.constant) { | ||
if (binding.path.isFunctionDeclaration() || binding.path.isVariableDeclarator() && binding.path.get("init").isFunction()) { | ||
var _ret3 = function () { | ||
var fun = binding.path.isFunctionDeclaration() ? binding.path : binding.path.get("init"); | ||
var allInside = true; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
const fun = binding.path.isFunctionDeclaration() ? binding.path : binding.path.get("init"); | ||
let allInside = true; | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = binding.referencePaths[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _ref2 = _step3.value; | ||
try { | ||
for (var _iterator3 = binding.referencePaths[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
const ref = _step3.value; | ||
if (!_ref2.find(function (p) { | ||
return p.node === fun.node; | ||
})) { | ||
allInside = false; | ||
break; | ||
} | ||
if (!ref.find(p => p.node === fun.node)) { | ||
allInside = false; | ||
break; | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return != null) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return != null) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
if (allInside) { | ||
scope.removeBinding(name); | ||
updateReferences(binding.path, _this); | ||
removeOrVoid(binding.path); | ||
return { | ||
v: "continue" | ||
}; | ||
} | ||
}(); | ||
if (typeof _ret3 === "object") return _ret3.v; | ||
if (allInside) { | ||
scope.removeBinding(name); | ||
updateReferences(binding.path, this); | ||
removeOrVoid(binding.path); | ||
continue; | ||
} | ||
} | ||
if (binding.references === 1 && binding.kind !== "param" && binding.kind !== "module" && binding.constant) { | ||
var replacement = binding.path.node; | ||
var replacementPath = binding.path; | ||
var isReferencedBefore = false; | ||
var refPath = binding.referencePaths[0]; | ||
let replacement = binding.path.node; | ||
let replacementPath = binding.path; | ||
let isReferencedBefore = false; | ||
const refPath = binding.referencePaths[0]; | ||
if (t.isVariableDeclarator(replacement)) { | ||
var _prevSiblings = prevSiblings(replacementPath); // traverse ancestors of a reference checking if it's before declaration | ||
const _prevSiblings = prevSiblings(replacementPath); // traverse ancestors of a reference checking if it's before declaration | ||
forEachAncestor(refPath, function (ancestor) { | ||
forEachAncestor(refPath, ancestor => { | ||
if (_prevSiblings.indexOf(ancestor) > -1) { | ||
@@ -427,3 +393,3 @@ isReferencedBefore = true; | ||
if (isReferencedBefore && refPath.scope !== binding.scope) { | ||
return "continue"; | ||
continue; | ||
} // simulate hoisting by replacing value | ||
@@ -437,3 +403,3 @@ // with undefined if declaration is after reference | ||
if (!replacementPath.get("id").isIdentifier()) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -445,13 +411,13 @@ | ||
if (!replacement) { | ||
return "continue"; | ||
continue; | ||
} | ||
if (!scope.isPure(replacement, true) && !isReferencedBefore) { | ||
return "continue"; | ||
continue; | ||
} | ||
var _bail = false; | ||
let bail = false; | ||
if (replacementPath.isIdentifier()) { | ||
var _binding2 = scope.getBinding(replacement.name); // the reference should be in the same scope | ||
const binding = scope.getBinding(replacement.name); // the reference should be in the same scope | ||
// and the replacement should be a constant - this is to | ||
@@ -461,4 +427,3 @@ // ensure that the duplication of replacement is not affected | ||
_bail = !(_binding2 && refPath.scope.getBinding(replacement.name) === _binding2 && _binding2.constantViolations.length === 0); | ||
bail = !(binding && refPath.scope.getBinding(replacement.name) === binding && binding.constantViolations.length === 0); | ||
} else { | ||
@@ -470,13 +435,13 @@ replacementPath.traverse({ | ||
ReferencedIdentifier(_ref3) { | ||
var node = _ref3.node; | ||
if (_bail) { | ||
ReferencedIdentifier({ | ||
node | ||
}) { | ||
if (bail) { | ||
return; | ||
} | ||
var binding = scope.getBinding(node.name); | ||
const binding = scope.getBinding(node.name); | ||
if (binding && refPath.scope.getBinding(node.name) === binding) { | ||
_bail = binding.constantViolations.length > 0; | ||
bail = binding.constantViolations.length > 0; | ||
} | ||
@@ -488,7 +453,7 @@ } | ||
if (_bail) { | ||
return "continue"; | ||
if (bail) { | ||
continue; | ||
} | ||
var parent = binding.path.parent; | ||
let parent = binding.path.parent; | ||
@@ -504,6 +469,6 @@ if (t.isVariableDeclaration(parent)) { | ||
var mayLoop = false; | ||
var sharesRoot = refPath.find(function (_ref4) { | ||
var node = _ref4.node; | ||
let mayLoop = false; | ||
const sharesRoot = refPath.find(({ | ||
node | ||
}) => { | ||
if (!mayLoop) { | ||
@@ -516,10 +481,8 @@ mayLoop = t.isWhileStatement(node) || t.isFor(node) || t.isFunction(node); | ||
var isObj = function isObj(n) { | ||
return t.isFunction(n) || t.isObjectExpression(n) || t.isArrayExpression(n); | ||
}; | ||
const isObj = n => t.isFunction(n) || t.isObjectExpression(n) || t.isArrayExpression(n); | ||
var isReplacementObj = isObj(replacement) || some(replacement, isObj); | ||
const isReplacementObj = isObj(replacement) || some(replacement, isObj); | ||
if (!sharesRoot || isReplacementObj && mayLoop) { | ||
return "continue"; | ||
continue; | ||
} // check if it's safe to replace | ||
@@ -534,3 +497,3 @@ // To solve https://github.com/babel/minify/issues/691 | ||
var inExpression = replacementPath.isBinaryExpression({ | ||
let inExpression = replacementPath.isBinaryExpression({ | ||
operator: "in" | ||
@@ -556,6 +519,6 @@ }); | ||
if (inExpression) { | ||
return "continue"; | ||
continue; | ||
} | ||
var replaced = replace(binding.referencePaths[0], { | ||
const replaced = replace(binding.referencePaths[0], { | ||
binding, | ||
@@ -576,8 +539,2 @@ scope, | ||
} | ||
}; | ||
for (var name in scope.bindings) { | ||
var _ret2 = _loop3(name); | ||
if (_ret2 === "continue") continue; | ||
} // end-for-of | ||
@@ -591,7 +548,7 @@ | ||
BlockStatement(path) { | ||
var paths = path.get("body"); | ||
var purge = false; | ||
const paths = path.get("body"); | ||
let purge = false; | ||
for (var i = 0; i < paths.length; i++) { | ||
var p = paths[i]; | ||
for (let i = 0; i < paths.length; i++) { | ||
const p = paths[i]; | ||
@@ -612,3 +569,3 @@ if (!purge && p.isCompletionStatement()) { | ||
ReturnStatement(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -633,4 +590,4 @@ if (!path.inList) { | ||
var noNext = true; | ||
var parentPath = path.parentPath; | ||
let noNext = true; | ||
let parentPath = path.parentPath; | ||
@@ -644,3 +601,3 @@ while (parentPath && !parentPath.isFunction() && noNext) { | ||
var nextPath = parentPath.getSibling(parentPath.key + 1); | ||
const nextPath = parentPath.getSibling(parentPath.key + 1); | ||
@@ -672,4 +629,4 @@ if (nextPath.node) { | ||
ConditionalExpression(path) { | ||
var node = path.node; | ||
var evaluateTest = path.get("test").evaluateTruthy(); | ||
const node = path.node; | ||
const evaluateTest = evaluateTruthy(path.get("test")); | ||
@@ -685,13 +642,13 @@ if (evaluateTest === true) { | ||
exit(path) { | ||
var evaluated = evaluate(path.get("discriminant"), { | ||
const evaluated = evaluate(path.get("discriminant"), { | ||
tdz: this.tdz | ||
}); | ||
if (!evaluated.confident) return; | ||
var discriminant = evaluated.value; | ||
var cases = path.get("cases"); | ||
var matchingCaseIndex = -1; | ||
var defaultCaseIndex = -1; | ||
const discriminant = evaluated.value; | ||
const cases = path.get("cases"); | ||
let matchingCaseIndex = -1; | ||
let defaultCaseIndex = -1; | ||
for (var i = 0; i < cases.length; i++) { | ||
var test = cases[i].get("test"); // handle default case | ||
for (let i = 0; i < cases.length; i++) { | ||
const test = cases[i].get("test"); // handle default case | ||
@@ -703,3 +660,3 @@ if (test.node === null) { | ||
var testResult = evaluate(test, { | ||
const testResult = evaluate(test, { | ||
tdz: this.tdz | ||
@@ -717,3 +674,3 @@ }); // if we are not able to deternine a test during | ||
var result; | ||
let result; | ||
@@ -736,6 +693,6 @@ if (matchingCaseIndex === -1) { | ||
replaceSwitch(_toConsumableArray(extractVars(path)).concat(_toConsumableArray(result.statements))); | ||
replaceSwitch([...extractVars(path), ...result.statements]); | ||
function getStatementsUntilBreak(start) { | ||
var result = { | ||
const result = { | ||
bail: false, | ||
@@ -745,7 +702,7 @@ statements: [] | ||
for (var _i = start; _i < cases.length; _i++) { | ||
var consequent = cases[_i].get("consequent"); | ||
for (let i = start; i < cases.length; i++) { | ||
const consequent = cases[i].get("consequent"); | ||
for (var j = 0; j < consequent.length; j++) { | ||
var _isBreaking = isBreaking(consequent[j], path); | ||
for (let j = 0; j < consequent.length; j++) { | ||
const _isBreaking = isBreaking(consequent[j], path); | ||
@@ -771,6 +728,6 @@ if (_isBreaking.bail) { | ||
function replaceSwitch(statements) { | ||
var isBlockRequired = false; | ||
let isBlockRequired = false; | ||
for (var _i2 = 0; _i2 < statements.length; _i2++) { | ||
if (t.isVariableDeclaration(statements[_i2], { | ||
for (let i = 0; i < statements.length; i++) { | ||
if (t.isVariableDeclaration(statements[i], { | ||
kind: "let" | ||
@@ -782,3 +739,3 @@ })) { | ||
if (t.isVariableDeclaration(statements[_i2], { | ||
if (t.isVariableDeclaration(statements[i], { | ||
kind: "const" | ||
@@ -802,4 +759,4 @@ })) { | ||
WhileStatement(path) { | ||
var test = path.get("test"); | ||
var result = evaluate(test, { | ||
const test = path.get("test"); | ||
const result = evaluate(test, { | ||
tdz: this.tdz | ||
@@ -814,5 +771,5 @@ }); | ||
ForStatement(path) { | ||
var test = path.get("test"); | ||
const test = path.get("test"); | ||
if (!test.isPure()) return; | ||
var result = evaluate(test, { | ||
const result = evaluate(test, { | ||
tdz: this.tdz | ||
@@ -825,3 +782,9 @@ }); | ||
} else { | ||
path.remove(); | ||
const init = path.get("init"); | ||
if (init.node && !init.isPure()) { | ||
path.replaceWith(init); | ||
} else { | ||
path.remove(); | ||
} | ||
} | ||
@@ -832,4 +795,4 @@ } | ||
DoWhileStatement(path) { | ||
var test = path.get("test"); | ||
var result = evaluate(test, { | ||
const test = path.get("test"); | ||
const result = evaluate(test, { | ||
tdz: this.tdz | ||
@@ -839,6 +802,6 @@ }); | ||
if (result.confident && test.isPure() && !result.value) { | ||
var body = path.get("body"); | ||
const body = path.get("body"); | ||
if (body.isBlockStatement()) { | ||
var stmts = body.get("body"); | ||
const stmts = body.get("body"); | ||
var _iteratorNormalCompletion4 = true; | ||
@@ -850,9 +813,9 @@ var _didIteratorError4 = false; | ||
for (var _iterator4 = stmts[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var _stmt = _step4.value; | ||
const stmt = _step4.value; | ||
var _isBreaking = isBreaking(_stmt, path); | ||
const _isBreaking = isBreaking(stmt, path); | ||
if (_isBreaking.bail || _isBreaking.break) return; | ||
var _isContinuing = isContinuing(_stmt, path); | ||
const _isContinuing = isContinuing(stmt, path); | ||
@@ -878,6 +841,6 @@ if (_isContinuing.bail || isContinuing.continue) return; | ||
} else if (body.isBreakStatement()) { | ||
var _isBreaking2 = isBreaking(body, path); | ||
const _isBreaking = isBreaking(body, path); | ||
if (_isBreaking2.bail) return; | ||
if (_isBreaking2.break) path.remove(); | ||
if (_isBreaking.bail) return; | ||
if (_isBreaking.break) path.remove(); | ||
} else if (body.isContinueStatement()) { | ||
@@ -898,3 +861,3 @@ return; | ||
var prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
const prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
@@ -905,3 +868,3 @@ if (!(prev && prev.isVariableDeclaration())) { | ||
var declars = prev.node.declarations; | ||
const declars = prev.node.declarations; | ||
@@ -933,3 +896,3 @@ if (declars.length !== 1 || declars[0].init || declars[0].id.name !== path.get("left").node.name) { | ||
ForInStatement(path) { | ||
var left = path.get("left"); | ||
const left = path.get("left"); | ||
@@ -940,3 +903,3 @@ if (!left.isIdentifier()) { | ||
var binding = path.scope.getBinding(left.node.name); | ||
const binding = path.scope.getBinding(left.node.name); | ||
@@ -986,3 +949,3 @@ if (!binding) { | ||
*/ | ||
var body = path.get("body"); | ||
const body = path.get("body"); | ||
@@ -996,15 +959,15 @@ if (body.isBlockStatement()) { | ||
IfStatement: { | ||
exit(path, _ref5) { | ||
var _ref5$opts = _ref5.opts; | ||
_ref5$opts = _ref5$opts === void 0 ? {} : _ref5$opts; | ||
var _ref5$opts$tdz = _ref5$opts.tdz, | ||
tdz = _ref5$opts$tdz === void 0 ? false : _ref5$opts$tdz; | ||
var consequent = path.get("consequent"); | ||
var alternate = path.get("alternate"); | ||
var test = path.get("test"); | ||
var evalResult = evaluate(test, { | ||
exit(path, { | ||
opts: { | ||
tdz = false | ||
} = {} | ||
}) { | ||
const consequent = path.get("consequent"); | ||
const alternate = path.get("alternate"); | ||
const test = path.get("test"); | ||
const evalResult = evaluate(test, { | ||
tdz | ||
}); | ||
var isPure = test.isPure(); | ||
var replacements = []; | ||
const isPure = test.isPure(); | ||
const replacements = []; | ||
@@ -1022,3 +985,3 @@ if (evalResult.confident && !isPure && test.isSequenceExpression()) { | ||
if (evalResult.confident && evalResult.value) { | ||
path.replaceWithMultiple(replacements.concat(_toConsumableArray(toStatements(consequent)), _toConsumableArray(extractVars(alternate)))); | ||
path.replaceWithMultiple([...replacements, ...toStatements(consequent), ...extractVars(alternate)]); | ||
return; | ||
@@ -1035,6 +998,6 @@ } // we can check if a test will be falsy 100% and if so we can inline the | ||
if (alternate.node) { | ||
path.replaceWithMultiple(replacements.concat(_toConsumableArray(toStatements(alternate)), _toConsumableArray(extractVars(consequent)))); | ||
path.replaceWithMultiple([...replacements, ...toStatements(alternate), ...extractVars(consequent)]); | ||
return; | ||
} else { | ||
path.replaceWithMultiple(replacements.concat(_toConsumableArray(extractVars(consequent)))); | ||
path.replaceWithMultiple([...replacements, ...extractVars(consequent)]); | ||
} | ||
@@ -1076,17 +1039,12 @@ } // remove alternate blocks that are empty | ||
Program: { | ||
exit(path) { | ||
var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref6$opts = _ref6.opts; | ||
_ref6$opts = _ref6$opts === void 0 ? {} : _ref6$opts; | ||
var _ref6$opts$optimizeRa = _ref6$opts.optimizeRawSize, | ||
optimizeRawSize = _ref6$opts$optimizeRa === void 0 ? false : _ref6$opts$optimizeRa, | ||
_ref6$opts$keepFnName = _ref6$opts.keepFnName, | ||
keepFnName = _ref6$opts$keepFnName === void 0 ? false : _ref6$opts$keepFnName, | ||
_ref6$opts$keepClassN = _ref6$opts.keepClassName, | ||
keepClassName = _ref6$opts$keepClassN === void 0 ? false : _ref6$opts$keepClassN, | ||
_ref6$opts$keepFnArgs = _ref6$opts.keepFnArgs, | ||
keepFnArgs = _ref6$opts$keepFnArgs === void 0 ? false : _ref6$opts$keepFnArgs, | ||
_ref6$opts$tdz = _ref6$opts.tdz, | ||
tdz = _ref6$opts$tdz === void 0 ? false : _ref6$opts$tdz; | ||
exit(path, { | ||
opts: { | ||
// set defaults | ||
optimizeRawSize = false, | ||
keepFnName = false, | ||
keepClassName = false, | ||
keepFnArgs = false, | ||
tdz = false | ||
} = {} | ||
} = {}) { | ||
(traverse.clearCache || traverse.cache.clear)(); | ||
@@ -1111,9 +1069,9 @@ path.scope.crawl(); | ||
function toStatements(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
if (path.isBlockStatement()) { | ||
var hasBlockScoped = false; | ||
let hasBlockScoped = false; | ||
for (var i = 0; i < node.body.length; i++) { | ||
var bodyNode = node.body[i]; | ||
for (let i = 0; i < node.body.length; i++) { | ||
const bodyNode = node.body[i]; | ||
@@ -1140,3 +1098,3 @@ if (t.isBlockScoped(bodyNode)) { | ||
function extractVars(path) { | ||
var declarators = []; | ||
const declarators = []; | ||
@@ -1152,7 +1110,5 @@ if (path.isVariableDeclaration({ | ||
for (var _iterator5 = path.node.declarations[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
var _decl = _step5.value; | ||
var bindingIds = Object.keys(t.getBindingIdentifiers(_decl.id)); | ||
declarators.push.apply(declarators, _toConsumableArray(bindingIds.map(function (name) { | ||
return t.variableDeclarator(t.identifier(name)); | ||
}))); | ||
const decl = _step5.value; | ||
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id)); | ||
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))); | ||
} | ||
@@ -1186,9 +1142,5 @@ } catch (err) { | ||
for (var _iterator6 = varPath.node.declarations[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) { | ||
var _decl3 = _step6.value; | ||
var _bindingIds2 = Object.keys(t.getBindingIdentifiers(_decl3.id)); | ||
declarators.push.apply(declarators, _toConsumableArray(_bindingIds2.map(function (name) { | ||
return t.variableDeclarator(t.identifier(name)); | ||
}))); | ||
const decl = _step6.value; | ||
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id)); | ||
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))); | ||
} | ||
@@ -1219,6 +1171,6 @@ } catch (err) { | ||
function replace(path, options) { | ||
var replacement = options.replacement, | ||
replacementPath = options.replacementPath, | ||
scope = options.scope, | ||
binding = options.binding; // Same name, different binding. | ||
const replacement = options.replacement, | ||
replacementPath = options.replacementPath, | ||
scope = options.scope, | ||
binding = options.binding; // Same name, different binding. | ||
@@ -1237,3 +1189,3 @@ if (scope.getBinding(path.node.name) !== binding) { | ||
var bail = false; | ||
let bail = false; | ||
traverse(replacement, { | ||
@@ -1257,6 +1209,5 @@ Function(path) { | ||
if (path.find(function (_ref7) { | ||
var node = _ref7.node; | ||
return node === replacement; | ||
})) { | ||
if (path.find(({ | ||
node | ||
}) => node === replacement)) { | ||
return; | ||
@@ -1269,5 +1220,5 @@ } // https://github.com/babel/minify/issues/611 | ||
if (replacementPath.isFunctionDeclaration()) { | ||
var fnName = replacementPath.get("id").node.name; | ||
const fnName = replacementPath.get("id").node.name; | ||
for (var name in replacementPath.scope.bindings) { | ||
for (let name in replacementPath.scope.bindings) { | ||
if (name === fnName) { | ||
@@ -1300,5 +1251,5 @@ return; | ||
ReferencedIdentifier(path) { | ||
var node = path.node, | ||
scope = path.scope; | ||
var binding = scope.getBinding(node.name); | ||
const node = path.node, | ||
scope = path.scope; | ||
const binding = scope.getBinding(node.name); | ||
@@ -1309,3 +1260,3 @@ if (!binding || !binding.path.isFunction() || binding.scope === scope || !binding.constant) { | ||
var index = binding.referencePaths.indexOf(path); | ||
const index = binding.referencePaths.indexOf(path); | ||
@@ -1332,3 +1283,3 @@ if (index === -1) { | ||
function removeUnreferencedId(path) { | ||
var id = path.get("id").node; | ||
const id = path.get("id").node; | ||
@@ -1339,5 +1290,5 @@ if (!id) { | ||
var node = path.node, | ||
scope = path.scope; | ||
var binding = scope.getBinding(id.name); // Check if shadowed or is not referenced. | ||
const node = path.node, | ||
scope = path.scope; | ||
const binding = scope.getBinding(id.name); // Check if shadowed or is not referenced. | ||
@@ -1352,5 +1303,3 @@ if (binding && (binding.path.node !== node || !binding.referenced)) { | ||
function isAncestor(path1, path2) { | ||
return !!path2.findParent(function (parent) { | ||
return parent === path1; | ||
}); | ||
return !!path2.findParent(parent => parent === path1); | ||
} | ||
@@ -1371,9 +1320,8 @@ | ||
function isControlTransfer(stmt, path) { | ||
var control = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "break"; | ||
var _break$continue = { | ||
function isControlTransfer(stmt, path, control = "break") { | ||
const _break$continue = { | ||
break: "BreakStatement", | ||
continue: "ContinueStatement" | ||
}, | ||
type = _break$continue[control]; | ||
type = _break$continue[control]; | ||
@@ -1384,3 +1332,3 @@ if (!type) { | ||
var checker = `is${type}`; | ||
const checker = `is${type}`; | ||
@@ -1391,4 +1339,4 @@ if (stmt[checker]()) { | ||
var isTransferred = false; | ||
var result = { | ||
let isTransferred = false; | ||
let result = { | ||
[control]: false, | ||
@@ -1412,3 +1360,3 @@ bail: false | ||
function _isControlTransfer(cPath, path) { | ||
var label = cPath.get("label"); | ||
const label = cPath.get("label"); | ||
@@ -1429,3 +1377,3 @@ if (label.node !== null) { | ||
var labelPath; | ||
let labelPath; | ||
@@ -1438,3 +1386,3 @@ if (path.scope.getLabel) { | ||
var _isAncestor = isAncestor(labelPath, path); | ||
const _isAncestor = isAncestor(labelPath, path); | ||
@@ -1448,8 +1396,8 @@ return { | ||
var isCTransfer = true; // this flag is to capture | ||
let isCTransfer = true; // this flag is to capture | ||
// switch(0) { case 0: while(1) if (x) break; } | ||
var possibleRunTimeControlTransfer = false; // and compute if it's breaking the correct thing | ||
let possibleRunTimeControlTransfer = false; // and compute if it's breaking the correct thing | ||
var parent = cPath.parentPath; | ||
let parent = cPath.parentPath; | ||
@@ -1504,3 +1452,3 @@ while (parent !== stmt.parentPath) { | ||
function getLabel(name, _path) { | ||
var label, | ||
let label, | ||
path = _path; | ||
@@ -1520,3 +1468,3 @@ | ||
function hasLoopParent(path) { | ||
var parent = path; | ||
let parent = path; | ||
@@ -1533,6 +1481,6 @@ do { | ||
function extractSequenceImpure(seq) { | ||
var expressions = seq.get("expressions"); | ||
var result = []; | ||
const expressions = seq.get("expressions"); | ||
const result = []; | ||
for (var i = 0; i < expressions.length; i++) { | ||
for (let i = 0; i < expressions.length; i++) { | ||
if (!expressions[i].isPure()) { | ||
@@ -1539,0 +1487,0 @@ result.push(expressions[i].node); |
"use strict"; | ||
module.exports = removeUseStrict; | ||
var newIssueUrl = "https://github.com/babel/minify/issues/new"; | ||
var useStrict = "use strict"; | ||
const newIssueUrl = "https://github.com/babel/minify/issues/new"; | ||
const useStrict = "use strict"; | ||
/** | ||
@@ -19,3 +19,3 @@ * Remove redundant use strict | ||
var useStricts = getUseStrictDirectives(block); // early exit | ||
const useStricts = getUseStrictDirectives(block); // early exit | ||
@@ -25,3 +25,3 @@ if (useStricts.length < 1) return; // only keep the first use strict | ||
if (useStricts.length > 1) { | ||
for (var i = 1; i < useStricts.length; i++) { | ||
for (let i = 1; i < useStricts.length; i++) { | ||
useStricts[i].remove(); | ||
@@ -38,5 +38,3 @@ } | ||
function hasStrictParent(path) { | ||
return path.findParent(function (parent) { | ||
return parent.isBlockStatement() && isStrict(parent); | ||
}); | ||
return path.findParent(parent => parent.isBlockStatement() && isStrict(parent)); | ||
} | ||
@@ -43,0 +41,0 @@ |
{ | ||
"name": "babel-plugin-minify-dead-code-elimination", | ||
"version": "0.5.0-alpha.7b176463", | ||
"version": "0.5.0-alpha.7ba5821f", | ||
"description": "", | ||
@@ -15,7 +15,7 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.5.0-alpha.7b176463", | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.7b176463", | ||
"babel-helper-remove-or-void": "^0.5.0-alpha.7b176463", | ||
"babel-helper-evaluate-path": "^0.5.0-alpha.7ba5821f", | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.7ba5821f", | ||
"babel-helper-remove-or-void": "^0.5.0-alpha.7ba5821f", | ||
"lodash.some": "^4.6.0" | ||
} | ||
} |
@@ -34,3 +34,3 @@ # babel-plugin-minify-dead-code-elimination | ||
```sh | ||
npm install babel-plugin-minify-dead-code-elimination | ||
npm install babel-plugin-minify-dead-code-elimination --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
45646
1173