Repository navigation
Conversation
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).
3 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
An opt-in erasable TypeScript mode in the TypeScript reference (
src/,src/effect/) and in the Rust implementation (rust/crates/psychoscript/).{ typescript: true }onCompileOptionsandProgramOptionsin TypeScript (runtime.compilereturning{ ok: true, value: Script }per feat!: compile returns a Script with its syntax tree #158 withscript.astholding the type-erased ESTreeProgram,runtime.program,runtime.execute,parseProgram).CompileOptions::typescript(true)withRuntime::compile_with, andProgramOptions::typescript(true), in Rust. The default staysfalse: plain JavaScript.#<linked-ts>turns the mode on per source for.tsmodule names.src/compiler/typescript.ts). It extends Acorn'sParser, with no new dependency, to parse TypeScript and erase type-only syntax straight to clean ESTree nodes. Source offsets are kept for diagnostics and forFunction.prototype.toString. Where TypeScript's grammar is ambiguous it followstsc:c ? (a) : b => dis(a)and then the alternate, andA extends infer T extends U ? X : YconstrainsT.rust/crates/psychoscript/src/compiler.rs). It parses withoxc'sSourceType::ts().with_module(true), as JavaScript mode parsesmjs: alone, a file is a script unless it has an import or export, and a top-levelfor awaitdoes not parse in a script. It validates non-erasable constructs before it compiles, erases type-only statements, members, wrappers and bindings (adeclareambient declaration does not shadow a host global), and strips modifiers inFunction.prototype.toStringassrc/compiler/scope.tsdoes.thisparameters, optional?, definite!and type predicates included);interface;type;as(as constincluded);satisfies; non-null!(in expressions, optional chains, calls,delete, assignments, updates, destructuring); generics and instantiation expressions;import type, inline type specifiers,export type,export interfaceand exported type aliases;declareambient declarations; type-onlynamespaceandmodule; overload signatures; and class modifiers (abstract,declare, index signatures, andpublic,private,protected,readonly,overrideon members that are not parameters).UnsupportedSyntaxat the exact line and column (formatted with\n at <program>:line:colper 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), astsc --erasableSyntaxOnlyrefuses them:enumandconst enum,namespaceandmodulewith runtime values, constructor parameter properties, decorators (@decin TypeScript mode; JavaScript mode keeps itsParseError),import =,export =, and<T>exprangle-bracket assertions.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'swasm32compile guard).TsBigIntLimitParserextendsBigIntLimitParserwith the TypeScript plugin). The compile cache keys a text by its mode (ts:orjs:), so the two parses of one text never collide.src/compiler/typescript.tsuses no Node-only import or global (test/source-portability.test.ts).typescript: truein the snapshot header, in both implementations, so a TypeScript run suspends and resumes, in round-trip mode too.README.md,interpreter-support.mdandrust/README.md.Why
Script plugins for the OpenCode Rust client are TypeScript files, checked by
tsc --erasableSyntaxOnlyagainst a generated.d.ts. With erasable TypeScript in both implementations, authors write and run.tssources 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,abstractin a class that isn't, a second declaration of adeclared name or of a type alias. The reference skips types without checking them and runs these.tscreports them all.rust/README.mdlists the difference.Upstream
Rebased onto
origin/dev(dec90d1; tipcd6efb0, backupbak/47ff595/erasable-ts402fe0d). Nothing was superseded by merged upstream work.Adapted to merged upstream changes:
960bb0e): updatedsrc/runtime.ts,src/effect/runtime.ts,src/runtime/execute.ts, andtest/typescript.test.tstoScript/{ ok: true, value: Script }, and assertedcompiled.value.astequalsparseProgram(code, { typescript: true }).program.4d6ba6b): kept the#[cfg(target_arch = "wasm32")]guard inrust/crates/psychoscript/src/run.rs::compile.0fea774), feat(rust): report a failed run as its error and stack, as dev's after #123 (2/2) #168 (e364475): regeneratedrust/conformance/typescript.jsonfor\n at <program>:line:coldiagnostic formatting; verifiedtypescript-erasure.jsonmatches cleanly.rust/README.md.Compared with open PR #159 (
d926fa6, stacked on #157880f23f):compiler.rs,run.rs,runtime.rs, andsnapshot.rs, plus aCompileOptions/Runtime::compile_withAPI collision inlib.rsandruntime.rs(both agree onheader.source.typescript = truein snapshots).erasable-tshas no dependency on feat(rust): persistent Session and zero-copy HostCtx API #157 (Session), implements both the TypeScript reference (src/compiler/typescript.ts, zero new dependencies) and Rust with a 36-case parity recording and conformance case, and matchestsc --erasableSyntaxOnlymore closely: refuses<T>exprand decorators withUnsupportedSyntax(whereas feat(rust): add precompiled named modules with per-module stacks and snapshot durability #159 accepts<T>expr, keeps decorators asParseError, and reports non-erasable TS asParseError), erases type-onlynamespace(whereas feat(rust): add precompiled named modules with per-module stacks and snapshot durability #159 rejects all non-ambientnamespace), parses TS withSourceType::ts().with_module(true)and keeps theawait-as-identifier fallback (whereas feat(rust): add precompiled named modules with per-module stacks and snapshot durability #159 usesSourceType::ts(), breaking top-levelfor awaitin TS withoutimport/export), adds TS-awarenesting::bound, and strips TS method modifiers inFunction.prototype.toString. feat(rust): add precompiled named modules with per-module stacks and snapshot durability #159 additionally hasRuntime::typescript(bool),CompileOptions::module_name/max_source_bytes, and allows emptyexport {}, bundled alongside itsSessionprecompiled-module loader. Iferasable-tslands first, feat(rust): add precompiled named modules with per-module stacks and snapshot durability #159 can drop its TS-stripping half and focus onSession::eval_compiled.Tests
test/typescript.test.ts. It replays the parity recording below against the reference, sobun testfails 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(includingscript.ast), and suspend and resume through bytes.rust/conformance/typescript.tswritesrust/conformance/typescript.json, andrust/crates/psychoscript/tests/typescript.rsreplays it. 36 cases assert identical values, compile outcomes, error kinds, line and column, andUnsupportedSyntaxmessages, and Rust suspends and resumes. Among them: a top-levelfor await, the conditional andinferforms above, and an export list refused as JavaScript mode refuses it.// @typescript truesupport torust/conformance/generate.tsandrust/bench/tests/conformance.rs, and the caserust/conformance/cases/typescript-erasure.js, which runs in the normal and the round-trip conformance runs.typescript_hides_no_nesting_either(nesting.rs) andtypescript_nesting_does_not_overflow_the_stack(tests/typescript.rs).dec90d1: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).bun conformance/typescript.tsandtypescript-erasure.jscheck — zero drift.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).