babel-plugin-minify-simplify
Advanced tools
Comparing version 0.5.0-alpha.9e986477 to 0.5.0-alpha.a24dd066
"use strict"; | ||
const VOID_0 = t => 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 | ||
const types = {}; // This is a test key which is used to avoid Object.keys check | ||
@@ -42,3 +44,3 @@ // Object.keys() check is really expensive | ||
if (isNodeOfType(t, inputPath.node, patternValue)) return true; | ||
const evalResult = inputPath.evaluate(); | ||
const evalResult = evaluate(inputPath); | ||
if (!evalResult.confident || !inputPath.isPure()) return false; | ||
@@ -45,0 +47,0 @@ return evalResult.value === patternValue; |
@@ -117,2 +117,10 @@ "use strict"; | ||
}, | ||
BinaryExpression(path) { | ||
if (["!=", "=="].indexOf(path.node.operator) !== -1) { | ||
undefinedToNull(path.get("left")); | ||
undefinedToNull(path.get("right")); | ||
} | ||
}, | ||
LogicalExpression: { | ||
@@ -956,13 +964,15 @@ exit: logicalExpression.simplifyPatterns | ||
function earlyReturnTransform(path) { | ||
const node = path.node; | ||
const block = path.get("body"); | ||
if (!t.isBlockStatement(node.body)) { | ||
if (!block.isBlockStatement()) { | ||
return; | ||
} | ||
for (let i = node.body.body.length; i >= 0; i--) { | ||
const statement = node.body.body[i]; | ||
const body = block.get("body"); | ||
if (t.isIfStatement(statement) && !statement.alternate && t.isReturnStatement(statement.consequent) && !statement.consequent.argument) { | ||
genericEarlyExitTransform(path.get("body").get("body")[i]); | ||
for (let i = body.length - 1; i >= 0; i--) { | ||
const statement = body[i]; | ||
if (t.isIfStatement(statement.node) && !statement.node.alternate && t.isReturnStatement(statement.node.consequent) && !statement.node.consequent.argument) { | ||
genericEarlyExitTransform(statement); | ||
} | ||
@@ -973,19 +983,23 @@ } | ||
function earlyContinueTransform(path) { | ||
const node = path.node; | ||
const block = path.get("body"); | ||
if (!t.isBlockStatement(node.body)) { | ||
if (!block.isBlockStatement()) { | ||
return; | ||
} | ||
for (let i = node.body.body.length; i >= 0; i--) { | ||
const statement = node.body.body[i]; | ||
let body = block.get("body"); | ||
if (t.isIfStatement(statement) && !statement.alternate && t.isContinueStatement(statement.consequent) && !statement.consequent.label) { | ||
genericEarlyExitTransform(path.get("body").get("body")[i]); | ||
for (let i = body.length - 1; i >= 0; i--) { | ||
const statement = body[i]; | ||
if (t.isIfStatement(statement.node) && !statement.node.alternate && t.isContinueStatement(statement.node.consequent) && !statement.node.consequent.label) { | ||
genericEarlyExitTransform(statement); | ||
} | ||
} // We may have reduced the body to a single statement. | ||
} // because we might have folded or removed statements | ||
if (node.body.body.length === 1 && !needsBlock(node.body, node)) { | ||
path.get("body").replaceWith(node.body.body[0]); | ||
body = block.get("body"); // We may have reduced the body to a single statement. | ||
if (body.length === 1 && !needsBlock(block.node, path.node)) { | ||
block.replaceWith(body[0].node); | ||
} | ||
@@ -1150,2 +1164,20 @@ } | ||
} | ||
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()); | ||
} | ||
} | ||
}; |
@@ -7,2 +7,4 @@ "use strict"; | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
module.exports = t => { | ||
@@ -14,3 +16,3 @@ const OP_AND = input => input === "&&"; | ||
function simplifyPatterns(path) { | ||
// cache of path.evaluate() | ||
// cache of evaluate(path) | ||
const evaluateMemo = new Map(); | ||
@@ -27,3 +29,3 @@ | ||
const evalResult = input.evaluate(); | ||
const evalResult = evaluate(input); | ||
evaluateMemo.set(input, evalResult); | ||
@@ -42,3 +44,3 @@ return evalResult.confident && input.isPure() && evalResult.value; | ||
const evalResult = input.evaluate(); | ||
const evalResult = evaluate(input); | ||
evaluateMemo.set(input, evalResult); | ||
@@ -67,3 +69,3 @@ return evalResult.confident && input.isPure() && !evalResult.value; | ||
} else { | ||
value = left.evaluate().value; | ||
value = evaluate(left).value; | ||
} | ||
@@ -70,0 +72,0 @@ |
{ | ||
"name": "babel-plugin-minify-simplify", | ||
"version": "0.5.0-alpha.9e986477", | ||
"version": "0.5.0-alpha.a24dd066", | ||
"description": "", | ||
@@ -15,6 +15,6 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.9e986477", | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.a24dd066", | ||
"babel-helper-is-nodes-equiv": "^0.0.1", | ||
"babel-helper-to-multiple-sequence-expressions": "^0.5.0-alpha.9e986477" | ||
"babel-helper-to-multiple-sequence-expressions": "^0.5.0-alpha.a24dd066" | ||
} | ||
} |
@@ -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
58673
1483