Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

🧠Simplifier

The simplifier module in go-fAST provides aggressive, safe folding and reduction of JavaScript AST nodes. It performs constant folding, dead code elimination, expression rewriting, and general simplifications without altering program semantics.

🔍 What It Does

The simplifier traverses a JavaScript AST and rewrites expressions where possible, folding constants, simplifying comparisons, and restructuring expressions to reduce size and improve readability.

Key capabilities include:

  • Folding binary and unary operations (e.g. 1 + 2 -> 3, !![] -> true)

  • Inlining known values (e.g. 'abc'.length -> 3)

  • Eliminating unreachable paths in conditional expressions

  • Simplifying array/object member expressions (e.g. [1, 2, 3][0] -> 1)

  • Resolving typeof, instanceof, and comparison operations

  • Rewriting SequenceExpressions and SpreadElements to preserve side-effects but remove nois

🧰 API Usage

import "github.com/t14raptor/go-fast/transform/simplifier"

simplifier.Simplify(program, resolve bool)
  • program: An *ast.Program from the parser

  • resolve: Boolean flag to indicate whether variable scope resolution should be performed prior to simplification. See resolver

🧪 Examples

Below are just some of the examples that the simplifer module can do.

Simple Expressions

Array/Object Access

Logical and Conditional Folding

Type Evaluation

Instance Checks

Last updated