Hermes Parser

0.35.0 · active · verified Sun Apr 19

Hermes Parser is a robust JavaScript parser built directly from the Hermes engine's parser, compiled to WebAssembly. This allows for high-performance parsing of JavaScript code in various environments, including Node.js and browsers. As of version 0.35.0, it supports parsing ES6+ syntax, Flow, and JSX. A key differentiator is its ability to output Abstract Syntax Trees (ASTs) in either the standard ESTree format or a Babel-compatible format, offering flexibility for integration into different tooling ecosystems. It also provides fine-grained control over Flow parsing, allowing users to explicitly enable Flow syntax detection or parse all ambiguous syntax as Flow. The project is actively maintained by Facebook, ensuring ongoing development and compatibility with modern JavaScript features.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates parsing JavaScript code with Flow and JSX syntax, showing how to toggle between ESTree and Babel AST formats, and control Flow parsing behavior. It also illustrates token extraction.

import { parse } from 'hermes-parser';

const codeWithFlowAndJSX = `
// @flow
type User = {
  id: string,
  name: string,
  isAdmin?: boolean,
};

const user: User = { id: '1', name: 'Alice' };

function greet(person: User): React.Element {
  return <div>Hello, {person.name}!</div>;
}

const element = greet(user);
`;

// Parse with ESTree format (default) and detect Flow via pragma
const estreeAst = parse(codeWithFlowAndJSX, {
  sourceType: 'module',
  flow: 'detect',
  sourceFilename: 'flow-jsx-example.js',
});

console.log('--- ESTree AST (excerpt) ---');
console.log(JSON.stringify(estreeAst.body[0], null, 2));

// Parse with Babel format and force all Flow parsing
const babelAst = parse(codeWithFlowAndJSX, {
  babel: true,
  sourceType: 'module',
  flow: 'all',
  sourceFilename: 'flow-jsx-example.js',
});

console.log('\n--- Babel AST (excerpt) ---');
console.log(JSON.stringify(babelAst.body[0], null, 2));

const simpleCode = `const x = 10;`;
const simpleAstTokens = parse(simpleCode, { tokens: true });
console.log('\n--- Tokens for simple code ---');
console.log(simpleAstTokens.tokens?.map(t => t.value));

view raw JSON →