A JavaScript parser built from the Hermes engine that supports ES6, Flow, and JSX syntax
npx @tessl/cli install tessl/npm-hermes-parser@0.32.0Hermes Parser is a JavaScript parser built from Facebook's Hermes engine, compiled to WebAssembly for web and Node.js environments. It supports parsing ES6 (ES2015+), Flow type annotations, and JSX syntax with configurable options for different AST output formats (ESTree or Babel-compatible).
npm install hermes-parserimport { parse } from "hermes-parser";
// Or with additional utilities
import { parse, isNode, getVisitorKeys } from "hermes-parser";Advanced imports:
import {
parse,
SimpleTraverser,
SimpleTransform,
getVisitorKeys,
isNode,
FlowVisitorKeys,
astNodeMutationHelpers,
astArrayMutationHelpers,
Transforms,
mutateESTreeASTForPrettier,
ParserOptionsKeys
} from "hermes-parser";
// Traversal control constants available as static properties
const { Skip: SimpleTraverserSkip, Break: SimpleTraverserBreak } = SimpleTraverser;Individual utility imports:
import {
nodeWith,
shallowCloneNode,
deepCloneNode,
replaceNodeOnParent,
removeNodeOnParent,
setParentPointersInDirectChildren,
updateAllParentPointers,
arrayIsEqual,
insertInArray,
removeFromArray,
replaceInArray
} from "hermes-parser";For CommonJS:
const { parse } = require("hermes-parser");import { parse } from "hermes-parser";
// Parse JavaScript code
const ast = parse("const x = 1 + 2;");
// Parse with Flow types
const flowAst = parse("function add(x: number, y: number): number { return x + y; }", {
flow: "all"
});
// Parse JSX
const jsxAst = parse("const element = <div>Hello World</div>;");
// Output Babel-compatible AST
const babelAst = parse("const x = 1;", { babel: true });Hermes Parser is built around several key components:
Primary parsing functionality that converts JavaScript/Flow/JSX source code into Abstract Syntax Trees with comprehensive configuration options.
function parse(code: string, options?: ParserOptions): Program;
function parse(code: string, options: {...ParserOptions, babel: true}): BabelFile;Tools for walking and analyzing AST structures with flexible visitor patterns and control flow mechanisms.
class SimpleTraverser {
static traverse(node: ESNode, options: TraverserOptions): void;
traverse(node: ESNode, options: TraverserOptions): void;
}
interface TraverserOptions {
enter: (node: ESNode, parent?: ESNode) => void;
leave: (node: ESNode, parent?: ESNode) => void;
visitorKeys?: VisitorKeysType;
}Utilities for modifying and transforming AST nodes with immutable patterns and parent pointer management.
class SimpleTransform {
static transform(node: ESNode, options: TransformOptions): ESNode | null;
static transformProgram(program: Program, options: TransformOptions): Program;
}
interface TransformOptions {
transform: (node: ESNode) => ESNode | null;
visitorKeys?: VisitorKeysType;
}Helper functions for AST node manipulation, visitor key management, and compatibility utilities.
function getVisitorKeys(node: ESNode, visitorKeys?: VisitorKeysType): ReadonlyArray<string>;
function isNode(value: unknown): value is ESNode;
function mutateESTreeASTForPrettier(ast: Program, visitorKeys?: VisitorKeysType): void;interface ParserOptions {
allowReturnOutsideFunction?: boolean;
babel?: boolean;
flow?: "all" | "detect";
enableExperimentalComponentSyntax?: boolean;
enableExperimentalFlowMatchSyntax?: boolean;
reactRuntimeTarget?: "18" | "19";
sourceFilename?: string;
sourceType?: "module" | "script" | "unambiguous";
tokens?: boolean;
}
interface ESNode {
type: string;
range?: [number, number];
loc?: SourceLocation;
parent?: ESNode;
}
interface Program extends ESNode {
type: "Program";
body: Statement[];
sourceType: "script" | "module";
}
type VisitorKeysType = {
[nodeType: string]: ReadonlyArray<string>;
};/**
* Set of all valid ParserOptions keys for validation
*/
const ParserOptionsKeys: ReadonlySet<keyof ParserOptions>;Available as static properties of SimpleTraverser:
/**
* Error constant for skipping node traversal (throw to skip current node)
*/
SimpleTraverser.Skip: Error;
/**
* Error constant for breaking out of traversal (throw to stop traversal)
*/
SimpleTraverser.Break: Error;