summaryrefslogtreecommitdiff
path: root/node_modules/babel-plugin-minify-simplify/lib/conditional-expression.js
blob: 9640e8c86f491af08246d393b51d9de2e728f02b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
  };
};