babel-plugin-minify-guarded-expressions
Advanced tools
Comparing version 0.5.0-alpha.7b176463 to 0.5.0-alpha.7ba5821f
"use strict"; | ||
module.exports = function (_ref) { | ||
var t = _ref.types; | ||
const evaluate = require("babel-helper-evaluate-path"); | ||
var flipExpressions = require("babel-helper-flip-expressions")(t); | ||
function evaluateTruthy(path) { | ||
const res = evaluate(path); | ||
if (res.confident) return !!res.value; | ||
} | ||
module.exports = function ({ | ||
types: t | ||
}) { | ||
const flipExpressions = require("babel-helper-flip-expressions")(t); | ||
return { | ||
@@ -17,5 +24,5 @@ name: "minify-guarded-expressions", | ||
enter: [function (path) { | ||
var node = path.node; | ||
var left = path.get("left"); | ||
var right = path.get("right"); // issues - 171, 174, 176 | ||
const node = path.node; | ||
const left = path.get("left"); | ||
const right = path.get("right"); // issues - 171, 174, 176 | ||
// we assume that it is returned/assigned/part of a bigger expression | ||
@@ -28,6 +35,6 @@ // or utilized somehow | ||
var shouldBail = !path.parentPath.isExpressionStatement(); | ||
const shouldBail = !path.parentPath.isExpressionStatement(); | ||
if (node.operator === "&&") { | ||
var leftTruthy = left.evaluateTruthy(); | ||
const leftTruthy = evaluateTruthy(left); | ||
@@ -39,14 +46,14 @@ if (leftTruthy === false) { | ||
path.replaceWith(node.right); | ||
} else if (right.evaluateTruthy() === false && right.isPure() && !shouldBail) { | ||
} else if (evaluateTruthy(right) === false && right.isPure() && !shouldBail) { | ||
path.replaceWith(node.left); | ||
} | ||
} else if (node.operator === "||") { | ||
var _leftTruthy = left.evaluateTruthy(); | ||
const leftTruthy = evaluateTruthy(left); | ||
if (_leftTruthy === false && left.isPure()) { | ||
if (leftTruthy === false && left.isPure()) { | ||
path.replaceWith(node.right); | ||
} else if (_leftTruthy === true) { | ||
} else if (leftTruthy === true) { | ||
// Short-circuit | ||
path.replaceWith(node.left); | ||
} else if (right.evaluateTruthy() === false && right.isPure() && !shouldBail) { | ||
} else if (evaluateTruthy(right) === false && right.isPure() && !shouldBail) { | ||
path.replaceWith(node.left); | ||
@@ -56,3 +63,3 @@ } | ||
}, function (path) { | ||
var node = path.node; | ||
const node = path.node; | ||
@@ -70,3 +77,3 @@ if (flipExpressions.hasSeen(node)) { | ||
if (flipExpressions.shouldFlip(node, 1)) { | ||
var newNode = flipExpressions.flip(node, true); | ||
const newNode = flipExpressions.flip(node, true); | ||
path.replaceWith(newNode); | ||
@@ -73,0 +80,0 @@ } |
{ | ||
"name": "babel-plugin-minify-guarded-expressions", | ||
"version": "0.5.0-alpha.7b176463", | ||
"version": "0.5.0-alpha.7ba5821f", | ||
"description": "", | ||
@@ -15,4 +15,4 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.7b176463" | ||
"babel-helper-flip-expressions": "^0.5.0-alpha.7ba5821f" | ||
} | ||
} |
@@ -22,3 +22,3 @@ # babel-plugin-minify-guarded-expressions | ||
```sh | ||
npm install babel-plugin-minify-guarded-expressions | ||
npm install babel-plugin-minify-guarded-expressions --save-dev | ||
``` | ||
@@ -25,0 +25,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
3852
67