JavaScript AST transformation library for Go, built for go-fast.
go get github.com/nukilabs/transformDead code elimination for JavaScript. Removes unused functions, classes, and variable declarations through iterative scope-aware analysis. Correctly handles circular dependencies and preserves expressions with side effects.
import (
"github.com/nukilabs/transform/deadcode"
"github.com/t14raptor/go-fast/parser"
"github.com/t14raptor/go-fast/generator"
)
p, _ := parser.ParseFile(`
function a() { return "a"; }
function b() { return "b"; }
b();
`)
deadcode.Eliminate(p, true)
fmt.Println(generator.Generate(p))
// function b() { return "b"; } b();Constant folding and expression simplification for JavaScript. Evaluates constant expressions, folds comparisons, simplifies logical operators, optimizes member access on literals, and handles JavaScript type coercion semantics.
import (
"github.com/nukilabs/transform/simplifier"
"github.com/t14raptor/go-fast/parser"
"github.com/t14raptor/go-fast/generator"
)
p, _ := parser.ParseFile(`!+[]+!+[]`)
simplifier.Simplify(p, true)
fmt.Println(generator.Generate(p))
// 2Selected simplifications:
| Input | Output |
|---|---|
![] |
false |
+!+[] |
1 |
"hello".length |
5 |
[1, 2, 3][0] |
1 |
true ? a : b |
a |
true && x |
x |
typeof undefined |
"undefined" |
Both packages expose a single entry point:
func deadcode.Eliminate(p ast.VisitableNode, resolve bool)
func simplifier.Simplify(p ast.VisitableNode, resolve bool)The resolve parameter controls whether symbol resolution is performed before the transformation. Set it to true unless you have already called resolver.Resolve on the AST.