JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
—
Preconfigured parsers for different JavaScript dialects and advanced parser configuration options.
Default JavaScript parser supporting ECMAScript syntax.
// Usage: Use by default or explicitly
const ast = parse(source); // Uses esprima by default
const ast = parse(source, {
parser: require("recast/parsers/esprima")
});Import Path: recast/parsers/esprima
Supported Features:
TypeScript parser using Babel parser with TypeScript plugin.
// Usage: Explicitly specify TypeScript parser
const ast = parse(source, {
parser: require("recast/parsers/typescript")
});Import Path: recast/parsers/typescript
Prerequisites: Requires @babel/parser to be installed: npm install @babel/parser
Supported Features:
Usage Example:
import { parse, print } from "recast";
const tsCode = `
interface User {
name: string;
age: number;
}
const user: User = { name: "Alice", age: 30 };
`;
const ast = parse(tsCode, {
parser: require("recast/parsers/typescript")
});
const result = print(ast);Modern JavaScript parser supporting latest ECMAScript features.
// Usage: Use Babel parser for modern JavaScript
const ast = parse(source, {
parser: require("recast/parsers/babel")
});Import Path: recast/parsers/babel
Prerequisites: Requires @babel/parser to be installed: npm install @babel/parser
Supported Features:
Flow type annotation parser using Babel.
// Usage: Parse Flow annotated JavaScript
const ast = parse(source, {
parser: require("recast/parsers/flow")
});Import Path: recast/parsers/flow
Prerequisites: Requires @babel/parser to be installed: npm install @babel/parser
Supported Features:
Alternative JavaScript parser with configurable ECMAScript version support.
// Usage: Use Acorn parser
const ast = parse(source, {
parser: require("recast/parsers/acorn")
});Import Path: recast/parsers/acorn
Prerequisites: Requires acorn to be installed: npm install acorn
Supported Features:
Alternative TypeScript parser with JSX support using Babel.
// Usage: Parse TypeScript with JSX support
const ast = parse(source, {
parser: require("recast/parsers/babel-ts")
});Import Path: recast/parsers/babel-ts
Prerequisites: Requires @babel/parser to be installed: npm install @babel/parser
Differences from typescript parser:
Legacy alias for the Babel parser.
// Usage: Legacy Babylon parser (same as babel parser)
const ast = parse(source, {
parser: require("recast/parsers/babylon")
});Import Path: recast/parsers/babylon
Note: This is a legacy alias that exports the same functionality as the babel parser. Use the babel parser directly for new projects.
Create custom parser configurations for specific needs.
/**
* Custom parser object interface
*/
interface CustomParser {
parse(source: string): any;
}Usage Example:
import { parse } from "recast";
const customParser = {
parse(source: string) {
return require("acorn").parse(source, {
ecmaVersion: 2020,
sourceType: "module",
// custom options
});
}
};
const ast = parse(source, {
parser: customParser
});Pass additional options to preconfigured parsers.
Usage Example with TypeScript:
import { parse } from "recast";
import getBabelOptions from "recast/parsers/_babel_options";
const customTypeScriptParser = {
parse(source: string) {
const babelOptions = getBabelOptions();
babelOptions.plugins.push("typescript", "decorators-legacy");
return require("@babel/parser").parse(source, babelOptions);
}
};
const ast = parse(source, {
parser: customTypeScriptParser
});Esprima (Default):
TypeScript Parser:
Babel Parser:
Flow Parser:
Acorn Parser:
Shadow Copy Preservation: Always use recast.parse() rather than parsing directly with the underlying parser to maintain the shadow copy system that enables format preservation.
Babel Plugin Dependencies: Babel-based parsers (typescript, flow, babel) require @babel/parser to be installed separately.
Parser Consistency: Use the same parser for parsing and any subsequent operations to ensure AST compatibility.
Source Map Support: All parsers support source map generation when used through recast's parse function.
Install with Tessl CLI
npx tessl i tessl/npm-recast