babel-plugin-minify-dead-code-elimination
Advanced tools
Comparing version 0.3.0-alpha.7f4dc3d4 to 0.3.0-alpha.879f25ec
601
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,13 @@ 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()) { | ||
var _binding2 = scope.getBinding(replacement.name); | ||
const binding = scope.getBinding(replacement.name); | ||
// the reference should be in the same scope | ||
@@ -458,3 +418,3 @@ // and the replacement should be a constant - this is to | ||
// https://github.com/babel/minify/issues/685 | ||
_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 { | ||
@@ -466,11 +426,9 @@ replacementPath.traverse({ | ||
ReferencedIdentifier(_ref2) { | ||
var node = _ref2.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; | ||
} | ||
@@ -481,7 +439,7 @@ } | ||
if (_bail) { | ||
return "continue"; | ||
if (bail) { | ||
continue; | ||
} | ||
var parent = binding.path.parent; | ||
let parent = binding.path.parent; | ||
if (t.isVariableDeclaration(parent)) { | ||
@@ -496,6 +454,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) { | ||
@@ -508,10 +464,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; | ||
} | ||
@@ -526,3 +480,3 @@ | ||
// all the binary "in" operations | ||
var inExpression = replacementPath.isBinaryExpression({ | ||
let inExpression = replacementPath.isBinaryExpression({ | ||
operator: "in" | ||
@@ -546,6 +500,6 @@ }); | ||
if (inExpression) { | ||
return "continue"; | ||
continue; | ||
} | ||
var replaced = replace(binding.referencePaths[0], { | ||
const replaced = replace(binding.referencePaths[0], { | ||
binding, | ||
@@ -565,8 +519,2 @@ scope, | ||
} | ||
}; | ||
for (var name in scope.bindings) { | ||
var _ret3 = _loop3(name); | ||
if (_ret3 === "continue") continue; | ||
} // end-for-of | ||
@@ -578,8 +526,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]; | ||
@@ -600,3 +548,3 @@ if (!purge && p.isCompletionStatement()) { | ||
ReturnStatement(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -622,4 +570,4 @@ if (!path.inList) { | ||
var noNext = true; | ||
var parentPath = path.parentPath; | ||
let noNext = true; | ||
let parentPath = path.parentPath; | ||
while (parentPath && !parentPath.isFunction() && noNext) { | ||
@@ -632,3 +580,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) { | ||
@@ -658,5 +606,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) { | ||
@@ -671,14 +619,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"); | ||
@@ -691,3 +639,3 @@ // handle default case | ||
var testResult = evaluate(test, { tdz: this.tdz }); | ||
const testResult = evaluate(test, { tdz: this.tdz }); | ||
@@ -704,3 +652,3 @@ // if we are not able to deternine a test during | ||
var result = void 0; | ||
let result; | ||
@@ -724,6 +672,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, | ||
@@ -733,7 +681,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) { | ||
@@ -757,10 +705,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; | ||
@@ -781,4 +729,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) { | ||
@@ -790,6 +738,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) { | ||
@@ -805,9 +753,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; | ||
@@ -819,7 +767,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; | ||
@@ -844,5 +792,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()) { | ||
@@ -863,3 +811,3 @@ return; | ||
var prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
const prev = path.parentPath.getSibling(path.parentPath.key - 1); | ||
if (!(prev && prev.isVariableDeclaration())) { | ||
@@ -869,3 +817,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) { | ||
@@ -895,3 +843,3 @@ return; | ||
ForInStatement(path) { | ||
var left = path.get("left"); | ||
const left = path.get("left"); | ||
if (!left.isIdentifier()) { | ||
@@ -901,3 +849,3 @@ return; | ||
var binding = path.scope.getBinding(left.node.name); | ||
const binding = path.scope.getBinding(left.node.name); | ||
if (!binding) { | ||
@@ -946,3 +894,3 @@ return; | ||
*/ | ||
var body = path.get("body"); | ||
const body = path.get("body"); | ||
if (body.isBlockStatement()) { | ||
@@ -954,16 +902,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()) { | ||
@@ -980,3 +923,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; | ||
@@ -993,6 +936,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)]); | ||
} | ||
@@ -1033,18 +976,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)(); | ||
@@ -1070,9 +1007,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)) { | ||
@@ -1097,3 +1034,3 @@ hasBlockScoped = true; | ||
function extractVars(path) { | ||
var declarators = []; | ||
const declarators = []; | ||
@@ -1107,9 +1044,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)))); | ||
} | ||
@@ -1142,8 +1077,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)))); | ||
} | ||
@@ -1174,6 +1107,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; | ||
@@ -1194,3 +1127,3 @@ // Same name, different binding. | ||
var bail = false; | ||
let bail = false; | ||
traverse(replacement, { | ||
@@ -1212,6 +1145,3 @@ Function(path) { | ||
// Avoid recursion. | ||
if (path.find(function (_ref6) { | ||
var node = _ref6.node; | ||
return node === replacement; | ||
})) { | ||
if (path.find(({ node }) => node === replacement)) { | ||
return; | ||
@@ -1224,4 +1154,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) { | ||
@@ -1255,6 +1185,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); | ||
@@ -1265,3 +1195,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) { | ||
@@ -1284,3 +1214,3 @@ return; | ||
function removeUnreferencedId(path) { | ||
var id = path.get("id").node; | ||
const id = path.get("id").node; | ||
if (!id) { | ||
@@ -1290,6 +1220,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); | ||
@@ -1305,5 +1235,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); | ||
} | ||
@@ -1324,9 +1252,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]; | ||
@@ -1336,3 +1263,3 @@ if (!type) { | ||
} | ||
var checker = `is${type}`; | ||
const checker = `is${type}`; | ||
@@ -1343,4 +1270,4 @@ if (stmt[checker]()) { | ||
var isTransferred = false; | ||
var result = { | ||
let isTransferred = false; | ||
let result = { | ||
[control]: false, | ||
@@ -1366,3 +1293,3 @@ bail: false | ||
function _isControlTransfer(cPath, path) { | ||
var label = cPath.get("label"); | ||
const label = cPath.get("label"); | ||
@@ -1383,3 +1310,3 @@ if (label.node !== null) { | ||
// if they are within the case, we keep them | ||
var labelPath = void 0; | ||
let labelPath; | ||
if (path.scope.getLabel) { | ||
@@ -1390,3 +1317,3 @@ labelPath = getLabel(label.node.name, path); | ||
} | ||
var _isAncestor = isAncestor(labelPath, path); | ||
const _isAncestor = isAncestor(labelPath, path); | ||
@@ -1400,10 +1327,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; | ||
@@ -1455,3 +1382,3 @@ while (parent !== stmt.parentPath) { | ||
function getLabel(name, _path) { | ||
var label = void 0, | ||
let label, | ||
path = _path; | ||
@@ -1468,3 +1395,3 @@ do { | ||
function hasLoopParent(path) { | ||
var parent = path; | ||
let parent = path; | ||
do { | ||
@@ -1479,5 +1406,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()) { | ||
@@ -1484,0 +1411,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.7f4dc3d4", | ||
"version": "0.3.0-alpha.879f25ec", | ||
"description": "", | ||
@@ -15,7 +15,7 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.3.0-alpha.7f4dc3d4", | ||
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.7f4dc3d4", | ||
"babel-helper-remove-or-void": "^0.3.0-alpha.7f4dc3d4", | ||
"babel-helper-evaluate-path": "^0.3.0-alpha.879f25ec", | ||
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.879f25ec", | ||
"babel-helper-remove-or-void": "^0.3.0-alpha.879f25ec", | ||
"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