# Constant Folding

The following code uses estraverse to implement a simplified version of the AST tree transformation known as Constant Folding (opens new window). Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime.

// 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 input = `
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
`;

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;
}

const t = espree.parse(input, { ecmaVersion: 6, loc: false });
estraverse.traverse(t, {
  leave: function (n, p) {
    if (
      n.type == "BinaryExpression" && 
        n.left.type == "Literal" && n.right.type == "Literal"
    ) { replaceByLiteral(n); }
  },
});
deb(t);
let c = escodegen.generate(t);
console.error(c);
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

Execution:

➜  espree-logging-casiano-rodriguez-leon-alumno5 git:(train) ✗ node src/cf.js >salida.json                              
var f = 3;
var e = 7;
var d = '3c';
var b = 10;
var a = 17 + b;
1
2
3
4
5
6
Last Updated: 10 months ago