babel-plugin-minify-simplify
Advanced tools
Comparing version 0.5.0-alpha.7b176463 to 0.5.0-alpha.85c9b6d8
"use strict"; | ||
var operators = new Set(["+", "-", "*", "%", "<<", ">>", ">>>", "&", "|", "^", "/", "**"]); | ||
var updateOperators = new Set(["+", "-"]); | ||
const operators = new Set(["+", "-", "*", "%", "<<", ">>", ">>>", "&", "|", "^", "/", "**"]); | ||
const updateOperators = new Set(["+", "-"]); | ||
module.exports = function (t) { | ||
module.exports = t => { | ||
function simplify(path) { | ||
var rightExpr = path.get("right"); | ||
var leftExpr = path.get("left"); | ||
const rightExpr = path.get("right"); | ||
const leftExpr = path.get("left"); | ||
@@ -15,7 +15,7 @@ if (path.node.operator !== "=") { | ||
var canBeUpdateExpression = rightExpr.get("right").isNumericLiteral() && rightExpr.get("right").node.value === 1 && updateOperators.has(rightExpr.node.operator); | ||
const canBeUpdateExpression = rightExpr.get("right").isNumericLiteral() && rightExpr.get("right").node.value === 1 && updateOperators.has(rightExpr.node.operator); | ||
if (leftExpr.isMemberExpression()) { | ||
var leftPropNames = getPropNames(leftExpr); | ||
var rightPropNames = getPropNames(rightExpr.get("left")); | ||
const leftPropNames = getPropNames(leftExpr); | ||
const rightPropNames = getPropNames(rightExpr.get("left")); | ||
@@ -31,3 +31,3 @@ if (!leftPropNames || leftPropNames.indexOf(undefined) > -1 || !rightPropNames || rightPropNames.indexOf(undefined) > -1 || !operators.has(rightExpr.node.operator) || !areArraysEqual(leftPropNames, rightPropNames)) { | ||
var newExpression; // special case x=x+1 --> ++x | ||
let newExpression; // special case x=x+1 --> ++x | ||
@@ -51,3 +51,3 @@ if (canBeUpdateExpression) { | ||
function areArraysEqual(arr1, arr2) { | ||
return arr1.every(function (value, index) { | ||
return arr1.every((value, index) => { | ||
return String(value) === String(arr2[index]); | ||
@@ -62,8 +62,8 @@ }); | ||
var obj = path.get("object"); | ||
var prop = path.get("property"); | ||
var propNames = [getName(prop.node)]; | ||
let obj = path.get("object"); | ||
const prop = path.get("property"); | ||
const propNames = [getName(prop.node)]; | ||
while (obj.type === "MemberExpression") { | ||
var node = obj.get("property").node; | ||
const node = obj.get("property").node; | ||
@@ -70,0 +70,0 @@ if (node) { |
@@ -1,30 +0,24 @@ | ||
var h = require("./helpers"); | ||
"use strict"; | ||
var PatternMatch = require("./pattern-match"); | ||
const h = require("./helpers"); | ||
module.exports = function (t) { | ||
const PatternMatch = require("./pattern-match"); | ||
module.exports = t => { | ||
// small abstractions | ||
var not = function not(node) { | ||
return t.unaryExpression("!", node); | ||
}; | ||
const not = node => t.unaryExpression("!", node); | ||
var notnot = function notnot(node) { | ||
return not(not(node)); | ||
}; | ||
const notnot = node => not(not(node)); | ||
var or = function or(a, b) { | ||
return t.logicalExpression("||", a, b); | ||
}; | ||
const or = (a, b) => t.logicalExpression("||", a, b); | ||
var and = function and(a, b) { | ||
return t.logicalExpression("&&", a, b); | ||
}; | ||
const and = (a, b) => t.logicalExpression("&&", a, b); | ||
function simplifyPatterns(path) { | ||
var test = path.get("test"); | ||
var consequent = path.get("consequent"); | ||
var alternate = path.get("alternate"); | ||
const test = path.get("test"); | ||
const consequent = path.get("consequent"); | ||
const alternate = path.get("alternate"); | ||
var _h$typeSymbols = h.typeSymbols(t), | ||
EX = _h$typeSymbols.Expression; // Convention: | ||
const _h$typeSymbols = h.typeSymbols(t), | ||
EX = _h$typeSymbols.Expression; // Convention: | ||
// =============== | ||
@@ -34,16 +28,4 @@ // for each pattern [test, consequent, alternate, handler(expr, cons, alt)] | ||
var matcher = new PatternMatch([[EX, true, false, function (e) { | ||
return notnot(e); | ||
}], [EX, false, true, function (e) { | ||
return not(e); | ||
}], [EX, true, EX, function (e, c, a) { | ||
return or(notnot(e), a); | ||
}], [EX, false, EX, function (e, c, a) { | ||
return and(not(e), a); | ||
}], [EX, EX, true, function (e, c) { | ||
return or(not(e), c); | ||
}], [EX, EX, false, function (e, c) { | ||
return and(notnot(e), c); | ||
}]]); | ||
var result = matcher.match([test, consequent, alternate], h.isPatternMatchesPath(t)); | ||
const matcher = new PatternMatch([[EX, true, false, e => notnot(e)], [EX, false, true, e => not(e)], [EX, true, EX, (e, c, a) => or(notnot(e), a)], [EX, false, EX, (e, c, a) => and(not(e), a)], [EX, EX, true, (e, c) => or(not(e), c)], [EX, EX, false, (e, c) => and(notnot(e), c)]]); | ||
const result = matcher.match([test, consequent, alternate], h.isPatternMatchesPath(t)); | ||
@@ -50,0 +32,0 @@ if (result.match) { |
"use strict"; | ||
var VOID_0 = function VOID_0(t) { | ||
return t.unaryExpression("void", t.numericLiteral(0), true); | ||
}; // Types as Symbols - for comparing types | ||
const VOID_0 = t => t.unaryExpression("void", t.numericLiteral(0), true); | ||
const evaluate = require("babel-helper-evaluate-path"); // Types as Symbols - for comparing types | ||
var types = {}; // This is a test key which is used to avoid Object.keys check | ||
const types = {}; // This is a test key which is used to avoid Object.keys check | ||
// Object.keys() check is really expensive | ||
// https://gist.github.com/vigneshshanmugam/c766550ecd02292dcdfbf0bf013b9d3d | ||
var testKey = "Expression"; | ||
const testKey = "Expression"; | ||
var typeSymbols = function typeSymbols(t) { | ||
const typeSymbols = t => { | ||
// don't recompute | ||
@@ -20,3 +20,3 @@ if (types[testKey] !== undefined) { | ||
t.TYPES.forEach(function (type) { | ||
t.TYPES.forEach(type => { | ||
types[type] = Symbol.for(type); | ||
@@ -27,27 +27,23 @@ }); | ||
var isNodeOfType = function isNodeOfType(t, node, typeSymbol) { | ||
return typeof typeSymbol !== "symbol" ? false : t["is" + Symbol.keyFor(typeSymbol)](node); | ||
}; | ||
const isNodeOfType = (t, node, typeSymbol) => typeof typeSymbol !== "symbol" ? false : t["is" + Symbol.keyFor(typeSymbol)](node); | ||
var isPatternMatchesPath = function isPatternMatchesPath(t) { | ||
return function _isPatternMatchesPath(patternValue, inputPath) { | ||
if (Array.isArray(patternValue)) { | ||
for (var i = 0; i < patternValue.length; i++) { | ||
if (_isPatternMatchesPath(patternValue[i], inputPath)) { | ||
return true; | ||
} | ||
const isPatternMatchesPath = t => function _isPatternMatchesPath(patternValue, inputPath) { | ||
if (Array.isArray(patternValue)) { | ||
for (let i = 0; i < patternValue.length; i++) { | ||
if (_isPatternMatchesPath(patternValue[i], inputPath)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
if (typeof patternValue === "function") { | ||
return patternValue(inputPath); | ||
} | ||
return false; | ||
} | ||
if (isNodeOfType(t, inputPath.node, patternValue)) return true; | ||
var evalResult = inputPath.evaluate(); | ||
if (!evalResult.confident || !inputPath.isPure()) return false; | ||
return evalResult.value === patternValue; | ||
}; | ||
if (typeof patternValue === "function") { | ||
return patternValue(inputPath); | ||
} | ||
if (isNodeOfType(t, inputPath.node, patternValue)) return true; | ||
const evalResult = evaluate(inputPath); | ||
if (!evalResult.confident || !inputPath.isPure()) return false; | ||
return evalResult.value === patternValue; | ||
}; | ||
@@ -54,0 +50,0 @@ |
@@ -1,9 +0,11 @@ | ||
var REPLACED = Symbol("replaced"); | ||
"use strict"; | ||
var h = require("./helpers"); | ||
const REPLACED = Symbol("replaced"); | ||
module.exports = function (t) { | ||
const h = require("./helpers"); | ||
module.exports = t => { | ||
function mergeNestedIfs(path) { | ||
var consequent = path.get("consequent"); | ||
var alternate = path.get("alternate"); // not nested if | ||
const consequent = path.get("consequent"); | ||
const alternate = path.get("alternate"); // not nested if | ||
@@ -13,3 +15,3 @@ if (!consequent.isIfStatement()) return; // there are no alternate nodes in both the if statements (nested) | ||
if (alternate.node || consequent.get("alternate").node) return; | ||
var test = path.get("test"); | ||
const test = path.get("test"); | ||
test.replaceWith(t.logicalExpression("&&", test.node, consequent.get("test").node)); | ||
@@ -21,6 +23,6 @@ consequent.replaceWith(t.clone(consequent.get("consequent").node)); | ||
function toGuardedExpression(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
if (node.consequent && !node.alternate && node.consequent.type === "ExpressionStatement") { | ||
var op = "&&"; | ||
let op = "&&"; | ||
@@ -41,3 +43,3 @@ if (t.isUnaryExpression(node.test, { | ||
function toTernary(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -52,3 +54,3 @@ if (t.isExpressionStatement(node.consequent) && t.isExpressionStatement(node.alternate)) { | ||
function toConditional(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -70,3 +72,3 @@ if (t.isReturnStatement(node.consequent) && t.isReturnStatement(node.alternate)) { | ||
function toReturn(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -124,3 +126,3 @@ if (!path.getSibling(path.key + 1).node && path.parentPath && path.parentPath.parentPath && path.parentPath.parentPath.isFunction()) { | ||
var next = path.getSibling(path.key + 1); // If the next satatement(s) is an if statement and we can simplify that | ||
let next = path.getSibling(path.key + 1); // If the next satatement(s) is an if statement and we can simplify that | ||
// to potentailly be an expression (or a return) then this will make it | ||
@@ -144,3 +146,3 @@ // easier merge. | ||
if (t.isReturnStatement(node.consequent) && !node.alternate && next.isReturnStatement()) { | ||
var nextArg = next.node.argument || h.VOID_0(t); | ||
const nextArg = next.node.argument || h.VOID_0(t); | ||
next.remove(); | ||
@@ -153,3 +155,3 @@ path.replaceWith(t.returnStatement(t.conditionalExpression(node.test, node.consequent.argument || h.VOID_0(t), nextArg))); | ||
if (path.parentPath && path.parentPath.parentPath && path.parentPath.parentPath.isFunction() && !path.getSibling(path.key + 2).node && t.isReturnStatement(node.consequent) && !node.alternate && next.isExpressionStatement()) { | ||
var nextExpr = next.node.expression; | ||
const nextExpr = next.node.expression; | ||
next.remove(); | ||
@@ -169,18 +171,14 @@ | ||
function removeUnnecessaryElse(path) { | ||
var node = path.node; | ||
var consequent = path.get("consequent"); | ||
var alternate = path.get("alternate"); | ||
const node = path.node; | ||
const consequent = path.get("consequent"); | ||
const alternate = path.get("alternate"); | ||
if (consequent.node && alternate.node && (consequent.isReturnStatement() || consequent.isBlockStatement() && t.isReturnStatement(consequent.node.body[consequent.node.body.length - 1])) && ( // don't hoist declarations | ||
// TODO: validate declarations after fixing scope issues | ||
alternate.isBlockStatement() ? !alternate.get("body").some(function (stmt) { | ||
return stmt.isVariableDeclaration({ | ||
kind: "let" | ||
}) || stmt.isVariableDeclaration({ | ||
kind: "const" | ||
}); | ||
}) : true)) { | ||
path.insertAfter(alternate.isBlockStatement() ? alternate.node.body.map(function (el) { | ||
return t.clone(el); | ||
}) : t.clone(alternate.node)); | ||
alternate.isBlockStatement() ? !alternate.get("body").some(stmt => stmt.isVariableDeclaration({ | ||
kind: "let" | ||
}) || stmt.isVariableDeclaration({ | ||
kind: "const" | ||
})) : true)) { | ||
path.insertAfter(alternate.isBlockStatement() ? alternate.node.body.map(el => t.clone(el)) : t.clone(alternate.node)); | ||
node.alternate = null; | ||
@@ -193,3 +191,3 @@ return REPLACED; | ||
// ordered | ||
var transforms = [toGuardedExpression, toTernary, toConditional, toReturn, removeUnnecessaryElse]; // run each of the replacement till we replace something | ||
const transforms = [toGuardedExpression, toTernary, toConditional, toReturn, removeUnnecessaryElse]; // run each of the replacement till we replace something | ||
// which is identified by the Symbol(REPLACED) that each of the | ||
@@ -199,3 +197,3 @@ // functions return when they replace something | ||
for (var _i = 0; _i < transforms.length; _i++) { | ||
var transform = transforms[_i]; | ||
const transform = transforms[_i]; | ||
@@ -212,3 +210,3 @@ if (transform(path) === REPLACED) { | ||
function switchConsequent(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -236,3 +234,3 @@ if (!node.alternate) { | ||
function conditionalReturnToGuards(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -243,8 +241,8 @@ if (!path.inList || !path.get("consequent").isBlockStatement() || node.alternate) { | ||
var ret; | ||
var test; | ||
var exprs = []; | ||
var statements = node.consequent.body; | ||
let ret; | ||
let test; | ||
const exprs = []; | ||
const statements = node.consequent.body; | ||
for (var i = 0, statement; statement = statements[i]; i++) { | ||
for (let i = 0, statement; statement = statements[i]; i++) { | ||
if (t.isExpressionStatement(statement)) { | ||
@@ -278,4 +276,4 @@ exprs.push(statement.expression); | ||
exprs.push(test); | ||
var expr = exprs.length === 1 ? exprs[0] : t.sequenceExpression(exprs); | ||
var replacement = t.logicalExpression("&&", node.test, expr); | ||
const expr = exprs.length === 1 ? exprs[0] : t.sequenceExpression(exprs); | ||
const replacement = t.logicalExpression("&&", node.test, expr); | ||
path.replaceWith(t.ifStatement(replacement, ret, null)); | ||
@@ -282,0 +280,0 @@ } |
433
lib/index.js
"use strict"; | ||
function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
module.exports = function (_ref) { | ||
var t = _ref.types; | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var flipExpressions = require("babel-helper-flip-expressions")(t); | ||
module.exports = ({ | ||
types: t | ||
}) => { | ||
const flipExpressions = require("babel-helper-flip-expressions")(t); | ||
var toMultipleSequenceExpressions = require("babel-helper-to-multiple-sequence-expressions")(t); | ||
const toMultipleSequenceExpressions = require("babel-helper-to-multiple-sequence-expressions")(t); | ||
var ifStatement = require("./if-statement")(t); | ||
const ifStatement = require("./if-statement")(t); | ||
var conditionalExpression = require("./conditional-expression")(t); | ||
const conditionalExpression = require("./conditional-expression")(t); | ||
var logicalExpression = require("./logical-expression")(t); | ||
const logicalExpression = require("./logical-expression")(t); | ||
var assignmentExpression = require("./assignment-expression")(t); | ||
const assignmentExpression = require("./assignment-expression")(t); | ||
var VOID_0 = t.unaryExpression("void", t.numericLiteral(0), true); | ||
var condExprSeen = Symbol("condExprSeen"); | ||
var seqExprSeen = Symbol("seqExprSeen"); | ||
var shouldRevisit = Symbol("shouldRevisit"); | ||
const VOID_0 = t.unaryExpression("void", t.numericLiteral(0), true); | ||
const condExprSeen = Symbol("condExprSeen"); | ||
const seqExprSeen = Symbol("seqExprSeen"); | ||
const shouldRevisit = Symbol("shouldRevisit"); | ||
return { | ||
@@ -61,3 +63,3 @@ name: "minify-simplify", | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -68,3 +70,3 @@ if (node.operator !== "!" || flipExpressions.hasSeen(node)) { | ||
var expr = node.argument; // We need to make sure that the return type will always be boolean. | ||
const expr = node.argument; // We need to make sure that the return type will always be boolean. | ||
@@ -80,3 +82,3 @@ if (!(t.isLogicalExpression(expr) || t.isConditionalExpression(expr) || t.isBinaryExpression(expr))) { | ||
if (flipExpressions.shouldFlip(expr, 1)) { | ||
var newNode = flipExpressions.flip(expr); | ||
const newNode = flipExpressions.flip(expr); | ||
path.replaceWith(newNode); | ||
@@ -86,3 +88,3 @@ } | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -97,4 +99,4 @@ if (node.operator !== "!") { | ||
var seq = node.argument.expressions; | ||
var expr = seq[seq.length - 1]; | ||
const seq = node.argument.expressions; | ||
const expr = seq[seq.length - 1]; | ||
seq[seq.length - 1] = t.unaryExpression("!", expr, true); | ||
@@ -104,3 +106,3 @@ path.replaceWith(node.argument); | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -115,3 +117,3 @@ if (node.operator !== "!") { | ||
var cond = node.argument; | ||
const cond = node.argument; | ||
cond.alternate = t.unaryExpression("!", cond.alternate, true); | ||
@@ -122,2 +124,10 @@ cond.consequent = t.unaryExpression("!", cond.consequent, true); | ||
}, | ||
BinaryExpression(path) { | ||
if (["!=", "=="].indexOf(path.node.operator) !== -1) { | ||
undefinedToNull(path.get("left")); | ||
undefinedToNull(path.get("right")); | ||
} | ||
}, | ||
LogicalExpression: { | ||
@@ -131,3 +141,3 @@ exit: logicalExpression.simplifyPatterns | ||
function flipIfOrConditional(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -141,5 +151,5 @@ if (!path.get("test").isLogicalExpression()) { | ||
node.test = flipExpressions.flip(node.test); | ||
var _ref2 = [node.consequent, node.alternate]; | ||
node.alternate = _ref2[0]; | ||
node.consequent = _ref2[1]; | ||
var _ref = [node.consequent, node.alternate]; | ||
node.alternate = _ref[0]; | ||
node.consequent = _ref[1]; | ||
} | ||
@@ -154,16 +164,16 @@ }, conditionalExpression.simplifyPatterns], | ||
var mutations = []; | ||
var firstLeft = null; | ||
var operator = null; | ||
const mutations = []; | ||
let firstLeft = null; | ||
let operator = null; | ||
function visit(path) { | ||
if (path.isConditionalExpression()) { | ||
var _bail = visit(path.get("consequent")); | ||
let bail = visit(path.get("consequent")); | ||
if (_bail) { | ||
if (bail) { | ||
return true; | ||
} | ||
_bail = visit(path.get("alternate")); | ||
return _bail; | ||
bail = visit(path.get("alternate")); | ||
return bail; | ||
} | ||
@@ -181,3 +191,3 @@ | ||
var left = path.get("left").node; | ||
const left = path.get("left").node; | ||
@@ -190,8 +200,6 @@ if (firstLeft == null) { | ||
mutations.push(function () { | ||
return path.replaceWith(path.get("right").node); | ||
}); | ||
mutations.push(() => path.replaceWith(path.get("right").node)); | ||
} | ||
var bail = visit(topPath); | ||
const bail = visit(topPath); | ||
@@ -202,5 +210,3 @@ if (bail) { | ||
mutations.forEach(function (f) { | ||
return f(); | ||
}); | ||
mutations.forEach(f => f()); | ||
topPath.replaceWith(t.assignmentExpression(operator, firstLeft, topPath.node)); | ||
@@ -211,3 +217,3 @@ }, // bar ? void 0 : void 0 | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -221,3 +227,3 @@ if (isVoid0(node.consequent) && isVoid0(node.alternate)) { | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -229,7 +235,7 @@ if (node[condExprSeen] || !isVoid0(node.consequent)) { | ||
node[condExprSeen] = true; | ||
var tests = [node.test]; | ||
var mutations = []; | ||
var alt; | ||
const tests = [node.test]; | ||
const mutations = []; | ||
let alt; | ||
var _loop = function _loop(next) { | ||
for (let next = path.get("alternate"); next.isConditionalExpression(); next = next.get("alternate")) { | ||
next.node[condExprSeen] = true; | ||
@@ -240,15 +246,7 @@ alt = next.node.alternate; | ||
tests.push(next.node.test); | ||
mutations.push(function () { | ||
return next.remove(); | ||
}); | ||
mutations.push(() => next.remove()); | ||
} else { | ||
alt = next.node; | ||
return "break"; | ||
break; | ||
} | ||
}; | ||
for (var next = path.get("alternate"); next.isConditionalExpression(); next = next.get("alternate")) { | ||
var _ret = _loop(next); | ||
if (_ret === "break") break; | ||
} | ||
@@ -260,5 +258,3 @@ | ||
var test = tests.reduce(function (expr, curTest) { | ||
return t.logicalExpression("||", expr, curTest); | ||
}); | ||
const test = tests.reduce((expr, curTest) => t.logicalExpression("||", expr, curTest)); | ||
path.replaceWith(t.conditionalExpression(test, VOID_0, alt)); | ||
@@ -271,3 +267,3 @@ }] | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -278,4 +274,4 @@ if (node.declarations.length < 2) { | ||
var inits = []; | ||
var empty = []; | ||
const inits = []; | ||
const empty = []; | ||
var _iteratorNormalCompletion = true; | ||
@@ -287,8 +283,8 @@ var _didIteratorError = false; | ||
for (var _iterator = node.declarations[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _decl = _step.value; | ||
const decl = _step.value; | ||
if (!_decl.init) { | ||
empty.push(_decl); | ||
if (!decl.init) { | ||
empty.push(decl); | ||
} else { | ||
inits.push(_decl); | ||
inits.push(decl); | ||
} | ||
@@ -341,3 +337,3 @@ } // This is based on exprimintation but there is a significant | ||
enter(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -348,13 +344,13 @@ if (!path.inList || node.init && !t.isExpression(node.init)) { | ||
var prev = path.getSibling(path.key - 1); | ||
var consumed = false; | ||
const prev = path.getSibling(path.key - 1); | ||
let consumed = false; | ||
if (prev.isVariableDeclaration()) { | ||
var referencedOutsideLoop = false; // we don't care if vars are referenced outside the loop as they are fn scope | ||
let referencedOutsideLoop = false; // we don't care if vars are referenced outside the loop as they are fn scope | ||
if (prev.node.kind === "let" || prev.node.kind === "const") { | ||
var ids = Object.keys(prev.getBindingIdentifiers()); | ||
const ids = Object.keys(prev.getBindingIdentifiers()); | ||
idloop: for (var i = 0; i < ids.length; i++) { | ||
var binding = prev.scope.bindings[ids[i]]; // TODO | ||
idloop: for (let i = 0; i < ids.length; i++) { | ||
const binding = prev.scope.bindings[ids[i]]; // TODO | ||
// Temporary Fix | ||
@@ -369,5 +365,5 @@ // if there is no binding, we assume it is referenced outside | ||
var refs = binding.referencePaths; | ||
const refs = binding.referencePaths; | ||
for (var j = 0; j < refs.length; j++) { | ||
for (let j = 0; j < refs.length; j++) { | ||
if (!isAncestor(path, refs[j])) { | ||
@@ -386,3 +382,3 @@ referencedOutsideLoop = true; | ||
} else if (prev.isExpressionStatement()) { | ||
var expr = prev.node.expression; | ||
const expr = prev.node.expression; | ||
@@ -409,3 +405,3 @@ if (node.init) { | ||
exit(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -417,3 +413,3 @@ if (!node.test) { | ||
if (!path.get("body").isBlockStatement()) { | ||
var bodyNode = path.get("body").node; | ||
const bodyNode = path.get("body").node; | ||
@@ -443,9 +439,9 @@ if (!t.isIfStatement(bodyNode)) { | ||
var statements = node.body.body; | ||
var exprs = []; | ||
var ifStatement = null; | ||
var breakAt = null; | ||
var i = 0; | ||
const statements = node.body.body; | ||
const exprs = []; | ||
let ifStatement = null; | ||
let breakAt = null; | ||
let i = 0; | ||
for (var statement; statement = statements[i]; i++) { | ||
for (let statement; statement = statements[i]; i++) { | ||
if (t.isIfStatement(statement)) { | ||
@@ -479,7 +475,7 @@ if (t.isBreakStatement(statement.consequent, { | ||
var rest = []; | ||
const rest = []; | ||
if (breakAt === "consequent") { | ||
if (t.isBlockStatement(ifStatement.alternate)) { | ||
rest.push.apply(rest, _toConsumableArray(ifStatement.alternate.body)); | ||
rest.push(...ifStatement.alternate.body); | ||
} else if (ifStatement.alternate) { | ||
@@ -490,3 +486,3 @@ rest.push(ifStatement.alternate); | ||
if (t.isBlockStatement(ifStatement.consequent)) { | ||
rest.push.apply(rest, _toConsumableArray(ifStatement.consequent.body)); | ||
rest.push(...ifStatement.consequent.body); | ||
} else if (ifStatement.consequent) { | ||
@@ -497,5 +493,5 @@ rest.push(ifStatement.consequent); | ||
rest.push.apply(rest, _toConsumableArray(statements.slice(i + 1))); | ||
var test = breakAt === "consequent" ? t.unaryExpression("!", ifStatement.test, true) : ifStatement.test; | ||
var expr; | ||
rest.push(...statements.slice(i + 1)); | ||
const test = breakAt === "consequent" ? t.unaryExpression("!", ifStatement.test, true) : ifStatement.test; | ||
let expr; | ||
@@ -527,4 +523,4 @@ if (exprs.length === 1) { | ||
this.fitsInSlidingWindow = path.getSource().length / 10 < 33000; | ||
var node = path.node; | ||
var statements = toMultipleSequenceExpressions(node.body); | ||
const node = path.node; | ||
const statements = toMultipleSequenceExpressions(node.body); | ||
@@ -540,9 +536,9 @@ if (!statements.length) { | ||
enter(path) { | ||
var node = path.node, | ||
parent = path.parent; | ||
var top = []; | ||
var bottom = []; | ||
const node = path.node, | ||
parent = path.parent; | ||
const top = []; | ||
const bottom = []; | ||
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]; | ||
@@ -556,3 +552,3 @@ if (t.isFunctionDeclaration(bodyNode)) { | ||
var statements = top.concat(toMultipleSequenceExpressions(bottom)); | ||
const statements = top.concat(toMultipleSequenceExpressions(bottom)); | ||
@@ -575,4 +571,4 @@ if (!statements.length) { | ||
exit(path) { | ||
var node = path.node, | ||
parent = path.parent; | ||
const node = path.node, | ||
parent = path.parent; | ||
@@ -600,3 +596,3 @@ if (t.isArrowFunctionExpression(parent) && node.body.length === 1 && t.isReturnStatement(node.body[0]) && node.body[0].argument !== null) { | ||
var statements = node.body; | ||
const statements = node.body; | ||
@@ -613,5 +609,5 @@ if (!statements.length) { | ||
for (var _iterator2 = statements[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _statement = _step2.value; | ||
const statement = _step2.value; | ||
if (!t.isExpressionStatement(_statement)) { | ||
if (!t.isExpressionStatement(statement)) { | ||
return; | ||
@@ -645,3 +641,3 @@ } | ||
function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -670,3 +666,3 @@ if (!path.parentPath.parentPath.isFunction() || path.getSibling(path.key + 1).node) { | ||
WhileStatement(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
path.replaceWith(t.forStatement(null, node.test, null, node.body)); | ||
@@ -685,3 +681,3 @@ }, | ||
node[seqExprSeen] = true; | ||
var ret = []; | ||
const ret = []; | ||
var _iteratorNormalCompletion3 = true; | ||
@@ -693,8 +689,8 @@ var _didIteratorError3 = false; | ||
for (var _iterator3 = node.expressions[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _n = _step3.value; | ||
const n = _step3.value; | ||
if (t.isSequenceExpression(_n)) { | ||
ret.push.apply(ret, _toConsumableArray(flatten(_n))); | ||
if (t.isSequenceExpression(n)) { | ||
ret.push(...flatten(n)); | ||
} else { | ||
ret.push(_n); | ||
ret.push(n); | ||
} | ||
@@ -726,3 +722,3 @@ } | ||
SwitchCase(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -740,3 +736,3 @@ if (!node.consequent.length) { | ||
function (path) { | ||
var node = path.node; // Need to be careful of side-effects. | ||
const node = path.node; // Need to be careful of side-effects. | ||
@@ -751,5 +747,5 @@ if (!t.isIdentifier(node.discriminant)) { | ||
var consTestPairs = []; | ||
var fallThru = []; | ||
var defaultRet; | ||
const consTestPairs = []; | ||
let fallThru = []; | ||
let defaultRet; | ||
var _iteratorNormalCompletion4 = true; | ||
@@ -761,11 +757,11 @@ var _didIteratorError4 = false; | ||
for (var _iterator4 = node.cases[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
var _switchCase = _step4.value; | ||
const switchCase = _step4.value; | ||
if (_switchCase.consequent.length > 1) { | ||
if (switchCase.consequent.length > 1) { | ||
return; | ||
} | ||
var cons = _switchCase.consequent[0]; // default case | ||
const cons = switchCase.consequent[0]; // default case | ||
if (!_switchCase.test) { | ||
if (!switchCase.test) { | ||
if (!t.isReturnStatement(cons)) { | ||
@@ -779,4 +775,4 @@ return; | ||
if (!_switchCase.consequent.length) { | ||
fallThru.push(_switchCase.test); | ||
if (!switchCase.consequent.length) { | ||
fallThru.push(switchCase.test); | ||
continue; | ||
@@ -790,8 +786,6 @@ } // TODO: can we void it? | ||
var test = t.binaryExpression("===", node.discriminant, _switchCase.test); | ||
let test = t.binaryExpression("===", node.discriminant, switchCase.test); | ||
if (fallThru.length && !defaultRet) { | ||
test = fallThru.reduceRight(function (right, test) { | ||
return t.logicalExpression("||", t.binaryExpression("===", node.discriminant, test), right); | ||
}, test); | ||
test = fallThru.reduceRight((right, test) => t.logicalExpression("||", t.binaryExpression("===", node.discriminant, test), right), test); | ||
} | ||
@@ -826,3 +820,3 @@ | ||
if (path.inList) { | ||
var nextPath = path.getSibling(path.key + 1); | ||
const nextPath = path.getSibling(path.key + 1); | ||
@@ -843,13 +837,7 @@ if (nextPath.isReturnStatement()) { | ||
var cond = consTestPairs.reduceRight(function (alt, _ref3) { | ||
var _ref4 = _slicedToArray(_ref3, 2), | ||
test = _ref4[0], | ||
cons = _ref4[1]; | ||
return t.conditionalExpression(test, cons, alt); | ||
}, defaultRet.argument || VOID_0); | ||
const cond = consTestPairs.reduceRight((alt, [test, cons]) => t.conditionalExpression(test, cons, alt), defaultRet.argument || VOID_0); | ||
path.replaceWith(t.returnStatement(cond)); // Maybe now we can merge with some previous switch statement. | ||
if (path.inList) { | ||
var prev = path.getSibling(path.key - 1); | ||
const prev = path.getSibling(path.key - 1); | ||
@@ -862,3 +850,3 @@ if (prev.isSwitchStatement()) { | ||
function (path) { | ||
var node = path.node; // Need to be careful of side-effects. | ||
const node = path.node; // Need to be careful of side-effects. | ||
@@ -873,5 +861,5 @@ if (!t.isIdentifier(node.discriminant)) { | ||
var exprTestPairs = []; | ||
var fallThru = []; | ||
var defaultExpr; | ||
const exprTestPairs = []; | ||
let fallThru = []; | ||
let defaultExpr; | ||
var _iteratorNormalCompletion5 = true; | ||
@@ -883,27 +871,27 @@ var _didIteratorError5 = false; | ||
for (var _iterator5 = node.cases[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) { | ||
var _switchCase2 = _step5.value; | ||
const switchCase = _step5.value; | ||
if (!_switchCase2.test) { | ||
if (_switchCase2.consequent.length !== 1) { | ||
if (!switchCase.test) { | ||
if (switchCase.consequent.length !== 1) { | ||
return; | ||
} | ||
if (!t.isExpressionStatement(_switchCase2.consequent[0])) { | ||
if (!t.isExpressionStatement(switchCase.consequent[0])) { | ||
return; | ||
} | ||
defaultExpr = _switchCase2.consequent[0].expression; | ||
defaultExpr = switchCase.consequent[0].expression; | ||
continue; | ||
} | ||
if (!_switchCase2.consequent.length) { | ||
fallThru.push(_switchCase2.test); | ||
if (!switchCase.consequent.length) { | ||
fallThru.push(switchCase.test); | ||
continue; | ||
} | ||
var _switchCase2$conseque = _slicedToArray(_switchCase2.consequent, 2), | ||
cons = _switchCase2$conseque[0], | ||
breakStatement = _switchCase2$conseque[1]; | ||
const _switchCase$consequen = _slicedToArray(switchCase.consequent, 2), | ||
cons = _switchCase$consequen[0], | ||
breakStatement = _switchCase$consequen[1]; | ||
if (_switchCase2 === node.cases[node.cases.length - 1]) { | ||
if (switchCase === node.cases[node.cases.length - 1]) { | ||
if (breakStatement && !t.isBreakStatement(breakStatement)) { | ||
@@ -916,12 +904,10 @@ return; | ||
if (!t.isExpressionStatement(cons) || _switchCase2.consequent.length > 2) { | ||
if (!t.isExpressionStatement(cons) || switchCase.consequent.length > 2) { | ||
return; | ||
} | ||
var test = t.binaryExpression("===", node.discriminant, _switchCase2.test); | ||
let test = t.binaryExpression("===", node.discriminant, switchCase.test); | ||
if (fallThru.length && !defaultExpr) { | ||
test = fallThru.reduceRight(function (right, test) { | ||
return t.logicalExpression("||", t.binaryExpression("===", node.discriminant, test), right); | ||
}, test); | ||
test = fallThru.reduceRight((right, test) => t.logicalExpression("||", t.binaryExpression("===", node.discriminant, test), right), test); | ||
} | ||
@@ -951,12 +937,6 @@ | ||
var cond = exprTestPairs.reduceRight(function (alt, _ref5) { | ||
var _ref6 = _slicedToArray(_ref5, 2), | ||
test = _ref6[0], | ||
cons = _ref6[1]; | ||
return t.conditionalExpression(test, cons, alt); | ||
}, defaultExpr || VOID_0); | ||
const cond = exprTestPairs.reduceRight((alt, [test, cons]) => t.conditionalExpression(test, cons, alt), defaultExpr || VOID_0); | ||
path.replaceWith(cond); | ||
}, function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -967,3 +947,3 @@ if (!node.cases.length) { | ||
var lastCase = path.get("cases")[node.cases.length - 1]; | ||
const lastCase = path.get("cases")[node.cases.length - 1]; | ||
@@ -974,3 +954,3 @@ if (!lastCase.node.consequent.length) { | ||
var potentialBreak = lastCase.get("consequent")[lastCase.node.consequent.length - 1]; | ||
const potentialBreak = lastCase.get("consequent")[lastCase.node.consequent.length - 1]; | ||
@@ -990,4 +970,4 @@ if (t.isBreakStatement(potentialBreak) && potentialBreak.node.label === null) { | ||
var test = node.test; | ||
var flip = false; | ||
const test = node.test; | ||
let flip = false; | ||
@@ -1014,3 +994,3 @@ if (t.isBinaryExpression(test)) { | ||
if (flip) { | ||
var consequent = node.consequent; | ||
const consequent = node.consequent; | ||
node.consequent = node.alternate; | ||
@@ -1042,3 +1022,3 @@ node.alternate = consequent; | ||
function earlyReturnTransform(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -1049,4 +1029,4 @@ if (!t.isBlockStatement(node.body)) { | ||
for (var i = node.body.body.length; i >= 0; i--) { | ||
var statement = node.body.body[i]; | ||
for (let i = node.body.body.length; i >= 0; i--) { | ||
const statement = node.body.body[i]; | ||
@@ -1060,3 +1040,3 @@ if (t.isIfStatement(statement) && !statement.alternate && t.isReturnStatement(statement.consequent) && !statement.consequent.argument) { | ||
function earlyContinueTransform(path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -1067,4 +1047,4 @@ if (!t.isBlockStatement(node.body)) { | ||
for (var i = node.body.body.length; i >= 0; i--) { | ||
var statement = node.body.body[i]; | ||
for (let i = node.body.body.length; i >= 0; i--) { | ||
const statement = node.body.body[i]; | ||
@@ -1083,9 +1063,7 @@ if (t.isIfStatement(statement) && !statement.alternate && t.isContinueStatement(statement.consequent) && !statement.consequent.label) { | ||
function genericEarlyExitTransform(path) { | ||
var node = path.node; | ||
var statements = path.parentPath.get(path.listKey).slice(path.key + 1).filter(function (stmt) { | ||
return !stmt.isFunctionDeclaration(); | ||
}); // deopt for any block scoped bindings | ||
const node = path.node; | ||
const statements = path.parentPath.get(path.listKey).slice(path.key + 1).filter(stmt => !stmt.isFunctionDeclaration()); // deopt for any block scoped bindings | ||
// issue#399 | ||
var deopt = !statements.every(function (stmt) { | ||
const deopt = !statements.every(stmt => { | ||
if (!(stmt.isVariableDeclaration({ | ||
@@ -1099,7 +1077,7 @@ kind: "let" | ||
var ids = Object.keys(stmt.getBindingIdentifiers()); | ||
const ids = Object.keys(stmt.getBindingIdentifiers()); | ||
for (var _i2 = 0; _i2 < ids.length; _i2++) { | ||
var id = ids[_i2]; | ||
var binding = path.scope.getBinding(id); // TODO | ||
const id = ids[_i2]; | ||
const binding = path.scope.getBinding(id); // TODO | ||
// Temporary Fix | ||
@@ -1113,39 +1091,18 @@ // if there is no binding, we assume it is referenced outside | ||
var refs = _toConsumableArray(binding.referencePaths).concat(_toConsumableArray(binding.constantViolations)); | ||
const refs = [...binding.referencePaths, ...binding.constantViolations]; | ||
var _iteratorNormalCompletion6 = true; | ||
var _didIteratorError6 = false; | ||
var _iteratorError6 = undefined; | ||
for (var _i3 = 0; _i3 < refs.length; _i3++) { | ||
const ref = refs[_i3]; | ||
if (!ref.isIdentifier()) return false; | ||
const fnParent = ref.getFunctionParent(); // TODO | ||
// Usage of scopes and bindings in simplify plugin results in | ||
// undefined bindings because scope changes are not updated in the | ||
// scope tree. So, we deopt whenever we encounter one such issue | ||
// and not perform the transformation | ||
try { | ||
for (var _iterator6 = refs[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) { | ||
var _ref7 = _step6.value; | ||
if (!_ref7.isIdentifier()) return false; | ||
if (!fnParent) { | ||
return false; | ||
} | ||
var fnParent = _ref7.getFunctionParent(); // TODO | ||
// Usage of scopes and bindings in simplify plugin results in | ||
// undefined bindings because scope changes are not updated in the | ||
// scope tree. So, we deopt whenever we encounter one such issue | ||
// and not perform the transformation | ||
if (!fnParent) { | ||
return false; | ||
} | ||
if (fnParent.scope !== path.scope) return false; | ||
} | ||
} catch (err) { | ||
_didIteratorError6 = true; | ||
_iteratorError6 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion6 && _iterator6.return != null) { | ||
_iterator6.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError6) { | ||
throw _iteratorError6; | ||
} | ||
} | ||
if (fnParent.scope !== path.scope) return false; | ||
} | ||
@@ -1167,3 +1124,3 @@ } | ||
var test = node.test; | ||
const test = node.test; | ||
@@ -1182,6 +1139,4 @@ if (t.isBinaryExpression(test) && test.operator === "!==") { | ||
path.get("consequent").replaceWith(t.blockStatement(statements.map(function (stmt) { | ||
return t.clone(stmt.node); | ||
}))); | ||
var l = statements.length; | ||
path.get("consequent").replaceWith(t.blockStatement(statements.map(stmt => t.clone(stmt.node)))); | ||
let l = statements.length; | ||
@@ -1199,3 +1154,3 @@ while (l-- > 0) { | ||
function createPrevExpressionEater(keyword) { | ||
var key; | ||
let key; | ||
@@ -1226,4 +1181,4 @@ switch (keyword) { | ||
var node = path.node; | ||
var prev = path.getSibling(path.key - 1); | ||
const node = path.node; | ||
const prev = path.getSibling(path.key - 1); | ||
@@ -1234,3 +1189,3 @@ if (!prev.isExpressionStatement()) { | ||
var seq = prev.node.expression; | ||
let seq = prev.node.expression; | ||
@@ -1245,3 +1200,3 @@ if (node[key]) { | ||
if (t.isSequenceExpression(seq)) { | ||
var lastExpr = seq.expressions[seq.expressions.length - 1]; | ||
const lastExpr = seq.expressions[seq.expressions.length - 1]; | ||
seq.expressions[seq.expressions.length - 1] = t.unaryExpression("void", lastExpr, true); | ||
@@ -1270,6 +1225,22 @@ } else { | ||
function isAncestor(path1, path2) { | ||
return !!path2.findParent(function (parent) { | ||
return parent === path1; | ||
}); | ||
return !!path2.findParent(parent => parent === path1); | ||
} | ||
function isPureVoid(path) { | ||
return path.isUnaryExpression({ | ||
operator: "void" | ||
}) && path.isPure(); | ||
} | ||
function isGlobalUndefined(path) { | ||
return path.isIdentifier({ | ||
name: "undefined" | ||
}) && !path.scope.getBinding("undefined"); | ||
} | ||
function undefinedToNull(path) { | ||
if (isGlobalUndefined(path) || isPureVoid(path)) { | ||
path.replaceWith(t.nullLiteral()); | ||
} | ||
} | ||
}; |
"use strict"; | ||
var h = require("./helpers"); | ||
const h = require("./helpers"); | ||
var PatternMatch = require("./pattern-match"); | ||
const PatternMatch = require("./pattern-match"); | ||
module.exports = function (t) { | ||
var OP_AND = function OP_AND(input) { | ||
return input === "&&"; | ||
}; | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
var OP_OR = function OP_OR(input) { | ||
return input === "||"; | ||
}; | ||
module.exports = t => { | ||
const OP_AND = input => input === "&&"; | ||
const OP_OR = input => input === "||"; | ||
function simplifyPatterns(path) { | ||
// cache of path.evaluate() | ||
var evaluateMemo = new Map(); | ||
// cache of evaluate(path) | ||
const evaluateMemo = new Map(); | ||
var TRUTHY = function TRUTHY(input) { | ||
const TRUTHY = input => { | ||
// !NaN and !undefined are truthy | ||
@@ -29,3 +27,3 @@ // separate check here as they are considered impure by babel | ||
var evalResult = input.evaluate(); | ||
const evalResult = evaluate(input); | ||
evaluateMemo.set(input, evalResult); | ||
@@ -35,3 +33,3 @@ return evalResult.confident && input.isPure() && evalResult.value; | ||
var FALSY = function FALSY(input) { | ||
const FALSY = input => { | ||
// NaN and undefined are falsy | ||
@@ -45,3 +43,3 @@ // separate check here as they are considered impure by babel | ||
var evalResult = input.evaluate(); | ||
const evalResult = evaluate(input); | ||
evaluateMemo.set(input, evalResult); | ||
@@ -51,20 +49,12 @@ return evalResult.confident && input.isPure() && !evalResult.value; | ||
var _h$typeSymbols = h.typeSymbols(t), | ||
EX = _h$typeSymbols.Expression; // Convention: | ||
const _h$typeSymbols = h.typeSymbols(t), | ||
EX = _h$typeSymbols.Expression; // Convention: | ||
// [left, operator, right, handler(leftNode, rightNode)] | ||
var matcher = new PatternMatch([[TRUTHY, OP_AND, EX, function (l, r) { | ||
return r; | ||
}], [FALSY, OP_AND, EX, function (l) { | ||
return l; | ||
}], [TRUTHY, OP_OR, EX, function (l) { | ||
return l; | ||
}], [FALSY, OP_OR, EX, function (l, r) { | ||
return r; | ||
}]]); | ||
var left = path.get("left"); | ||
var right = path.get("right"); | ||
var operator = path.node.operator; | ||
var result = matcher.match([left, operator, right], h.isPatternMatchesPath(t)); | ||
const matcher = new PatternMatch([[TRUTHY, OP_AND, EX, (l, r) => r], [FALSY, OP_AND, EX, l => l], [TRUTHY, OP_OR, EX, l => l], [FALSY, OP_OR, EX, (l, r) => r]]); | ||
const left = path.get("left"); | ||
const right = path.get("right"); | ||
const operator = path.node.operator; | ||
const result = matcher.match([left, operator, right], h.isPatternMatchesPath(t)); | ||
@@ -74,3 +64,3 @@ if (result.match) { | ||
// it satisfied one of TRUTHY/FALSY paths | ||
var value; | ||
let value; | ||
@@ -80,3 +70,3 @@ if (evaluateMemo.has(left)) { | ||
} else { | ||
value = left.evaluate().value; | ||
value = evaluate(left).value; | ||
} | ||
@@ -83,0 +73,0 @@ |
@@ -1,168 +0,156 @@ | ||
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr); } | ||
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); } | ||
var LEAF_NODE = Symbol("LEAF_NODE"); | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
module.exports = | ||
/*#__PURE__*/ | ||
function () { | ||
function PatternMatch(patterns) { | ||
_classCallCheck(this, PatternMatch); | ||
const LEAF_NODE = Symbol("LEAF_NODE"); | ||
module.exports = class PatternMatch { | ||
constructor(patterns) { | ||
this.decisionTree = this.makeDecisionTree(patterns); | ||
} | ||
_createClass(PatternMatch, [{ | ||
key: "handle", | ||
value: function handle(input, isMatch) { | ||
var result = this.match(input, isMatch); | ||
handle(input, isMatch) { | ||
const result = this.match(input, isMatch); | ||
if (!result.match) { | ||
throw new Error("No Match Found for " + input.toString()); | ||
} | ||
if (!result.match) { | ||
throw new Error("No Match Found for " + input.toString()); | ||
} | ||
if (typeof result.value !== "function") { | ||
throw new Error("Expecting a function. Instead got - " + result.value.toString()); | ||
} | ||
result.value.call(null, input, result.keys); | ||
if (typeof result.value !== "function") { | ||
throw new Error("Expecting a function. Instead got - " + result.value.toString()); | ||
} | ||
}, { | ||
key: "match", | ||
value: function match(input) { | ||
var isMatch = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function (a, b) { | ||
return a === b; | ||
}; | ||
var current = this.decisionTree; | ||
var result = { | ||
match: false, | ||
value: void 0, | ||
keys: [] | ||
}; // to handle falsy keys | ||
var NO_MATCH = Symbol("NO_MATCH"); | ||
result.value.call(null, input, result.keys); | ||
} | ||
for (var i = 0; i < input.length; i++) { | ||
var matchedKey = NO_MATCH; // because map doesn't support custom key equal function | ||
match(input, isMatch = (a, b) => a === b) { | ||
let current = this.decisionTree; | ||
const result = { | ||
match: false, | ||
value: void 0, | ||
keys: [] | ||
}; // to handle falsy keys | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const NO_MATCH = Symbol("NO_MATCH"); | ||
try { | ||
for (var _iterator = current.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _key = _step.value; | ||
for (let i = 0; i < input.length; i++) { | ||
let matchedKey = NO_MATCH; // because map doesn't support custom key equal function | ||
if (isMatch(_key, input[i])) { | ||
matchedKey = _key; | ||
result.keys.push(matchedKey); | ||
break; | ||
} | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
try { | ||
for (var _iterator = current.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
const key = _step.value; | ||
if (isMatch(key, input[i])) { | ||
matchedKey = key; | ||
result.keys.push(matchedKey); | ||
break; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return != null) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
if (matchedKey !== NO_MATCH) { | ||
current = current.get(matchedKey); | ||
if (matchedKey !== NO_MATCH) { | ||
current = current.get(matchedKey); | ||
if (i === input.length - 1) { | ||
if (current.has(LEAF_NODE)) { | ||
result.match = true; | ||
result.value = current.get(LEAF_NODE); | ||
} | ||
if (i === input.length - 1) { | ||
if (current.has(LEAF_NODE)) { | ||
result.match = true; | ||
result.value = current.get(LEAF_NODE); | ||
} | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
} else { | ||
break; | ||
} | ||
return result; | ||
} | ||
}, { | ||
key: "makeDecisionTree", | ||
value: function makeDecisionTree(patterns) { | ||
// order of keys in a Map is the order of insertion | ||
var root = new Map(); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
return result; | ||
} | ||
makeDecisionTree(patterns) { | ||
// order of keys in a Map is the order of insertion | ||
const root = new Map(); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = patterns[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
const pattern = _step2.value; | ||
make(root, pattern); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
for (var _iterator2 = patterns[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var _pattern2 = _step2.value; | ||
make(root, _pattern2); | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return != null) { | ||
_iterator2.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
return root; | ||
return root; | ||
function make(parent, pattern) { | ||
if (pattern.length < 2) { | ||
throw new Error("at least 2 elements required in a pattern"); | ||
} | ||
function make(parent, pattern) { | ||
if (pattern.length < 2) { | ||
throw new Error("at least 2 elements required in a pattern"); | ||
} | ||
if (pattern.length === 2) { | ||
if (parent.has(pattern[0])) { | ||
var pattern0 = parent.get(pattern[0]); | ||
if (pattern.length === 2) { | ||
if (parent.has(pattern[0])) { | ||
const pattern0 = parent.get(pattern[0]); | ||
if (!pattern0.has(LEAF_NODE)) { | ||
pattern0.set(LEAF_NODE, pattern[1]); | ||
} // here we don't handle duplicates | ||
// this pattern would have already been matched | ||
if (!pattern0.has(LEAF_NODE)) { | ||
pattern0.set(LEAF_NODE, pattern[1]); | ||
} // here we don't handle duplicates | ||
// this pattern would have already been matched | ||
} else { | ||
parent.set(pattern[0], new Map([[LEAF_NODE, pattern[1]]])); | ||
} | ||
return parent; | ||
} else { | ||
parent.set(pattern[0], new Map([[LEAF_NODE, pattern[1]]])); | ||
} | ||
var _pattern = _toArray(pattern), | ||
return parent; | ||
} | ||
const _pattern = _toArray(pattern), | ||
current = _pattern[0], | ||
rest = _pattern.slice(1); | ||
if (parent.has(current)) { | ||
make(parent.get(current), rest); | ||
} else { | ||
parent.set(current, make(new Map(), rest)); | ||
} | ||
if (parent.has(current)) { | ||
make(parent.get(current), rest); | ||
} else { | ||
parent.set(current, make(new Map(), rest)); | ||
} | ||
return parent; | ||
} | ||
return parent; | ||
} | ||
}]); | ||
} | ||
return PatternMatch; | ||
}(); | ||
}; |
{ | ||
"name": "babel-plugin-minify-simplify", | ||
"version": "0.5.0-alpha.7b176463", | ||
"version": "0.5.0-alpha.85c9b6d8", | ||
"description": "", | ||
@@ -15,6 +15,6 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.7b176463", | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.85c9b6d8", | ||
"babel-helper-is-nodes-equiv": "^0.0.1", | ||
"babel-helper-to-multiple-sequence-expressions": "^0.5.0-alpha.7b176463" | ||
"babel-helper-to-multiple-sequence-expressions": "^0.5.0-alpha.85c9b6d8" | ||
} | ||
} |
@@ -54,3 +54,3 @@ # babel-plugin-minify-simplify | ||
```sh | ||
npm install babel-plugin-minify-simplify | ||
npm install babel-plugin-minify-simplify --save-dev | ||
``` | ||
@@ -57,0 +57,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
58566
1480