> For the complete documentation index, see [llms.txt](https://gofast.disasm.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gofast.disasm.dev/built-in-visitors/simplifier.md).

# 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

```go
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](https://gofast.disasm.dev/built-in-visitors/resolver.md)

### 🧪 Examples

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

#### Simple Expressions

```javascript
!![]
// => true

1 + 2
// => 3

"a" + 1
// => "a1"
```

#### Array/Object Access

```javascript
[1, 2, 3][0]
// => 1

"foo"[0]
// => "f"

({a: 1})['a']
// => 1
```

#### Logical and Conditional Folding

```javascript
true && x
// => x

false || x
// => x

true ? 1 : 2
// => 1

x ? true : false
// => !!x
```

#### Type Evaluation

```javascript
typeof 42
// => "number"

typeof undefined
// => "undefined"
```

#### Instance Checks

```javascript
42 instanceof Object
// => false

[] instanceof Object
// => true
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gofast.disasm.dev/built-in-visitors/simplifier.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
