diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2024-08-30 17:21:18 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2024-08-30 17:21:18 +0800 |
| commit | efbc2ffe6a1ee5ec38a4396305561d6515d0943e (patch) | |
| tree | 2b4c94f18f2c792dfda813050755d5c0273fae22 /node_modules/babel-plugin-minify-simplify/lib/logical-expression.js | |
| parent | 43acceb75957c880a5ba5a31aae221394a36d5f6 (diff) | |
node fixes, updates
Diffstat (limited to 'node_modules/babel-plugin-minify-simplify/lib/logical-expression.js')
| -rw-r--r-- | node_modules/babel-plugin-minify-simplify/lib/logical-expression.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/node_modules/babel-plugin-minify-simplify/lib/logical-expression.js b/node_modules/babel-plugin-minify-simplify/lib/logical-expression.js new file mode 100644 index 00000000..a91af230 --- /dev/null +++ b/node_modules/babel-plugin-minify-simplify/lib/logical-expression.js @@ -0,0 +1,75 @@ +"use strict"; + +const h = require("./helpers"); + +const PatternMatch = require("./pattern-match"); + +const evaluate = require("babel-helper-evaluate-path"); + +module.exports = t => { + const OP_AND = input => input === "&&"; + + const OP_OR = input => input === "||"; + + function simplifyPatterns(path) { + // cache of evaluate(path) + const evaluateMemo = new Map(); + + const TRUTHY = input => { + // !NaN and !undefined are truthy + // separate check here as they are considered impure by babel + if (input.isUnaryExpression() && input.get("argument").isIdentifier()) { + if (input.node.argument.name === "NaN" || input.node.argument.name === "undefined") { + return true; + } + } + + const evalResult = evaluate(input); + evaluateMemo.set(input, evalResult); + return evalResult.confident && input.isPure() && evalResult.value; + }; + + const FALSY = input => { + // NaN and undefined are falsy + // separate check here as they are considered impure by babel + if (input.isIdentifier()) { + if (input.node.name === "NaN" || input.node.name === "undefined") { + return true; + } + } + + const evalResult = evaluate(input); + evaluateMemo.set(input, evalResult); + return evalResult.confident && input.isPure() && !evalResult.value; + }; + + const _h$typeSymbols = h.typeSymbols(t), + EX = _h$typeSymbols.Expression; // Convention: + // [left, operator, right, handler(leftNode, rightNode)] + + + 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)); + + if (result.match) { + // here we are sure that left.evaluate is always confident becuase + // it satisfied one of TRUTHY/FALSY paths + let value; + + if (evaluateMemo.has(left)) { + value = evaluateMemo.get(left).value; + } else { + value = evaluate(left).value; + } + + path.replaceWith(result.value(t.valueToNode(value), right.node)); + } + } + + return { + simplifyPatterns + }; +};
\ No newline at end of file |
