or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdparsing.mdtransformation.mdtraversal.mdutilities.md
tile.json

tessl/npm-hermes-parser

A JavaScript parser built from the Hermes engine that supports ES6, Flow, and JSX syntax

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/hermes-parser@0.32.x

To install, run

npx @tessl/cli install tessl/npm-hermes-parser@0.32.0

index.mddocs/

Hermes Parser

Hermes 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).

Package Information

  • Package Name: hermes-parser
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation:
    npm install hermes-parser

Core Imports

import { 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");

Basic Usage

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 });

Architecture

Hermes Parser is built around several key components:

  • Core Parser: WebAssembly-compiled Hermes parser engine for high-performance parsing
  • AST Adapters: Transform native Hermes AST to ESTree or Babel-compatible formats
  • Configuration System: Flexible parsing options for different JavaScript dialects and output formats
  • Traversal System: Tools for walking and analyzing AST structures
  • Transform System: Utilities for modifying and transforming AST nodes
  • Type Safety: Full Flow/TypeScript integration with comprehensive type definitions

Capabilities

Core Parsing

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;

Parsing

AST Traversal

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;
}

Traversal

AST Transformation

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;
}

Transformation

AST Utilities

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;

Utilities

Types

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>;
};

Constants

/**
 * Set of all valid ParserOptions keys for validation
 */
const ParserOptionsKeys: ReadonlySet<keyof ParserOptions>;

Traversal Control Constants

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;