babel-plugin-minify-dead-code-elimination
Advanced tools
Comparing version 0.3.0-alpha.37064b40 to 0.3.0-alpha.5ee3078e
605
lib/index.js
"use strict"; | ||
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); } } | ||
const some = require("lodash.some"); | ||
var some = require("lodash.some"); | ||
var _require = require("babel-helper-mark-eval-scopes"); | ||
var _require = require("babel-helper-mark-eval-scopes"), | ||
markEvalScopes = _require.markEvalScopes, | ||
hasEval = _require.hasEval; | ||
const markEvalScopes = _require.markEvalScopes, | ||
hasEval = _require.hasEval; | ||
var removeUseStrict = require("./remove-use-strict"); | ||
var evaluate = require("babel-helper-evaluate-path"); | ||
const removeUseStrict = require("./remove-use-strict"); | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
function prevSiblings(path) { | ||
var parentPath = path.parentPath; | ||
var siblings = []; | ||
const parentPath = path.parentPath; | ||
const siblings = []; | ||
var key = parentPath.key; | ||
let key = parentPath.key; | ||
@@ -32,15 +31,12 @@ 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); | ||
const shouldRevisit = Symbol("shouldRevisit"); | ||
var removeOrVoid = require("babel-helper-remove-or-void")(t); | ||
var 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"); | ||
const markForRemoval = Symbol("markForRemoval"); | ||
var main = { | ||
const main = { | ||
// remove side effectless statement | ||
@@ -65,18 +61,17 @@ ExpressionStatement(path) { | ||
var node = path.node, | ||
scope = path.scope; | ||
const node = path.node, | ||
scope = path.scope; | ||
var seen = new Set(); | ||
var declars = []; | ||
var mutations = []; | ||
var _loop = function _loop(name) { | ||
var binding = scope.bindings[name]; | ||
const seen = new Set(); | ||
const declars = []; | ||
const mutations = []; | ||
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; | ||
} | ||
@@ -86,46 +81,41 @@ seen.add(declarPath); | ||
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; | ||
_loop2(declar); | ||
declars.push(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) { | ||
_iterator2.return(); | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
@@ -136,34 +126,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); | ||
statement.declarations.push(...declars); | ||
return; | ||
@@ -173,12 +149,12 @@ } | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
@@ -188,3 +164,3 @@ } | ||
var varDecl = t.variableDeclaration("var", declars); | ||
const varDecl = t.variableDeclaration("var", declars); | ||
node.body.body.unshift(varDecl); | ||
@@ -205,4 +181,2 @@ } | ||
enter(path) { | ||
var _this = this; | ||
if (path.isProgram()) { | ||
@@ -216,3 +190,3 @@ return; | ||
var scope = path.scope; | ||
const scope = path.scope; | ||
@@ -222,10 +196,10 @@ // if the scope is created by a function, we obtain its | ||
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; | ||
@@ -242,8 +216,8 @@ | ||
} 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]; | ||
if (_binding.referenced) { | ||
const binding = scope.bindings[left.node.name]; | ||
if (binding.referenced) { | ||
// when the first binding is referenced (right to left) | ||
@@ -254,3 +228,3 @@ // exit without marking anything after this | ||
_binding[markForRemoval] = true; | ||
binding[markForRemoval] = true; | ||
continue; | ||
@@ -265,11 +239,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; | ||
if (maybeBlockParent && maybeBlockParent.isForXStatement({ | ||
@@ -279,10 +253,10 @@ left: declaration.node | ||
// 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 ( | ||
@@ -293,9 +267,9 @@ // ClassDeclaration has binding in two scopes | ||
binding.path.isClassDeclaration() && binding.path === scope.path) { | ||
return "continue"; | ||
continue; | ||
} | ||
var mutations = []; | ||
var bail = false; | ||
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) { | ||
@@ -310,9 +284,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)); | ||
} | ||
@@ -322,3 +292,3 @@ }); | ||
if (bail) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -329,3 +299,3 @@ | ||
// deopt for object and array pattern | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -339,77 +309,67 @@ | ||
if (binding.path.parentPath.node.declarations.length !== 1) { | ||
return "continue"; | ||
continue; | ||
} | ||
binding.path.parentPath.replaceWith(binding.path.node.init); | ||
} 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 _ret4 = 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 ref = _step3.value; | ||
try { | ||
for (var _iterator3 = binding.referencePaths[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
const ref = _step3.value; | ||
if (!ref.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) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_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 _ret4 === "object") return _ret4.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; | ||
let replacement = binding.path.node; | ||
let replacementPath = binding.path; | ||
let isReferencedBefore = false; | ||
var refPath = binding.referencePaths[0]; | ||
const refPath = binding.referencePaths[0]; | ||
if (t.isVariableDeclarator(replacement)) { | ||
var _prevSiblings = prevSiblings(replacementPath); | ||
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) { | ||
@@ -424,3 +384,3 @@ isReferencedBefore = true; | ||
if (isReferencedBefore && refPath.scope !== binding.scope) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -435,3 +395,3 @@ | ||
if (!replacementPath.get("id").isIdentifier()) { | ||
return "continue"; | ||
continue; | ||
} | ||
@@ -442,13 +402,18 @@ replacementPath = replacementPath.get("init"); | ||
if (!replacement) { | ||
return "continue"; | ||
continue; | ||
} | ||
if (!scope.isPure(replacement, true) && !isReferencedBefore) { | ||
return "continue"; | ||
continue; | ||
} | ||
var _bail = false; | ||
let bail = false; | ||
if (replacementPath.isIdentifier()) { | ||
_bail = refPath.scope.getBinding(replacement.name) !== scope.getBinding(replacement.name); | ||
const binding = scope.getBinding(replacement.name); | ||
// the reference should be in the same scope | ||
// and the replacement should be a constant - this is to | ||
// ensure that the duplication of replacement is not affected | ||
// https://github.com/babel/minify/issues/685 | ||
bail = !(binding && refPath.scope.getBinding(replacement.name) === binding && binding.constantViolations.length === 0); | ||
} else { | ||
@@ -460,9 +425,10 @@ replacementPath.traverse({ | ||
ReferencedIdentifier(_ref2) { | ||
var node = _ref2.node; | ||
if (_bail) { | ||
ReferencedIdentifier({ node }) { | ||
if (bail) { | ||
return; | ||
} | ||
_bail = refPath.scope.getBinding(node.name) !== scope.getBinding(node.name); | ||
const binding = scope.getBinding(node.name); | ||
if (binding && refPath.scope.getBinding(node.name) === binding) { | ||
bail = binding.constantViolations.length > 0; | ||
} | ||
} | ||
@@ -472,7 +438,7 @@ }); | ||
if (_bail) { | ||
return "continue"; | ||
if (bail) { | ||
continue; | ||
} | ||
var parent = binding.path.parent; | ||
let parent = binding.path.parent; | ||
if (t.isVariableDeclaration(parent)) { | ||
@@ -487,6 +453,4 @@ parent = binding.path.parentPath.parent; | ||
// which would also could affect correctness in that they are not the same reference. | ||
var mayLoop = false; | ||
var sharesRoot = refPath.find(function (_ref3) { | ||
var node = _ref3.node; | ||
let mayLoop = false; | ||
const sharesRoot = refPath.find(({ node }) => { | ||
if (!mayLoop) { | ||
@@ -499,10 +463,8 @@ mayLoop = t.isWhileStatement(node) || t.isFor(node) || t.isFunction(node); | ||
// Anything that inherits from Object. | ||
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; | ||
} | ||
@@ -517,3 +479,3 @@ | ||
// all the binary "in" operations | ||
var inExpression = replacementPath.isBinaryExpression({ | ||
let inExpression = replacementPath.isBinaryExpression({ | ||
operator: "in" | ||
@@ -537,6 +499,6 @@ }); | ||
if (inExpression) { | ||
return "continue"; | ||
continue; | ||
} | ||
var replaced = replace(binding.referencePaths[0], { | ||
const replaced = replace(binding.referencePaths[0], { | ||
binding, | ||
@@ -556,8 +518,2 @@ scope, | ||
} | ||
}; | ||
for (var name in scope.bindings) { | ||
var _ret3 = _loop3(name); | ||
if (_ret3 === "continue") continue; | ||
} // end-for-of | ||
@@ -569,8 +525,8 @@ } | ||
BlockStatement(path) { | ||
var paths = path.get("body"); | ||
const paths = path.get("body"); | ||
var purge = false; | ||
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]; | ||
@@ -591,3 +547,3 @@ if (!purge && p.isCompletionStatement()) { | ||
ReturnStatement(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -613,4 +569,4 @@ if (!path.inList) { | ||
var noNext = true; | ||
var parentPath = path.parentPath; | ||
let noNext = true; | ||
let parentPath = path.parentPath; | ||
while (parentPath && !parentPath.isFunction() && noNext) { | ||
@@ -623,3 +579,3 @@ // https://github.com/babel/minify/issues/265 | ||
var nextPath = parentPath.getSibling(parentPath.key + 1); | ||
const nextPath = parentPath.getSibling(parentPath.key + 1); | ||
if (nextPath.node) { | ||
@@ -649,5 +605,5 @@ if (nextPath.isReturnStatement()) { | ||
ConditionalExpression(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
var evaluateTest = path.get("test").evaluateTruthy(); | ||
const evaluateTest = path.get("test").evaluateTruthy(); | ||
if (evaluateTest === true) { | ||
@@ -662,14 +618,14 @@ path.replaceWith(node.consequent); | ||
exit(path) { | ||
var evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz }); | ||
const evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz }); | ||
if (!evaluated.confident) return; | ||
var discriminant = evaluated.value; | ||
var cases = path.get("cases"); | ||
const discriminant = evaluated.value; | ||
const cases = path.get("cases"); | ||
var matchingCaseIndex = -1; | ||
var defaultCaseIndex = -1; | ||
let matchingCaseIndex = -1; | ||
let defaultCaseIndex = -1; | ||
for (var i = 0; i < cases.length; i++) { | ||
var test = cases[i].get("test"); | ||
for (let i = 0; i < cases.length; i++) { | ||
const test = cases[i].get("test"); | ||
@@ -682,3 +638,3 @@ // handle default case | ||
var testResult = evaluate(test, { tdz: this.tdz }); | ||
const testResult = evaluate(test, { tdz: this.tdz }); | ||
@@ -695,3 +651,3 @@ // if we are not able to deternine a test during | ||
var result = void 0; | ||
let result; | ||
@@ -715,6 +671,6 @@ if (matchingCaseIndex === -1) { | ||
// will be again removed by DCE | ||
replaceSwitch([].concat(_toConsumableArray(extractVars(path)), _toConsumableArray(result.statements))); | ||
replaceSwitch([...extractVars(path), ...result.statements]); | ||
function getStatementsUntilBreak(start) { | ||
var result = { | ||
const result = { | ||
bail: false, | ||
@@ -724,7 +680,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); | ||
if (_isBreaking.bail) { | ||
@@ -748,10 +704,10 @@ result.bail = true; | ||
function replaceSwitch(statements) { | ||
var isBlockRequired = false; | ||
let isBlockRequired = false; | ||
for (var _i2 = 0; _i2 < statements.length; _i2++) { | ||
if (t.isVariableDeclaration(statements[_i2], { kind: "let" })) { | ||
for (let i = 0; i < statements.length; i++) { | ||
if (t.isVariableDeclaration(statements[i], { kind: "let" })) { | ||
isBlockRequired = true; | ||
break; | ||
} | ||
if (t.isVariableDeclaration(statements[_i2], { kind: "const" })) { | ||
if (t.isVariableDeclaration(statements[i], { kind: "const" })) { | ||
isBlockRequired = true; | ||
@@ -772,4 +728,4 @@ break; | ||
WhileStatement(path) { | ||
var test = path.get("test"); | ||
var result = evaluate(test, { tdz: this.tdz }); | ||
const test = path.get("test"); | ||
const result = evaluate(test, { tdz: this.tdz }); | ||
if (result.confident && test.isPure() && !result.value) { | ||
@@ -781,6 +737,6 @@ path.remove(); | ||
ForStatement(path) { | ||
var test = path.get("test"); | ||
const test = path.get("test"); | ||
if (!test.isPure()) return; | ||
var result = evaluate(test, { tdz: this.tdz }); | ||
const result = evaluate(test, { tdz: this.tdz }); | ||
if (result.confident) { | ||
@@ -796,9 +752,9 @@ if (result.value) { | ||
DoWhileStatement(path) { | ||
var test = path.get("test"); | ||
var result = evaluate(test, { tdz: this.tdz }); | ||
const test = path.get("test"); | ||
const result = evaluate(test, { tdz: this.tdz }); | ||
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; | ||
@@ -810,7 +766,7 @@ 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); | ||
if (_isContinuing.bail || isContinuing.continue) return; | ||
@@ -835,5 +791,5 @@ } | ||
} else if (body.isBreakStatement()) { | ||
var _isBreaking2 = isBreaking(body, path); | ||
if (_isBreaking2.bail) return; | ||
if (_isBreaking2.break) path.remove(); | ||
const _isBreaking = isBreaking(body, path); | ||
if (_isBreaking.bail) return; | ||
if (_isBreaking.break) path.remove(); | ||
} else if (body.isContinueStatement()) { | ||
@@ -854,3 +810,3 @@ return; | ||
var prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
const prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
if (!(prev && prev.isVariableDeclaration())) { | ||
@@ -860,3 +816,3 @@ return; | ||
var declars = prev.node.declarations; | ||
const declars = prev.node.declarations; | ||
if (declars.length !== 1 || declars[0].init || declars[0].id.name !== path.get("left").node.name) { | ||
@@ -886,3 +842,3 @@ return; | ||
ForInStatement(path) { | ||
var left = path.get("left"); | ||
const left = path.get("left"); | ||
if (!left.isIdentifier()) { | ||
@@ -892,3 +848,3 @@ return; | ||
var binding = path.scope.getBinding(left.node.name); | ||
const binding = path.scope.getBinding(left.node.name); | ||
if (!binding) { | ||
@@ -937,3 +893,3 @@ return; | ||
*/ | ||
var body = path.get("body"); | ||
const body = path.get("body"); | ||
if (body.isBlockStatement()) { | ||
@@ -945,16 +901,11 @@ removeUseStrict(body); | ||
IfStatement: { | ||
exit(path, _ref4) { | ||
var _ref4$opts = _ref4.opts; | ||
_ref4$opts = _ref4$opts === undefined ? {} : _ref4$opts; | ||
var _ref4$opts$tdz = _ref4$opts.tdz, | ||
tdz = _ref4$opts$tdz === undefined ? false : _ref4$opts$tdz; | ||
exit(path, { opts: { tdz = false } = {} }) { | ||
const consequent = path.get("consequent"); | ||
const alternate = path.get("alternate"); | ||
const test = path.get("test"); | ||
var consequent = path.get("consequent"); | ||
var alternate = path.get("alternate"); | ||
var test = path.get("test"); | ||
const evalResult = evaluate(test, { tdz }); | ||
const isPure = test.isPure(); | ||
var evalResult = evaluate(test, { tdz }); | ||
var isPure = test.isPure(); | ||
var replacements = []; | ||
const replacements = []; | ||
if (evalResult.confident && !isPure && test.isSequenceExpression()) { | ||
@@ -971,3 +922,3 @@ replacements.push(t.expressionStatement(extractSequenceImpure(test))); | ||
if (evalResult.confident && evalResult.value) { | ||
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(toStatements(consequent)), _toConsumableArray(extractVars(alternate)))); | ||
path.replaceWithMultiple([...replacements, ...toStatements(consequent), ...extractVars(alternate)]); | ||
return; | ||
@@ -984,6 +935,6 @@ } | ||
if (alternate.node) { | ||
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(toStatements(alternate)), _toConsumableArray(extractVars(consequent)))); | ||
path.replaceWithMultiple([...replacements, ...toStatements(alternate), ...extractVars(consequent)]); | ||
return; | ||
} else { | ||
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(extractVars(consequent)))); | ||
path.replaceWithMultiple([...replacements, ...extractVars(consequent)]); | ||
} | ||
@@ -1024,18 +975,12 @@ } | ||
Program: { | ||
exit(path) { | ||
var _ref5 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
_ref5$opts = _ref5.opts; | ||
_ref5$opts = _ref5$opts === undefined ? {} : _ref5$opts; | ||
var _ref5$opts$optimizeRa = _ref5$opts.optimizeRawSize, | ||
optimizeRawSize = _ref5$opts$optimizeRa === undefined ? false : _ref5$opts$optimizeRa, | ||
_ref5$opts$keepFnName = _ref5$opts.keepFnName, | ||
keepFnName = _ref5$opts$keepFnName === undefined ? false : _ref5$opts$keepFnName, | ||
_ref5$opts$keepClassN = _ref5$opts.keepClassName, | ||
keepClassName = _ref5$opts$keepClassN === undefined ? false : _ref5$opts$keepClassN, | ||
_ref5$opts$keepFnArgs = _ref5$opts.keepFnArgs, | ||
keepFnArgs = _ref5$opts$keepFnArgs === undefined ? false : _ref5$opts$keepFnArgs, | ||
_ref5$opts$tdz = _ref5$opts.tdz, | ||
tdz = _ref5$opts$tdz === undefined ? false : _ref5$opts$tdz; | ||
exit(path, { | ||
opts: { | ||
// set defaults | ||
optimizeRawSize = false, | ||
keepFnName = false, | ||
keepClassName = false, | ||
keepFnArgs = false, | ||
tdz = false | ||
} = {} | ||
} = {}) { | ||
(traverse.clearCache || traverse.cache.clear)(); | ||
@@ -1061,9 +1006,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]; | ||
if (t.isBlockScoped(bodyNode)) { | ||
@@ -1088,3 +1033,3 @@ hasBlockScoped = true; | ||
function extractVars(path) { | ||
var declarators = []; | ||
const declarators = []; | ||
@@ -1098,9 +1043,7 @@ if (path.isVariableDeclaration({ kind: "var" })) { | ||
for (var _iterator5 = path.node.declarations[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
var decl = _step5.value; | ||
const decl = _step5.value; | ||
var bindingIds = Object.keys(t.getBindingIdentifiers(decl.id)); | ||
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id)); | ||
declarators.push.apply(declarators, _toConsumableArray(bindingIds.map(function (name) { | ||
return t.variableDeclarator(t.identifier(name)); | ||
}))); | ||
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))); | ||
} | ||
@@ -1133,8 +1076,6 @@ } catch (err) { | ||
for (var _iterator6 = varPath.node.declarations[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) { | ||
var _decl = _step6.value; | ||
const decl = _step6.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 bindingIds = Object.keys(t.getBindingIdentifiers(decl.id)); | ||
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name)))); | ||
} | ||
@@ -1165,6 +1106,6 @@ } catch (err) { | ||
function replace(path, options) { | ||
var replacement = options.replacement, | ||
replacementPath = options.replacementPath, | ||
scope = options.scope, | ||
binding = options.binding; | ||
const replacement = options.replacement, | ||
replacementPath = options.replacementPath, | ||
scope = options.scope, | ||
binding = options.binding; | ||
@@ -1185,3 +1126,3 @@ // Same name, different binding. | ||
var bail = false; | ||
let bail = false; | ||
traverse(replacement, { | ||
@@ -1203,6 +1144,3 @@ Function(path) { | ||
// Avoid recursion. | ||
if (path.find(function (_ref6) { | ||
var node = _ref6.node; | ||
return node === replacement; | ||
})) { | ||
if (path.find(({ node }) => node === replacement)) { | ||
return; | ||
@@ -1215,4 +1153,4 @@ } | ||
if (replacementPath.isFunctionDeclaration()) { | ||
var fnName = replacementPath.get("id").node.name; | ||
for (var name in replacementPath.scope.bindings) { | ||
const fnName = replacementPath.get("id").node.name; | ||
for (let name in replacementPath.scope.bindings) { | ||
if (name === fnName) { | ||
@@ -1246,6 +1184,6 @@ return; | ||
ReferencedIdentifier(path) { | ||
var node = path.node, | ||
scope = path.scope; | ||
const node = path.node, | ||
scope = path.scope; | ||
var binding = scope.getBinding(node.name); | ||
const binding = scope.getBinding(node.name); | ||
@@ -1256,3 +1194,3 @@ if (!binding || !binding.path.isFunction() || binding.scope === scope || !binding.constant) { | ||
var index = binding.referencePaths.indexOf(path); | ||
const index = binding.referencePaths.indexOf(path); | ||
if (index === -1) { | ||
@@ -1275,3 +1213,3 @@ return; | ||
function removeUnreferencedId(path) { | ||
var id = path.get("id").node; | ||
const id = path.get("id").node; | ||
if (!id) { | ||
@@ -1281,6 +1219,6 @@ return; | ||
var node = path.node, | ||
scope = path.scope; | ||
const node = path.node, | ||
scope = path.scope; | ||
var binding = scope.getBinding(id.name); | ||
const binding = scope.getBinding(id.name); | ||
@@ -1296,5 +1234,3 @@ // Check if shadowed or is not referenced. | ||
function isAncestor(path1, path2) { | ||
return !!path2.findParent(function (parent) { | ||
return parent === path1; | ||
}); | ||
return !!path2.findParent(parent => parent === path1); | ||
} | ||
@@ -1315,9 +1251,8 @@ | ||
// tells if a "stmt" is a break/continue statement | ||
function isControlTransfer(stmt, path) { | ||
var control = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "break"; | ||
function isControlTransfer(stmt, path, control = "break") { | ||
var _break$continue = { | ||
break: "BreakStatement", | ||
continue: "ContinueStatement" | ||
}, | ||
type = _break$continue[control]; | ||
}; | ||
const type = _break$continue[control]; | ||
@@ -1327,3 +1262,3 @@ if (!type) { | ||
} | ||
var checker = `is${type}`; | ||
const checker = `is${type}`; | ||
@@ -1334,4 +1269,4 @@ if (stmt[checker]()) { | ||
var isTransferred = false; | ||
var result = { | ||
let isTransferred = false; | ||
let result = { | ||
[control]: false, | ||
@@ -1357,3 +1292,3 @@ bail: false | ||
function _isControlTransfer(cPath, path) { | ||
var label = cPath.get("label"); | ||
const label = cPath.get("label"); | ||
@@ -1374,3 +1309,3 @@ if (label.node !== null) { | ||
// if they are within the case, we keep them | ||
var labelPath = void 0; | ||
let labelPath; | ||
if (path.scope.getLabel) { | ||
@@ -1381,3 +1316,3 @@ labelPath = getLabel(label.node.name, path); | ||
} | ||
var _isAncestor = isAncestor(labelPath, path); | ||
const _isAncestor = isAncestor(labelPath, path); | ||
@@ -1391,10 +1326,10 @@ return { | ||
// set the flag that it is indeed breaking | ||
var isCTransfer = true; | ||
let isCTransfer = true; | ||
// this flag is to capture | ||
// switch(0) { case 0: while(1) if (x) break; } | ||
var possibleRunTimeControlTransfer = false; | ||
let possibleRunTimeControlTransfer = false; | ||
// and compute if it's breaking the correct thing | ||
var parent = cPath.parentPath; | ||
let parent = cPath.parentPath; | ||
@@ -1446,3 +1381,3 @@ while (parent !== stmt.parentPath) { | ||
function getLabel(name, _path) { | ||
var label = void 0, | ||
let label, | ||
path = _path; | ||
@@ -1459,3 +1394,3 @@ do { | ||
function hasLoopParent(path) { | ||
var parent = path; | ||
let parent = path; | ||
do { | ||
@@ -1470,5 +1405,5 @@ if (parent.isLoop()) { | ||
function extractSequenceImpure(seq) { | ||
var expressions = seq.get("expressions"); | ||
var result = []; | ||
for (var i = 0; i < expressions.length; i++) { | ||
const expressions = seq.get("expressions"); | ||
const result = []; | ||
for (let i = 0; i < expressions.length; i++) { | ||
if (!expressions[i].isPure()) { | ||
@@ -1475,0 +1410,0 @@ result.push(expressions[i].node); |
@@ -5,5 +5,5 @@ "use strict"; | ||
var newIssueUrl = "https://github.com/babel/minify/issues/new"; | ||
const newIssueUrl = "https://github.com/babel/minify/issues/new"; | ||
var useStrict = "use strict"; | ||
const useStrict = "use strict"; | ||
@@ -22,3 +22,3 @@ /** | ||
var useStricts = getUseStrictDirectives(block); | ||
const useStricts = getUseStrictDirectives(block); | ||
@@ -30,3 +30,3 @@ // early exit | ||
if (useStricts.length > 1) { | ||
for (var i = 1; i < useStricts.length; i++) { | ||
for (let i = 1; i < useStricts.length; i++) { | ||
useStricts[i].remove(); | ||
@@ -43,5 +43,3 @@ } | ||
function hasStrictParent(path) { | ||
return path.findParent(function (parent) { | ||
return parent.isBlockStatement() && isStrict(parent); | ||
}); | ||
return path.findParent(parent => parent.isBlockStatement() && isStrict(parent)); | ||
} | ||
@@ -48,0 +46,0 @@ |
{ | ||
"name": "babel-plugin-minify-dead-code-elimination", | ||
"version": "0.3.0-alpha.37064b40", | ||
"version": "0.3.0-alpha.5ee3078e", | ||
"description": "", | ||
@@ -15,7 +15,7 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.3.0-alpha.37064b40", | ||
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.37064b40", | ||
"babel-helper-remove-or-void": "^0.3.0-alpha.37064b40", | ||
"babel-helper-evaluate-path": "^0.3.0-alpha.5ee3078e", | ||
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.5ee3078e", | ||
"babel-helper-remove-or-void": "^0.3.0-alpha.5ee3078e", | ||
"lodash.some": "^4.6.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
45316
1176