All files constant-folding.js

100% Statements 176/176
100% Branches 29/29
100% Functions 6/6
100% Lines 176/176

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 1781x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 46x 46x 46x 46x 127x 127x 127x 10x 10x 10x 10x 10x 127x 117x 117x 127x 46x 46x 46x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 17x 17x 20x 20x 20x 17x 17x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 11x 11x 2x 2x 2x 2x 11x 2x 2x 2x 2x 1x 1x 2x 6x 6x 4x 4x 6x 2x 2x 2x 2x 2x 9x 1x 1x 1x 1x 1x 1x 7x 3x 3x 3x 1x 3x 2x 2x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 2x 2x 6x 6x 2x 2x 2x 2x 2x 2x 11x 11x 17x 17x 17x 17x 17x 1x 1x 1x 1x 1x 1x 17x 2x 2x 2x 2x 2x 2x 2x 17x 17x 17x 17x    
// See https://github.com/babel/minify/tree/master/packages/babel-plugin-minify-constant-folding
const fs = require("fs");
const deb = require('../src/deb.js');
const escodegen = require("escodegen");
const espree = require("espree");
const estraverse = require("estraverse");
const b = require("ast-types").builders;
const visit = require("ast-types").visit;
const recast = require("recast");
 
 
"use strict";
 
module.exports = {constantFolding, squashArray};
 
 
/**
 * Function that takes an array which may have more arrays within and sets them all in the same
 * cardinality. This is done recursively.
 * 
 * For example, [3, [4, 5], [6], [[7], 8]] is transformed into [3, 4, 5, 6, 7, 8]
 * @param {Node[]} array array of node which has all the arrays to be squashed within.
 * @return {Node[]} array of Literal nodes.
 */
function squashArray(array, result = []) {
  // console.log("DING DONG NEW CALL");
  // console.log(array);
  for (let element of array) {
    // console.log(element);
    // console.log("\n");
    if (element.type == "ArrayExpression") {
      // console.log("Result state rn: \n");
      // for (let result_element of result) {
      //   console.log(result_element);
      // }
      squashArray(element.elements, result);
    } else {
      result.push(element);
    }
  }
  // console.log("===========================");
  return result;
}
 
/**
 * A function that does the usual constant folding.
 * However, it also does constant folding for methods and arguments,
 * including lists as well.
 * @param {string} code A string containing the code on which to fold
 * constants.
 * @returns {string} Returns the code, reparsed, with the constants and such
 * folded.
 */
function constantFolding(code, pattern) {
  let ast = espree.parse(code, { ecmaVersion: 7, loc: false });
  visit(ast, {
    visitBinaryExpression(path) {
      const node = path.node;
      this.traverse(path);
      if (node.left.type == "Literal" && node.right.type == "Literal") {
        let result = eval(node.left.value + node.operator + node.right.value);
        node.type = "Literal";
        delete node.operator;
        delete node.left;
        delete node.right;
        node.value = result;
        node.raw = "" + result;
      }
    },
    visitArrayExpression(path) {
      const node = path.node;
      path.node.elements = squashArray(node.elements);
      return false;
    },
    visitCallExpression(path) {
      const node = path.node;
      this.traverse(path);
      if (node.callee.type == "MemberExpression") {
        const callee = node.callee;
        if (callee.object.type == "ArrayExpression") {
          let squashedObject = squashArray(callee.object.elements);
          if (callee.property.name == "concat") {
            let squashedArguments = squashArray(node.arguments);
            node.type = "ArrayExpression";
            for (let element of squashedArguments) {
              squashedObject.push(element);
            }
            node.elements = squashedObject;
            path.node = node;
            return false;
 
          } else if (callee.property.name == "join") {
            node.type = "Literal";
            let value = "";
            let separator = ",";
            if (node.arguments.length !== 0) {
              separator = node.arguments[0].value;
            }
            for (let i = 0; i < squashedObject.length; i += 1) {
              value += squashedObject[i].value;
              if (i != squashedObject.length-1) {
                value += separator;
              }
            }
            node.value = value;
            node.raw = "\\"+node.value+"\\";
            path.node = node;
            return false;
 
          } else if (callee.property.name == "shift") {
            node.type = "Literal";
            node.value = squashedObject.shift().value;
            node.raw = "\\"+node.value+"\\";
            path.node = node;
            return false;
 
          } else if (callee.property.name == "slice") {
            this.traverse(path);
            let slicedElements;
            if (node.arguments.length == 1) {
              slicedElements = callee.object.elements.slice(0, node.arguments[0].value);
            } else {
              slicedElements = callee.object.elements.slice(node.arguments[0].value, node.arguments[1].value);
            }
            node.type = "ArrayExpression";
            node.elements = slicedElements;
            path.node = node;
            delete node.callee;
            delete node.arguments;
 
          } else if (callee.property.name == "pop") {
            // console.log(squashedObject);
            let poppedValue = squashedObject[squashedObject.length-1];
            node.type = poppedValue.type;
            // node = poppedValue;
            // console.log(poppedValue);
            path.node = node;
            // console.log(path.node);
            return false;
 
          } else if (callee.property.name == "reverse") {
            let reversedArray = [];
            for (let i = squashedObject.length - 1; i >= 0; i -= 1) {
              reversedArray.push(squashedObject[i]);
            }
            path.node.type = "ArrayExpression";
            path.node.elements = reversedArray;
            delete path.node.callee;
            delete path.node.arguments;
            return false;
          }
        }
      }
    },
    visitMemberExpression(path) {
      const node = path.node;
      this.traverse(path);
      if (node.object.type == "ArrayExpression" && node.property.name == "length") {
        node.object = squashArray(node.object.elements);
        node.type = "Literal";
        node.value = node.object.length;
        node.raw = `"${node.value}"`;
        delete node.object;
        delete node.property;
      } else if (node.object.type == "ArrayExpression" && node.property.type == "Literal") {
        node.object = squashArray(node.object.elements);
        node.type = "Literal";
        node.value = node.object[node.property.value].value;
        node.raw = `"${node.value}"`;
        delete node.object;
        delete node.property;
      }
    },
  });
  return recast.print(ast).code;
}