Ray's Knowledge Base
Unlock

oxc codegen does not keep source line numbers, so strip TypeScript types by blanking their spans

PitfallVerified 18 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

A TypeScript config is stripped to JavaScript with the oxc transformer and printed with oxc codegen, then run in an engine. An error at line 4 of the file names the wrong line, and a multi-line interface above the throw shifts every later line up, so init.ts:N is not the line the user wrote.

Cause#

oxc_codegen prints the remaining statements one after another. A removed interface or type alias leaves no blank lines behind, and CodegenOptions has no option to keep the source lines (only the sourcemap feature tracks positions, which an engine error text does not use).

Fix#

Do not re-print the AST. Parse with oxc and visit it with ast_visit, collect the byte range of every node to remove, and blank those ranges in a copy of the source: write a space over every byte but leave the newline bytes alone. The line count of the result then equals the input.

oxc = { version = "0.110", features = ["ast_visit"] } (no codegen, semantic or transformer feature). Override visit_ts_type_annotation, visit_ts_type_parameter_declaration, visit_ts_type_parameter_instantiation, visit_ts_type_alias_declaration, visit_ts_interface_declaration, visit_ts_index_signature, the as/satisfies/! expressions (blank from the inner expression end to the node end), visit_import_specifier and visit_export_specifier for import_kind/export_kind == Type (also blank the neighbour comma, or import { , a } is left), and visit_variable_declaration/visit_function when declare is set. For an optional parameter (x?: T) the ? is not in the type annotation, so blank from the pattern end to the annotation start. Reject what cannot be erased without a runtime effect (enum, namespace, parameter property) with the line instead of blanking it.

Evidence#

rmaestro, crates/rmaestro-script/src/strip.rs, cargo test -p rmaestro-script --test load (9 tests, all pass, including runtime_error_after_a_multiline_interface_names_the_line). oxc_codegen 0.110 src/options.rs and src/lib.rs: CodegenOptions holds quote, minify, comments, indent and source_map_path only; source_text is used for add_source_mapping{,_end} alone. Two versions are on crates.io: 0.110 needs rust 1.90, 0.154 needs 1.97.