Skip to content
tsrx-orgPublic

About

The fastest TypeScript parser, semantic analysis, and codegen toolchain

Resources

Stars

7 stars

Watchers

0 watching

Forks

Repository files navigation

@tsrx/yuku

A fast TSRX parser, analyzer, and code generator, built on Yuku. Maintained by Jack Shelton.

npm install @tsrx/yuku

Documentation · Playground

Why

Fast. 9.2x faster than the official parser, @tsrx/core, on the TSRX test suite, and 4.8x on the pages of tsrx.dev. See Benchmark.

ESTree. The tree is ESTree, with TSRX's node types as @tsrx/core names them, and errors carry the TSRXnnnn codes @tsrx/core reports.

Correct. Every TSRX source of the TSRX repository and of @tsrx/core's tests parses, or fails, as @tsrx/core parses it, except where it accepts what TypeScript rejects.

Small. One WebAssembly module, 746 kB and about 221 kB gzipped, with no native addons and no dependency but Yuku.

Anywhere. Node.js 16.4 or newer, Bun, Deno, Workers, and browsers, on every platform.

Usage

import { analyze, generate, parse } from "@tsrx/yuku";

const source = `export const List = ({ items }) => @{
  @for (const item of items; key item.id) {
    <li>{item.name}</li>
  } @empty {
    <p>Nothing yet</p>
  }
}`;

const { program, diagnostics } = parse(source);

analyze(source).rootScope.find("List");

generate(program).code;

parse builds an ESTree program with TSRX's own nodes, as @tsrx/core names them, and never throws: errors are in diagnostics, with the TSRXnnnn code @tsrx/core reports. It takes Yuku's options, path, sourceType, semanticErrors, attachComments, and tokens, and preserveParens, which is false as in @tsrx/core.

analyze gives Yuku's semantic model, and Analyzer analyzes a project of files with their imports and exports. generate prints TSRX back.

It runs in Node.js 16.4 or newer, Bun, Deno, Workers, and browsers.

Built on Yuku

Yuku is a JavaScript and TypeScript compiler toolchain written in Zig, documented at yuku.fyi. TSRX is a Yuku dialect: one Zig file, src/tsrx.zig, compiled into Yuku's parser. Yuku parses, analyzes, and prints the JavaScript and TypeScript around it, and the tree crosses into JavaScript in one buffer, decoded when you read program. How it works has the details.

Next, each <style> gets a CSS tree, parsed in the same pass by yuku-css, once yuku#280 lands. Today its CSS is one JSXText, as in @tsrx/core.

Benchmark

Parsing the 1,987 TSRX sources of the test suite against the official parser, @tsrx/core, both building an ESTree AST.

Time per file to parse with @tsrx/yuku and @tsrx/core

Median time per file over 3 runs, each in a fresh Node.js 24 process on an Apple M5 Pro. Run it with bun run bench.

The pages of tsrx.dev, 319 kB in all, in milliseconds, the same way over 5 runs:

page @tsrx/yuku @tsrx/core 0.5.5
specification 0.554 1.949
getting-started 0.431 2.059
features 0.376 1.726
compiler-demo 0.145 1.451
blog-rethinking-tsrx 0.094 0.396
blog-removing-lazy-destructuring 0.084 0.301
index 0.083 0.385
blog-simplifying-tsrx-after-feedback 0.069 0.334
blog 0.028 0.127
playground 0.013 0.093
layout 0.006 0.176
total 1.883 8.998

4.8x faster than @tsrx/core in all.

Development

Needs Zig 0.17 and Bun. build.zig.zon pins Yuku to the commit of the yuku version in package.json, and zig build fetches it. To work on both, check Yuku out next to this repository and set .yuku = .{ .path = "../yuku" } in build.zig.zon and "yuku": "file:../yuku/packages/yuku" in package.json.

bun install
bun run build      # zig build && tsdown
bun run typecheck
bun run test
bun run sync       # syncs test/suite with the TSRX repository at the release of @tsrx/core

The test suite is every .tsrx file of the TSRX repository and every case in the tests of @tsrx/core, each expected to parse, or fail, as the official parser parses it, except where it accepts what TypeScript rejects.

Thanks

Thanks to Arshad Yaseen, the author of Yuku, for his help.

License

MIT

About

The fastest TypeScript parser, semantic analysis, and codegen toolchain

Resources

Stars

7 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages