From efbc2ffe6a1ee5ec38a4396305561d6515d0943e Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Fri, 30 Aug 2024 17:21:18 +0800 Subject: node fixes, updates --- .../lib/conditional-expression.js | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js (limited to 'node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js') diff --git a/node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js b/node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js new file mode 100644 index 00000000..9640e8c8 --- /dev/null +++ b/node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js @@ -0,0 +1,39 @@ +"use strict"; + +const h = require("./helpers"); + +const PatternMatch = require("./pattern-match"); + +module.exports = t => { + // small abstractions + const not = node => t.unaryExpression("!", node); + + const notnot = node => not(not(node)); + + const or = (a, b) => t.logicalExpression("||", a, b); + + const and = (a, b) => t.logicalExpression("&&", a, b); + + function simplifyPatterns(path) { + const test = path.get("test"); + const consequent = path.get("consequent"); + const alternate = path.get("alternate"); + + const _h$typeSymbols = h.typeSymbols(t), + EX = _h$typeSymbols.Expression; // Convention: + // =============== + // for each pattern [test, consequent, alternate, handler(expr, cons, alt)] + + + 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)); + + if (result.match) { + path.replaceWith(result.value(test.node, consequent.node, alternate.node)); + } + } + + return { + simplifyPatterns + }; +}; \ No newline at end of file -- cgit v1.2.3