All files constant-folding.js

95.23% Statements 80/84
87.8% Branches 36/41
100% Functions 8/8
94.87% Lines 74/78

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 178 179 180 181 182 183 184  1x 1x 1x 1x 1x 1x   1x   1x                   7x 7x   69x     6x 1x 5x 1x     69x       4x 1x 3x 2x 1x   1x   1x 1x     69x     2x     7x 7x 7x                     2x   2x 2x   2x 2x                     1x 1x 1x   1x 1x                     2x 2x 2x 6x 6x 4x   2x 2x                     1x 1x 1x     1x   1x 3x 1x     1x   1x 1x                     1x 1x 1x         1x 1x                     1x 1x 1x   3x   1x 4x 2x 3x     2x     1x   1x 1x  
// 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 { Console } = require("console");
 
"use strict";
 
module.exports = constantFolding;
 
/**
 * A function that takes js code in a string
 * and applies constant folding to transform it
 * @param {string} code A string containing the js code
 * 
 * @returns {string} Returns the transformed code as a string
 */
function constantFolding(code) {
  const t = espree.parse(code, { ecmaVersion: 6, loc: false });
  estraverse.traverse(t, {
    leave: function (n) {
      if (
        n.type == "MemberExpression" && 
          n.object.type == "ArrayExpression") { 
            if (n.property.name == "length" ) {
              replaceByArrayLength(n);
            } else if (n.property.type == "Literal" || n.property.type == "BinaryExpression") {
              replaceByArraySquare(n);
            }
          }
      if (
          n.type == "CallExpression" && 
          n.callee.type  == "MemberExpression" && n.callee.object.type == "ArrayExpression"
      ) {
        if (n.callee.property.name == "concat" ) {
          replaceByArrayConcat(n);
        } else if (n.callee.property.name == "join") {
          if (n.arguments[0] != null) {
            replaceByArrayJoin(n, n.arguments[0].value);
          } else {
            replaceByArrayJoin(n);
          }
        } else Eif (n.callee.property.name == "pop") {
          replaceByArrayPop(n);
        }
      }
      if (
        n.type == "BinaryExpression" && 
          n.left.type == "Literal" && n.right.type == "Literal"
      ) { replaceByLiteral(n); }
    },
  });
  deb(t);
  let c = escodegen.generate(t);
  return c;
}
 
/**
 * A function that takes an ast node and evaluates
 * it's binary expresion in order to replace it with
 * it's value
 * 
 * @param code An ast node
 */
function replaceByLiteral(n) {
  n.type = "Literal";
 
  n.value = eval(`${n.left.raw} ${n.operator} ${n.right.raw}`);
  n.raw = String(n.value);
 
  delete n.left;
  delete n.right;
}
 
/**
 * A function that takes an ast node and evaluates
 * it's member expresion in order to replace it with
 * it's value
 * In this case the member expression is array.length
 * @param code An ast node
 */
function replaceByArrayLength(n) {
  n.type = "Literal";
  n.value = n.object.elements.length;
  n.raw = String(n.value);
 
  delete n.object;
  delete n.property;
}
 
/**
 * A function that takes an ast node and evaluates
 * it's member expresion in order to replace it with
 * it's value
 * In this case the member expression is array.join().
 * @param code An ast node
 */
function replaceByArrayJoin(n, separator = ',') {
  n.type = "Literal"
  let resultstring = "";
  for (let i = 0; i < n.callee.object.elements.length; i++) {
    resultstring += n.callee.object.elements[i].value;
    if (i >= n.callee.object.elements.length - 1) break;
    resultstring += separator;
  }
  n.value = resultstring;
  n.raw = String(resultstring);
}
 
/**
 * A function that takes an ast node and evaluates
 * it's member expresion in order to replace it with
 * it's value
 * In this case the member expression is array[].
 * @param code An ast node
 */
function replaceByArraySquare(n){
  n.type = "Literal";
  let position = 0;
  Iif (n.property.type == "BinaryExpression") {
    position = eval(`${n.property.left.raw} ${n.property.operator} ${n.property.right.raw}`);
  } else {
    position = n.property.value;
  }
  for (let i = 0; i < n.object.elements.length; i++){
    if(i == position) {
      n.value = n.object.elements[i].value;
    }
  }
  n.raw = String(n.value);
 
  delete n.object
  delete n.property;
}
 
/**
 * A function that takes an ast node and evaluates
 * it's member expresion in order to replace it with
 * it's value
 * In this case the member expression is array.pop().
 * @param code An ast node
 */
function replaceByArrayPop(n) {
  n.type = n.callee.object.elements[n.callee.object.elements.length -1].type;
  Eif(n.type == "Identifier") {
    n.name = n.callee.object.elements[n.callee.object.elements.length -1].name;
  } else if (n.type == "Literal") {
    n.value = n.callee.object.elements[n.callee.object.elements.length -1].value;
    n.raw = String(n.value);
  }
  delete n.callee;
  delete n.arguments;
}
 
/**
 * A function that takes an ast node and evaluates
 * it's member expresion in order to replace it with
 * it's value
 * In this case the member expression is array.concat().
 * @param code An ast node
 */
function replaceByArrayConcat(n) {
  n.type = "ArrayExpression";
  let elements = [];
  for (let i = 0; i < n.callee.object.elements.length; i++) {
    //console.log(n.callee.object.elements[i].raw)
    elements.push(n.callee.object.elements[i]);
  }
  for (let i = 0; i < n.arguments.length; i++) {
    if(n.arguments[i].type == "ArrayExpression") {
      for (let j = 0; j < n.arguments[i].elements.length; j++) {
        elements.push(n.arguments[i].elements[j]);
      }
    } else {
      elements.push(n.arguments[i]);
    }
  }
  n.elements = elements;
 
  delete n.callee;
  delete n.arguments;
}