Socket
Socket
Sign inDemoInstall

babel-plugin-minify-dead-code-elimination

Package Overview
Dependencies
Maintainers
5
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-minify-dead-code-elimination - npm Package Compare versions

Comparing version 0.3.0-alpha.6ae655c0 to 0.3.0-alpha.6ceffb8d

601

lib/index.js
"use strict";
const some = require("lodash.some");
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); } }
var _require = require("babel-helper-mark-eval-scopes");
var some = require("lodash.some");
const 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");
const evaluate = require("babel-helper-evaluate-path");
var removeUseStrict = require("./remove-use-strict");
var evaluate = require("babel-helper-evaluate-path");
function prevSiblings(path) {
const parentPath = path.parentPath;
const siblings = [];
var parentPath = path.parentPath;
var siblings = [];
let key = parentPath.key;
var key = parentPath.key;

@@ -31,12 +32,15 @@ while ((path = parentPath.getSibling(--key)).type) {

module.exports = ({ types: t, traverse }) => {
const removeOrVoid = require("babel-helper-remove-or-void")(t);
const shouldRevisit = Symbol("shouldRevisit");
module.exports = function (_ref) {
var t = _ref.types,
traverse = _ref.traverse;
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
const markForRemoval = Symbol("markForRemoval");
var markForRemoval = Symbol("markForRemoval");
const main = {
var main = {
// remove side effectless statement

@@ -61,17 +65,18 @@ ExpressionStatement(path) {

const node = path.node,
scope = path.scope;
var node = path.node,
scope = path.scope;
const seen = new Set();
const declars = [];
const mutations = [];
for (const name in scope.bindings) {
const binding = scope.bindings[name];
var seen = new Set();
var declars = [];
var mutations = [];
var _loop = function _loop(name) {
var binding = scope.bindings[name];
if (!binding.path.isVariableDeclarator()) {
continue;
return "continue";
}
const declarPath = binding.path.parentPath;
var declarPath = binding.path.parentPath;
if (seen.has(declarPath)) {
continue;
return "continue";
}

@@ -81,41 +86,46 @@ seen.add(declarPath);

if (declarPath.parentPath.isForInStatement()) {
continue;
return "continue";
}
if (declarPath.parentPath.parentPath.isFunction()) {
continue;
return "continue";
}
if (!declarPath.node || !declarPath.node.declarations) {
continue;
return "continue";
}
const assignmentSequence = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
var assignmentSequence = [];
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 _iterator = declarPath.node.declarations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
const declar = _step.value;
for (var _iterator2 = declarPath.node.declarations[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var declar = _step2.value;
declars.push(declar);
if (declar.init) {
assignmentSequence.push(t.assignmentExpression("=", declar.id, declar.init));
mutations.push(() => {
declar.init = null;
});
}
_loop2(declar);
}
} 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;
}

@@ -126,20 +136,34 @@ }

if (assignmentSequence.length) {
mutations.push(() => declarPath.replaceWith(t.sequenceExpression(assignmentSequence)));
mutations.push(function () {
return declarPath.replaceWith(t.sequenceExpression(assignmentSequence));
});
} else {
mutations.push(() => removeOrVoid(declarPath));
mutations.push(function () {
return removeOrVoid(declarPath);
});
}
};
for (var name in scope.bindings) {
var _ret = _loop(name);
if (_ret === "continue") continue;
}
if (declars.length) {
mutations.forEach(f => f());
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
mutations.forEach(function (f) {
return f();
});
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator2 = node.body.body[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
const statement = _step2.value;
for (var _iterator = node.body.body[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var statement = _step.value;
if (t.isVariableDeclaration(statement)) {
statement.declarations.push(...declars);
var _statement$declaratio;
(_statement$declaratio = statement.declarations).push.apply(_statement$declaratio, declars);
return;

@@ -149,12 +173,12 @@ }

} 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;
}

@@ -164,3 +188,3 @@ }

const varDecl = t.variableDeclaration("var", declars);
var varDecl = t.variableDeclaration("var", declars);
node.body.body.unshift(varDecl);

@@ -181,2 +205,4 @@ }

enter(path) {
var _this = this;
if (path.isProgram()) {

@@ -190,3 +216,3 @@ return;

const scope = path.scope;
var scope = path.scope;

@@ -196,10 +222,10 @@ // if the scope is created by a function, we obtain its

const canRemoveParams = path.isFunction() && path.node.kind !== "set";
const paramsList = canRemoveParams ? path.get("params") : [];
var canRemoveParams = path.isFunction() && path.node.kind !== "set";
var paramsList = canRemoveParams ? path.get("params") : [];
for (let i = paramsList.length - 1; i >= 0; i--) {
const param = paramsList[i];
for (var i = paramsList.length - 1; i >= 0; i--) {
var param = paramsList[i];
if (param.isIdentifier()) {
const binding = scope.bindings[param.node.name];
var binding = scope.bindings[param.node.name];
if (!binding) continue;

@@ -216,8 +242,8 @@

} else if (param.isAssignmentPattern()) {
const left = param.get("left");
const right = param.get("right");
var left = param.get("left");
var right = param.get("right");
if (left.isIdentifier() && right.isPure()) {
const binding = scope.bindings[left.node.name];
if (binding.referenced) {
var _binding = scope.bindings[left.node.name];
if (_binding.referenced) {
// when the first binding is referenced (right to left)

@@ -228,3 +254,3 @@ // exit without marking anything after this

binding[markForRemoval] = true;
_binding[markForRemoval] = true;
continue;

@@ -239,11 +265,11 @@ }

for (const name in scope.bindings) {
const binding = scope.bindings[name];
var _loop3 = function _loop3(name) {
var binding = scope.bindings[name];
if (!binding.referenced && binding.kind !== "module") {
if (binding.kind === "param" && (this.keepFnArgs || !binding[markForRemoval])) {
continue;
if (binding.kind === "param" && (_this.keepFnArgs || !binding[markForRemoval])) {
return "continue";
} else if (binding.path.isVariableDeclarator()) {
const declaration = binding.path.parentPath;
const maybeBlockParent = declaration.parentPath;
var declaration = binding.path.parentPath;
var maybeBlockParent = declaration.parentPath;
if (maybeBlockParent && maybeBlockParent.isForXStatement({

@@ -253,10 +279,10 @@ left: declaration.node

// Can't remove if in a for-in/for-of/for-await statement `for (var x in wat)`.
continue;
return "continue";
}
} else if (!scope.isPure(binding.path.node)) {
// TODO: AssignmentPattern are marked as impure and unused ids aren't removed yet
continue;
return "continue";
} else if (binding.path.isFunctionExpression() || binding.path.isClassExpression()) {
// `bar(function foo() {})` foo is not referenced but it's used.
continue;
return "continue";
} else if (

@@ -267,9 +293,9 @@ // ClassDeclaration has binding in two scopes

binding.path.isClassDeclaration() && binding.path === scope.path) {
continue;
return "continue";
}
const mutations = [];
let bail = false;
var mutations = [];
var bail = false;
// Make sure none of the assignments value is used
binding.constantViolations.forEach(p => {
binding.constantViolations.forEach(function (p) {
if (bail || p === binding.path) {

@@ -284,5 +310,9 @@ return;

if (p.isAssignmentExpression() && !p.get("right").isPure()) {
mutations.push(() => p.replaceWith(p.get("right")));
mutations.push(function () {
return p.replaceWith(p.get("right"));
});
} else {
mutations.push(() => removeOrVoid(p));
mutations.push(function () {
return removeOrVoid(p);
});
}

@@ -292,3 +322,3 @@ });

if (bail) {
continue;
return "continue";
}

@@ -299,3 +329,3 @@

// deopt for object and array pattern
continue;
return "continue";
}

@@ -309,67 +339,77 @@

if (binding.path.parentPath.node.declarations.length !== 1) {
continue;
return "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(f => f());
mutations.forEach(function (f) {
return f();
});
scope.removeBinding(name);
} else if (binding.constant) {
if (binding.path.isFunctionDeclaration() || binding.path.isVariableDeclarator() && binding.path.get("init").isFunction()) {
const fun = binding.path.isFunctionDeclaration() ? binding.path : binding.path.get("init");
let allInside = true;
var _iteratorNormalCompletion3 = true;
var _didIteratorError3 = false;
var _iteratorError3 = undefined;
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;
try {
for (var _iterator3 = binding.referencePaths[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
const ref = _step3.value;
try {
for (var _iterator3 = binding.referencePaths[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
var ref = _step3.value;
if (!ref.find(p => p.node === fun.node)) {
allInside = false;
break;
if (!ref.find(function (p) {
return p.node === fun.node;
})) {
allInside = false;
break;
}
}
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} catch (err) {
_didIteratorError3 = true;
_iteratorError3 = err;
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
try {
if (!_iteratorNormalCompletion3 && _iterator3.return) {
_iterator3.return();
}
} finally {
if (_didIteratorError3) {
throw _iteratorError3;
}
}
}
}
if (allInside) {
scope.removeBinding(name);
updateReferences(binding.path, this);
removeOrVoid(binding.path);
continue;
}
if (allInside) {
scope.removeBinding(name);
updateReferences(binding.path, _this);
removeOrVoid(binding.path);
return {
v: "continue"
};
}
}();
if (typeof _ret4 === "object") return _ret4.v;
}
if (binding.references === 1 && binding.kind !== "param" && binding.kind !== "module" && binding.constant) {
let replacement = binding.path.node;
let replacementPath = binding.path;
let isReferencedBefore = false;
var replacement = binding.path.node;
var replacementPath = binding.path;
var isReferencedBefore = false;
const refPath = binding.referencePaths[0];
var refPath = binding.referencePaths[0];
if (t.isVariableDeclarator(replacement)) {
const _prevSiblings = prevSiblings(replacementPath);
var _prevSiblings = prevSiblings(replacementPath);
// traverse ancestors of a reference checking if it's before declaration
forEachAncestor(refPath, ancestor => {
forEachAncestor(refPath, function (ancestor) {
if (_prevSiblings.indexOf(ancestor) > -1) {

@@ -384,3 +424,3 @@ isReferencedBefore = true;

if (isReferencedBefore && refPath.scope !== binding.scope) {
continue;
return "continue";
}

@@ -395,3 +435,3 @@

if (!replacementPath.get("id").isIdentifier()) {
continue;
return "continue";
}

@@ -402,13 +442,13 @@ replacementPath = replacementPath.get("init");

if (!replacement) {
continue;
return "continue";
}
if (!scope.isPure(replacement, true) && !isReferencedBefore) {
continue;
return "continue";
}
let bail = false;
var _bail = false;
if (replacementPath.isIdentifier()) {
const binding = scope.getBinding(replacement.name);
var _binding2 = scope.getBinding(replacement.name);
// the reference should be in the same scope

@@ -418,3 +458,3 @@ // and the replacement should be a constant - this is to

// https://github.com/babel/minify/issues/685
bail = !(binding && refPath.scope.getBinding(replacement.name) === binding && binding.constantViolations.length === 0);
_bail = !(_binding2 && refPath.scope.getBinding(replacement.name) === _binding2 && _binding2.constantViolations.length === 0);
} else {

@@ -426,9 +466,11 @@ replacementPath.traverse({

ReferencedIdentifier({ node }) {
if (bail) {
ReferencedIdentifier(_ref2) {
var node = _ref2.node;
if (_bail) {
return;
}
const binding = scope.getBinding(node.name);
var binding = scope.getBinding(node.name);
if (binding && refPath.scope.getBinding(node.name) === binding) {
bail = binding.constantViolations.length > 0;
_bail = binding.constantViolations.length > 0;
}

@@ -439,7 +481,7 @@ }

if (bail) {
continue;
if (_bail) {
return "continue";
}
let parent = binding.path.parent;
var parent = binding.path.parent;
if (t.isVariableDeclaration(parent)) {

@@ -454,4 +496,6 @@ parent = binding.path.parentPath.parent;

// which would also could affect correctness in that they are not the same reference.
let mayLoop = false;
const sharesRoot = refPath.find(({ node }) => {
var mayLoop = false;
var sharesRoot = refPath.find(function (_ref3) {
var node = _ref3.node;
if (!mayLoop) {

@@ -464,8 +508,10 @@ mayLoop = t.isWhileStatement(node) || t.isFor(node) || t.isFunction(node);

// Anything that inherits from Object.
const isObj = n => t.isFunction(n) || t.isObjectExpression(n) || t.isArrayExpression(n);
var isObj = function isObj(n) {
return t.isFunction(n) || t.isObjectExpression(n) || t.isArrayExpression(n);
};
const isReplacementObj = isObj(replacement) || some(replacement, isObj);
var isReplacementObj = isObj(replacement) || some(replacement, isObj);
if (!sharesRoot || isReplacementObj && mayLoop) {
continue;
return "continue";
}

@@ -480,3 +526,3 @@

// all the binary "in" operations
let inExpression = replacementPath.isBinaryExpression({
var inExpression = replacementPath.isBinaryExpression({
operator: "in"

@@ -500,6 +546,6 @@ });

if (inExpression) {
continue;
return "continue";
}
const replaced = replace(binding.referencePaths[0], {
var replaced = replace(binding.referencePaths[0], {
binding,

@@ -519,2 +565,8 @@ scope,

}
};
for (var name in scope.bindings) {
var _ret3 = _loop3(name);
if (_ret3 === "continue") continue;
} // end-for-of

@@ -526,8 +578,8 @@ }

BlockStatement(path) {
const paths = path.get("body");
var paths = path.get("body");
let purge = false;
var purge = false;
for (let i = 0; i < paths.length; i++) {
const p = paths[i];
for (var i = 0; i < paths.length; i++) {
var p = paths[i];

@@ -548,3 +600,3 @@ if (!purge && p.isCompletionStatement()) {

ReturnStatement(path) {
const node = path.node;
var node = path.node;

@@ -570,4 +622,4 @@ if (!path.inList) {

let noNext = true;
let parentPath = path.parentPath;
var noNext = true;
var parentPath = path.parentPath;
while (parentPath && !parentPath.isFunction() && noNext) {

@@ -580,3 +632,3 @@ // https://github.com/babel/minify/issues/265

const nextPath = parentPath.getSibling(parentPath.key + 1);
var nextPath = parentPath.getSibling(parentPath.key + 1);
if (nextPath.node) {

@@ -606,5 +658,5 @@ if (nextPath.isReturnStatement()) {

ConditionalExpression(path) {
const node = path.node;
var node = path.node;
const evaluateTest = path.get("test").evaluateTruthy();
var evaluateTest = path.get("test").evaluateTruthy();
if (evaluateTest === true) {

@@ -619,14 +671,14 @@ path.replaceWith(node.consequent);

exit(path) {
const evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz });
var evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz });
if (!evaluated.confident) return;
const discriminant = evaluated.value;
const cases = path.get("cases");
var discriminant = evaluated.value;
var cases = path.get("cases");
let matchingCaseIndex = -1;
let defaultCaseIndex = -1;
var matchingCaseIndex = -1;
var defaultCaseIndex = -1;
for (let i = 0; i < cases.length; i++) {
const test = cases[i].get("test");
for (var i = 0; i < cases.length; i++) {
var test = cases[i].get("test");

@@ -639,3 +691,3 @@ // handle default case

const testResult = evaluate(test, { tdz: this.tdz });
var testResult = evaluate(test, { tdz: this.tdz });

@@ -652,3 +704,3 @@ // if we are not able to deternine a test during

let result;
var result = void 0;

@@ -672,6 +724,6 @@ if (matchingCaseIndex === -1) {

// will be again removed by DCE
replaceSwitch([...extractVars(path), ...result.statements]);
replaceSwitch([].concat(_toConsumableArray(extractVars(path)), _toConsumableArray(result.statements)));
function getStatementsUntilBreak(start) {
const result = {
var result = {
bail: false,

@@ -681,7 +733,7 @@ statements: []

for (let i = start; i < cases.length; i++) {
const consequent = cases[i].get("consequent");
for (var _i = start; _i < cases.length; _i++) {
var consequent = cases[_i].get("consequent");
for (let j = 0; j < consequent.length; j++) {
const _isBreaking = isBreaking(consequent[j], path);
for (var j = 0; j < consequent.length; j++) {
var _isBreaking = isBreaking(consequent[j], path);
if (_isBreaking.bail) {

@@ -705,10 +757,10 @@ result.bail = true;

function replaceSwitch(statements) {
let isBlockRequired = false;
var isBlockRequired = false;
for (let i = 0; i < statements.length; i++) {
if (t.isVariableDeclaration(statements[i], { kind: "let" })) {
for (var _i2 = 0; _i2 < statements.length; _i2++) {
if (t.isVariableDeclaration(statements[_i2], { kind: "let" })) {
isBlockRequired = true;
break;
}
if (t.isVariableDeclaration(statements[i], { kind: "const" })) {
if (t.isVariableDeclaration(statements[_i2], { kind: "const" })) {
isBlockRequired = true;

@@ -729,4 +781,4 @@ break;

WhileStatement(path) {
const test = path.get("test");
const result = evaluate(test, { tdz: this.tdz });
var test = path.get("test");
var result = evaluate(test, { tdz: this.tdz });
if (result.confident && test.isPure() && !result.value) {

@@ -738,6 +790,6 @@ path.remove();

ForStatement(path) {
const test = path.get("test");
var test = path.get("test");
if (!test.isPure()) return;
const result = evaluate(test, { tdz: this.tdz });
var result = evaluate(test, { tdz: this.tdz });
if (result.confident) {

@@ -753,9 +805,9 @@ if (result.value) {

DoWhileStatement(path) {
const test = path.get("test");
const result = evaluate(test, { tdz: this.tdz });
var test = path.get("test");
var result = evaluate(test, { tdz: this.tdz });
if (result.confident && test.isPure() && !result.value) {
const body = path.get("body");
var body = path.get("body");
if (body.isBlockStatement()) {
const stmts = body.get("body");
var stmts = body.get("body");
var _iteratorNormalCompletion4 = true;

@@ -767,7 +819,7 @@ var _didIteratorError4 = false;

for (var _iterator4 = stmts[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
const stmt = _step4.value;
var stmt = _step4.value;
const _isBreaking = isBreaking(stmt, path);
var _isBreaking = isBreaking(stmt, path);
if (_isBreaking.bail || _isBreaking.break) return;
const _isContinuing = isContinuing(stmt, path);
var _isContinuing = isContinuing(stmt, path);
if (_isContinuing.bail || isContinuing.continue) return;

@@ -792,5 +844,5 @@ }

} else if (body.isBreakStatement()) {
const _isBreaking = isBreaking(body, path);
if (_isBreaking.bail) return;
if (_isBreaking.break) path.remove();
var _isBreaking2 = isBreaking(body, path);
if (_isBreaking2.bail) return;
if (_isBreaking2.break) path.remove();
} else if (body.isContinueStatement()) {

@@ -811,3 +863,3 @@ return;

const prev = path.parentPath.getSibling(path.parentPath.key - 1);
var prev = path.parentPath.getSibling(path.parentPath.key - 1);
if (!(prev && prev.isVariableDeclaration())) {

@@ -817,3 +869,3 @@ return;

const declars = prev.node.declarations;
var declars = prev.node.declarations;
if (declars.length !== 1 || declars[0].init || declars[0].id.name !== path.get("left").node.name) {

@@ -843,3 +895,3 @@ return;

ForInStatement(path) {
const left = path.get("left");
var left = path.get("left");
if (!left.isIdentifier()) {

@@ -849,3 +901,3 @@ return;

const binding = path.scope.getBinding(left.node.name);
var binding = path.scope.getBinding(left.node.name);
if (!binding) {

@@ -894,3 +946,3 @@ return;

*/
const body = path.get("body");
var body = path.get("body");
if (body.isBlockStatement()) {

@@ -902,11 +954,16 @@ removeUseStrict(body);

IfStatement: {
exit(path, { opts: { tdz = false } = {} }) {
const consequent = path.get("consequent");
const alternate = path.get("alternate");
const test = path.get("test");
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;
const evalResult = evaluate(test, { tdz });
const isPure = test.isPure();
var consequent = path.get("consequent");
var alternate = path.get("alternate");
var test = path.get("test");
const replacements = [];
var evalResult = evaluate(test, { tdz });
var isPure = test.isPure();
var replacements = [];
if (evalResult.confident && !isPure && test.isSequenceExpression()) {

@@ -923,3 +980,3 @@ replacements.push(t.expressionStatement(extractSequenceImpure(test)));

if (evalResult.confident && evalResult.value) {
path.replaceWithMultiple([...replacements, ...toStatements(consequent), ...extractVars(alternate)]);
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(toStatements(consequent)), _toConsumableArray(extractVars(alternate))));
return;

@@ -936,6 +993,6 @@ }

if (alternate.node) {
path.replaceWithMultiple([...replacements, ...toStatements(alternate), ...extractVars(consequent)]);
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(toStatements(alternate)), _toConsumableArray(extractVars(consequent))));
return;
} else {
path.replaceWithMultiple([...replacements, ...extractVars(consequent)]);
path.replaceWithMultiple([].concat(replacements, _toConsumableArray(extractVars(consequent))));
}

@@ -976,12 +1033,18 @@ }

Program: {
exit(path, {
opts: {
// set defaults
optimizeRawSize = false,
keepFnName = false,
keepClassName = false,
keepFnArgs = false,
tdz = false
} = {}
} = {}) {
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;
(traverse.clearCache || traverse.cache.clear)();

@@ -1007,9 +1070,9 @@ path.scope.crawl();

function toStatements(path) {
const node = path.node;
var node = path.node;
if (path.isBlockStatement()) {
let hasBlockScoped = false;
var hasBlockScoped = false;
for (let i = 0; i < node.body.length; i++) {
const bodyNode = node.body[i];
for (var i = 0; i < node.body.length; i++) {
var bodyNode = node.body[i];
if (t.isBlockScoped(bodyNode)) {

@@ -1034,3 +1097,3 @@ hasBlockScoped = true;

function extractVars(path) {
const declarators = [];
var declarators = [];

@@ -1044,7 +1107,9 @@ if (path.isVariableDeclaration({ kind: "var" })) {

for (var _iterator5 = path.node.declarations[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
const decl = _step5.value;
var decl = _step5.value;
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
var bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name))));
declarators.push.apply(declarators, _toConsumableArray(bindingIds.map(function (name) {
return t.variableDeclarator(t.identifier(name));
})));
}

@@ -1077,6 +1142,8 @@ } catch (err) {

for (var _iterator6 = varPath.node.declarations[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
const decl = _step6.value;
var _decl = _step6.value;
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
declarators.push(...bindingIds.map(name => t.variableDeclarator(t.identifier(name))));
var _bindingIds = Object.keys(t.getBindingIdentifiers(_decl.id));
declarators.push.apply(declarators, _toConsumableArray(_bindingIds.map(function (name) {
return t.variableDeclarator(t.identifier(name));
})));
}

@@ -1107,6 +1174,6 @@ } catch (err) {

function replace(path, options) {
const replacement = options.replacement,
replacementPath = options.replacementPath,
scope = options.scope,
binding = options.binding;
var replacement = options.replacement,
replacementPath = options.replacementPath,
scope = options.scope,
binding = options.binding;

@@ -1127,3 +1194,3 @@ // Same name, different binding.

let bail = false;
var bail = false;
traverse(replacement, {

@@ -1145,3 +1212,6 @@ Function(path) {

// Avoid recursion.
if (path.find(({ node }) => node === replacement)) {
if (path.find(function (_ref6) {
var node = _ref6.node;
return node === replacement;
})) {
return;

@@ -1154,4 +1224,4 @@ }

if (replacementPath.isFunctionDeclaration()) {
const fnName = replacementPath.get("id").node.name;
for (let name in replacementPath.scope.bindings) {
var fnName = replacementPath.get("id").node.name;
for (var name in replacementPath.scope.bindings) {
if (name === fnName) {

@@ -1185,6 +1255,6 @@ return;

ReferencedIdentifier(path) {
const node = path.node,
scope = path.scope;
var node = path.node,
scope = path.scope;
const binding = scope.getBinding(node.name);
var binding = scope.getBinding(node.name);

@@ -1195,3 +1265,3 @@ if (!binding || !binding.path.isFunction() || binding.scope === scope || !binding.constant) {

const index = binding.referencePaths.indexOf(path);
var index = binding.referencePaths.indexOf(path);
if (index === -1) {

@@ -1214,3 +1284,3 @@ return;

function removeUnreferencedId(path) {
const id = path.get("id").node;
var id = path.get("id").node;
if (!id) {

@@ -1220,6 +1290,6 @@ return;

const node = path.node,
scope = path.scope;
var node = path.node,
scope = path.scope;
const binding = scope.getBinding(id.name);
var binding = scope.getBinding(id.name);

@@ -1235,3 +1305,5 @@ // Check if shadowed or is not referenced.

function isAncestor(path1, path2) {
return !!path2.findParent(parent => parent === path1);
return !!path2.findParent(function (parent) {
return parent === path1;
});
}

@@ -1252,8 +1324,9 @@

// tells if a "stmt" is a break/continue statement
function isControlTransfer(stmt, path, control = "break") {
function isControlTransfer(stmt, path) {
var control = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "break";
var _break$continue = {
break: "BreakStatement",
continue: "ContinueStatement"
};
const type = _break$continue[control];
},
type = _break$continue[control];

@@ -1263,3 +1336,3 @@ if (!type) {

}
const checker = `is${type}`;
var checker = `is${type}`;

@@ -1270,4 +1343,4 @@ if (stmt[checker]()) {

let isTransferred = false;
let result = {
var isTransferred = false;
var result = {
[control]: false,

@@ -1293,3 +1366,3 @@ bail: false

function _isControlTransfer(cPath, path) {
const label = cPath.get("label");
var label = cPath.get("label");

@@ -1310,3 +1383,3 @@ if (label.node !== null) {

// if they are within the case, we keep them
let labelPath;
var labelPath = void 0;
if (path.scope.getLabel) {

@@ -1317,3 +1390,3 @@ labelPath = getLabel(label.node.name, path);

}
const _isAncestor = isAncestor(labelPath, path);
var _isAncestor = isAncestor(labelPath, path);

@@ -1327,10 +1400,10 @@ return {

// set the flag that it is indeed breaking
let isCTransfer = true;
var isCTransfer = true;
// this flag is to capture
// switch(0) { case 0: while(1) if (x) break; }
let possibleRunTimeControlTransfer = false;
var possibleRunTimeControlTransfer = false;
// and compute if it's breaking the correct thing
let parent = cPath.parentPath;
var parent = cPath.parentPath;

@@ -1382,3 +1455,3 @@ while (parent !== stmt.parentPath) {

function getLabel(name, _path) {
let label,
var label = void 0,
path = _path;

@@ -1395,3 +1468,3 @@ do {

function hasLoopParent(path) {
let parent = path;
var parent = path;
do {

@@ -1406,5 +1479,5 @@ if (parent.isLoop()) {

function extractSequenceImpure(seq) {
const expressions = seq.get("expressions");
const result = [];
for (let i = 0; i < expressions.length; i++) {
var expressions = seq.get("expressions");
var result = [];
for (var i = 0; i < expressions.length; i++) {
if (!expressions[i].isPure()) {

@@ -1411,0 +1484,0 @@ result.push(expressions[i].node);

@@ -5,5 +5,5 @@ "use strict";

const newIssueUrl = "https://github.com/babel/minify/issues/new";
var newIssueUrl = "https://github.com/babel/minify/issues/new";
const useStrict = "use strict";
var useStrict = "use strict";

@@ -22,3 +22,3 @@ /**

const useStricts = getUseStrictDirectives(block);
var useStricts = getUseStrictDirectives(block);

@@ -30,3 +30,3 @@ // early exit

if (useStricts.length > 1) {
for (let i = 1; i < useStricts.length; i++) {
for (var i = 1; i < useStricts.length; i++) {
useStricts[i].remove();

@@ -43,3 +43,5 @@ }

function hasStrictParent(path) {
return path.findParent(parent => parent.isBlockStatement() && isStrict(parent));
return path.findParent(function (parent) {
return parent.isBlockStatement() && isStrict(parent);
});
}

@@ -46,0 +48,0 @@

{
"name": "babel-plugin-minify-dead-code-elimination",
"version": "0.3.0-alpha.6ae655c0",
"version": "0.3.0-alpha.6ceffb8d",
"description": "",

@@ -15,7 +15,7 @@ "keywords": [

"dependencies": {
"babel-helper-evaluate-path": "^0.3.0-alpha.6ae655c0",
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.6ae655c0",
"babel-helper-remove-or-void": "^0.3.0-alpha.6ae655c0",
"babel-helper-evaluate-path": "^0.3.0-alpha.6ceffb8d",
"babel-helper-mark-eval-scopes": "^0.3.0-alpha.6ceffb8d",
"babel-helper-remove-or-void": "^0.3.0-alpha.6ceffb8d",
"lodash.some": "^4.6.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc