This guide focuses on flAST's current public API surface and the behaviors that tend to matter in real analysis or transformation code.
- Exports
generateFlatAST(inputCode, opts?)ArboristapplyIteratively(script, funcs, maxIterations?)loggergenerateCode(rootNode, opts?)generateRootNode(inputCode, opts?)parseCode(inputCode, opts?)- Node Metadata Cheat Sheet
- Behavior Notes That Matter In Practice
import {
Arborist,
applyIteratively,
generateFlatAST,
logger,
generateCode,
generateRootNode,
parseCode,
} from 'flast';The main flAST entry point. Returns an ordered flat array of enriched nodes.
ASTNode[][]for invalid input
nodeIdsrcparentNodeparentKeychildNodesdeclNodereferencesscopescopeIdlineageancestry
typeMapallScopes
- Default:
true - When
false, scope and identifier-relation enrichment is skipped.
- Default:
true - When
false, nodes do not store original source slices insrc
- Default:
true - Retries parse with
sourceType: 'script'after a compatible module parse failure
- Forwarded to Espree
const ast = generateFlatAST(code, {
detailed: true,
includeSrc: true,
alternateSourceTypeOnFailure: true,
});Safe mutation helper for replacing and deleting nodes, then validating the resulting script.
const arbFromCode = new Arborist(script);
const arbFromAst = new Arborist(generateFlatAST(script));script: current generated scriptast: current flat ASTmarkedForDeletionreplacementsappliedCounterlogger
- Low-level queueing primitive used by the convenience helpers below
- Marks a node for replacement when
replacementNodeis provided - Marks a node for deletion when omitted
- Queues a replacement without relying on an optional second argument
- Preferred in examples and user-facing code when you are replacing a node
- Queues a deletion without relying on an omitted argument
- Preferred in examples and user-facing code when you are deleting a node
- Returns the number of queued mutations
- Applies queued replacements/deletions
- Regenerates code
- Reparses the result
- Reverts if the generated code is invalid
- Returns number of applied changes
- Deleting a node may target a higher removable parent for validity
- Deleting or replacing the root behaves differently from leaf edits
- Comments are merged and preserved where possible, but complex transforms should still be tested
Runs one or more Arborist-based transforms repeatedly until no changes are made or the iteration limit is reached.
function transform(arb) {
for (const node of arb.ast[0].typeMap.Literal) {
if (node.value === 'a') {
arb.replaceNode(node, {type: 'Literal', value: 'b', raw: "'b'"});
}
}
return arb;
}
const result = applyIteratively(script, [transform], 3);- Useful when one transform unlocks another in a later pass
- Resilient against invalid end states because Arborist validates changes
- Later transforms can still run even if an earlier one throws
Simple shared logger used by flAST utilities.
setLogLevelDebug()setLogLevelLog()setLogLevelError()setLogLevelNone()setLogFunc(fn)
import {logger} from 'flast';
logger.setLogLevelDebug();
logger.setLogFunc((...args) => {
console.error('[flast]', ...args);
});Generates JavaScript source from an AST node.
- generated code string
- Uses escodegen under the hood
- Supports escodegen-style generation options
- Preserves comments when present and supported by the node structure
Parses input and returns a root node or null.
Programnode on successnullon invalid input
- When
alternateSourceTypeOnFailureis enabled, flAST can retry parsing withsourceType: 'script'. - When
includeSrcis enabled, the root getssrc.
Parses JavaScript with Espree and returns the parser root node.
ProgramAST root
- flAST enables comment and range support.
- Comments are attached when tokens are available.
- Use this when you want parser output directly rather than flAST's flat representation.
parentNodechildNodesparentKey
srcstartendrangeloc
nodeIdtype
scopescopeIdlineageancestrydeclNodereferences
generateFlatAST('return a;', {alternateSourceTypeOnFailure: false})returns[].generateRootNode('return a;', {alternateSourceTypeOnFailure: false})returnsnull.typeMapreturns[]for missing node types.- Module-scope parse failures may be retried in script mode.
detailed: falsetrades metadata for speed and lower memory use.