Skip to content

feat: erase type-only TypeScript syntax in both implementations - #179

Closed
simonklee wants to merge 1 commit into
devfrom
erasable-ts
Closed

simonklee wants to merge 1 commit into
devfrom
erasable-ts

Conversation

@simonklee

Copy link
Copy Markdown
Member

What

An opt-in erasable TypeScript mode in the TypeScript reference (src/, src/effect/) and in the Rust implementation (rust/crates/psychoscript/).

  • API. { typescript: true } on CompileOptions and ProgramOptions in TypeScript (runtime.compile returning { ok: true, value: Script } per feat!: compile returns a Script with its syntax tree #158 with script.ast holding the type-erased ESTree Program, runtime.program, runtime.execute, parseProgram). CompileOptions::typescript(true) with Runtime::compile_with, and ProgramOptions::typescript(true), in Rust. The default stays false: plain JavaScript. #<linked-ts> turns the mode on per source for .ts module names.
  • TypeScript reference (src/compiler/typescript.ts). It extends Acorn's Parser, with no new dependency, to parse TypeScript and erase type-only syntax straight to clean ESTree nodes. Source offsets are kept for diagnostics and for Function.prototype.toString. Where TypeScript's grammar is ambiguous it follows tsc: c ? (a) : b => d is (a) and then the alternate, and A extends infer T extends U ? X : Y constrains T.
  • Rust compiler (rust/crates/psychoscript/src/compiler.rs). It parses with oxc's SourceType::ts().with_module(true), as JavaScript mode parses mjs: alone, a file is a script unless it has an import or export, and a top-level for await does not parse in a script. It validates non-erasable constructs before it compiles, erases type-only statements, members, wrappers and bindings (a declare ambient declaration does not shadow a host global), and strips modifiers in Function.prototype.toString as src/compiler/scope.ts does.
  • Erased syntax. Type annotations on variables, parameters, returns, catch clauses and fields (this parameters, optional ?, definite ! and type predicates included); interface; type; as (as const included); satisfies; non-null ! (in expressions, optional chains, calls, delete, assignments, updates, destructuring); generics and instantiation expressions; import type, inline type specifiers, export type, export interface and exported type aliases; declare ambient declarations; type-only namespace and module; overload signatures; and class modifiers (abstract, declare, index signatures, and public, private, protected, readonly, override on members that are not parameters).
  • Refused at compile time with UnsupportedSyntax at the exact line and column (formatted with \n at <program>:line:col per feat: report a failed run as its error and stack, as Node prints it #123/feat(rust): report a failed run as its error and stack, as dev's after #123 (2/2) #168), as tsc --erasableSyntaxOnly refuses them: enum and const enum, namespace and module with runtime values, constructor parameter properties, decorators (@dec in TypeScript mode; JavaScript mode keeps its ParseError), import =, export =, and <T>expr angle-bracket assertions.
  • Nesting. nesting::bound(code, typescript) reads the nesting that TypeScript syntax hides from the JavaScript scan, so a TypeScript source parses on a thread with the stack it needs (preserving feat(rust): optional locale feature and wasm32 compile guard #153's wasm32 compile guard).
  • Limits and caches. TypeScript mode enforces the BigInt literal bound of JavaScript mode (TsBigIntLimitParser extends BigIntLimitParser with the TypeScript plugin). The compile cache keys a text by its mode (ts: or js:), so the two parses of one text never collide. src/compiler/typescript.ts uses no Node-only import or global (test/source-portability.test.ts).
  • Snapshots. A saved source records typescript: true in the snapshot header, in both implementations, so a TypeScript run suspends and resumes, in round-trip mode too.
  • Docs. README.md, interpreter-support.md and rust/README.md.

Why

Script plugins for the OpenCode Rust client are TypeScript files, checked by tsc --erasableSyntaxOnly against a generated .d.ts. With erasable TypeScript in both implementations, authors write and run .ts sources directly, with no transpiler in between. JavaScript mode is untouched, and Rust stays at parity with the reference.

Known difference

In TypeScript mode Rust also refuses what oxc's own TypeScript checks refuse, as a ParseError: a class member declared twice, a required parameter after an optional one, abstract in a class that isn't, a second declaration of a declared name or of a type alias. The reference skips types without checking them and runs these. tsc reports them all. rust/README.md lists the difference.

Upstream

Rebased onto origin/dev (dec90d1; tip cd6efb0, backup bak/47ff595/erasable-ts 402fe0d). Nothing was superseded by merged upstream work.

Adapted to merged upstream changes:

Compared with open PR #159 (d926fa6, stacked on #157 880f23f):

Tests

  • Added test/typescript.test.ts. It replays the parity recording below against the reference, so bun test fails when the reference's parser changes and not only when the recording is regenerated. It also covers the Promise API, the Effect API, parseProgram, compile (including script.ast), and suspend and resume through bytes.
  • Added a parity suite recorded from the reference: rust/conformance/typescript.ts writes rust/conformance/typescript.json, and rust/crates/psychoscript/tests/typescript.rs replays it. 36 cases assert identical values, compile outcomes, error kinds, line and column, and UnsupportedSyntax messages, and Rust suspends and resumes. Among them: a top-level for await, the conditional and infer forms above, and an export list refused as JavaScript mode refuses it.
  • Added // @typescript true support to rust/conformance/generate.ts and rust/bench/tests/conformance.rs, and the case rust/conformance/cases/typescript-erasure.js, which runs in the normal and the round-trip conformance runs.
  • Added typescript_hides_no_nesting_either (nesting.rs) and typescript_nesting_does_not_overflow_the_stack (tests/typescript.rs).
  • Verified on dec90d1:
    • TypeScript: bun run typecheck, bun tsc -p tsconfig.build.json --noEmit, bun test test/typescript.test.ts test/language.test.ts test/source-portability.test.ts (28 pass), bun test test/typescript.test.ts test/errors.test.ts test/classes.test.ts test/parity.test.ts (276 pass), bun test --timeout 60000 test/snapshot.test.ts (38 pass), PSYCHOSCRIPT_ROUNDTRIP=1 bun test test/typescript.test.ts (4 pass), bun run pack (Node and Bun).
    • Recording drift: bun conformance/typescript.ts and typescript-erasure.js check — zero drift.
    • Rust: cargo fmt --all --check, cargo clippy --locked -p psychoscript -p psychoscript-bench --all-targets -- -D warnings, cargo test --locked -p psychoscript --lib nesting (8/8 pass), cargo test --locked -p psychoscript --test typescript --test host --test snapshot (53/53 pass), CONFORMANCE_FILTER=typescript-erasure cargo test --locked -p psychoscript-bench --test conformance (pass).

Add opt-in erasable TypeScript mode via typescript: true on compile, program, execute, and parseProgram (CompileOptions::typescript and ProgramOptions::typescript in Rust, with Runtime::compile_with).

Type-only syntax is erased at compile time in both the Acorn-based TypeScript reference and the oxc-based Rust compiler: type annotations, interface, type, as, satisfies, non-null !, generics and instantiation expressions, import type / export type, declare ambient statements, type-only namespace, overload signatures, and class member modifiers. Non-erasable syntax with runtime meaning (enum, namespace with values, constructor parameter properties, decorators, import =, export =, and <T>expr assertions) fails at compile time with UnsupportedSyntax at its exact line and column. Snapshots record typescript: true on saved sources so TypeScript runs suspend and resume across both runtimes.

Includes TS unit tests, a 46-case reference-recorded parity suite (rust/conformance/typescript.ts -> typescript.json -> tests/typescript.rs), and a round-trip conformance case (typescript-erasure).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant