Skip to content

feat!: use SWC Next for type stripping and remove transform mode - #507

Merged
marco-ippolito merged 17 commits into
nodejs:mainfrom
kdy1:kdy1/try-swc-next
Dec 3, 2026
Merged

marco-ippolito merged 17 commits into
nodejs:mainfrom
kdy1:kdy1/try-swc-next

Conversation

@kdy1

@kdy1 kdy1 commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Summary

Replace swc_ts_fast_strip with the SWC Next crates from crates.io, pinned to 0.9.1, for TypeScript type stripping. Stripping keeps the fixed-width output: every UTF-16 offset in the output is the same as in the input. The transformSync API and the Node.js REPL helpers do not change.

Remove mode: "transform", the amaro/transform loader, the transformation-only options, and their dependencies and tests. This is consistent with the removal of transformation support in nodejs/node#61803.

This is a breaking change. It must be released as a new major version of Amaro (2.0.0).

Breaking changes

  • mode: "transform" is removed. A call with it throws unknown variant `transform`, expected `strip-only` . It does not fall back to stripping.
  • The amaro/transform export is removed. require.resolve("amaro/transform") throws ERR_PACKAGE_PATH_NOT_EXPORTED.
  • In the TypeScript types, Options no longer has transform and sourceMap, Options["mode"] is only "strip-only", and TransformOutput no longer has map.
  • Every import = declaration is rejected with UnsupportedSyntax. This includes type-only forms, for example import type fs = require("node:fs") and export import type A = N.B. Amaro 1.x erases the type-only forms. This matches erasableSyntaxOnly in tsc 5.9.
  • Error messages come from SWC Next. Each message names the unsupported construct, for example TypeScript enum declarations are not supported in type stripping instead of TypeScript enum is not supported in strip-only mode. The message for an angle-bracket type assertion ends with Use `expr as T` instead. Error codes (UnsupportedSyntax, InvalidSyntax) and location fields do not change.
  • A string with a type suffix that would become a directive after stripping, for example 'use strict' as any; at the start of a script or function body, is rejected with InvalidSyntax. Amaro 1.x erases the suffix, and the string becomes a directive.
  • The stripper does not report strict-mode early errors, for example with statements and legacy octal literals in module code. V8 reports them when it loads the output.

Changes

  • crates/amaro/src/strip.rs adapts SWC Next to Amaro:
    • It removes a leading BOM and reports diagnostics in source order.
    • It selects the UnsupportedSyntax code from StripResult::unsupported_syntax (feat(type-strip)!: report the kind of non-erasable syntax swc-project/swc-next#1702), not from the message text. It adds the help text of that diagnostic to the message.
    • It keeps the error for the deprecated module keyword (deprecatedTsModuleAsError). A fast path skips the second parse when no erased module word can start a declaration.
    • It honors parser.decorators: false. Only tsx and decorators in parser have an effect.
    • It rejects import and export declarations when module: false, because SWC Next accepts them in scripts.
  • crates/amaro/src/options.rs defines the public Options and TransformOutput types.
  • Dependencies:
    • Remove swc_ts_fast_strip and the SWC transform, codegen, and source map crates.
    • Add swc_next_allocator, swc_next_ecma_ast, swc_next_ecma_parser, and swc_next_type_strip at =0.9.1.
    • Keep the published SWC crates for the REPL helpers and diagnostic formatting.
    • Allow the Zlib license for foldhash in deny.toml. It comes in through hashbrown 0.17.1.
    • Update the Rust toolchain from 1.98.1 to 1.99.0.
  • Update the README, the loader tests, and the hook registration tests. Remove the transform tests and snapshots.

Performance

With SWC Next 0.6.1, on typescript.js (9.1 MB), stripping takes 56.3 ms instead of 152.8 ms with Amaro 1.3.0, and the output is the same. The full benchmark table is in the PR comments.

Validation

At 82b96fcc:

  • cargo test --locked: 26 passed.
  • cargo clippy --all-targets -- -D warnings and cargo fmt --check: passed.
  • npm run typecheck, npm run build, and biome ci: passed.
  • npm test: 101 passed, with a local WebAssembly build (cargo build --release --target wasm32-unknown-unknown and wasm-bindgen 0.2.100).

With SWC Next 0.6.1, a differential test against swc_ts_fast_strip 59.0.2 found 0 output differences on 40,356 real-world units. The PR comments have the details and the TypeScript test-suite results.

Review status

The bug analysis of the related swc-next components is complete, and the self-review found no blocking problem. The passing tests above do not establish complete compatibility or prove that swc-next is free of bugs.

AI assistance and human review

AI was used to prepare these changes. Human-reviewed by @kdy1.

  • Harness: T3 Code (Codex harness)
  • Model: gpt-6.1-sol
  • Reasoning Effort: medium

The SWC Next 0.6.1 update (9dea4ed0a) and the SWC Next 0.9.1 update (aba22c08, 82b96fcc) were prepared with Claude Code (Claude Opus 5.5). This description was updated to match the diff at 9daedd0a with Claude Code (Claude Opus 5.5) in T3 Code.

@marco-ippolito

Copy link
Copy Markdown
Member

Amazingg work, does this boost the performance?

@kdy1

kdy1 commented Oct 31, 2026

Copy link
Copy Markdown
Member Author
image

It's currently mixed, so I'll optimize the swc-next first

@kdy1

kdy1 commented Nov 1, 2026

Copy link
Copy Markdown
Member Author
image

It's now faster for all cases! The swc-next commit I used is swc-project/swc-next@95e2394, and I'll try to find and fix any remaining bugs before merging.

kdy1 and others added 2 commits November 2, 2026 18:59
`deprecatedTsModuleAsError` is enabled by default, and the check parsed the
whole input a second time when the text contained "module". Most real files
contain that word in `module.exports`, `node:module`, or comments, so most
inputs were parsed twice.

Run the second parse only when a `module` word is followed by a possible
declaration name and that word is erased in the stripped output.
`strip_types` erases every `module` declaration as a whole statement, so a
keyword that survives unchanged is in a comment, a string, or an
expression. Inputs with strip diagnostics keep the previous behavior.

Results for 442 corpus variants, including injected declarations, are
identical to the previous build. This includes error codes, messages, and
locations. Median `transformSync` time in the release WASM build:

- hono-types.ts (76 KB): 1.11 ms -> 0.68 ms
- checker.ts (3 MB): 34.7 ms -> 21.0 ms
- typescript.js (8 MB): 100.7 ms -> 62.9 ms
- astro-config.ts (125 KB): 0.97 ms -> 1.05 ms (the parse is still needed
  because a comment in an erased interface contains "module ...")

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 added a commit to swc-project/swc-next that referenced this pull request Nov 2, 2026
## Motivation

On targets without a native SIMD backend, including
`wasm32-unknown-unknown` without `simd128`, `swc_next_simd` processed
each 16-byte vector one lane at a time. Loops written with `u8x16` were
then much slower than byte loops. Block comment scanning in the lexer
and blanking in `strip_types` were affected the most. For example, on a
1 MiB single-line block comment, swc-next parse took 5.4 ms, and the old
`swc_ecma_parser` took 0.35 ms. Node.js amaro and the swc-next npm
WebAssembly bindings build for this target without `simd128`.

## Changes

- The scalar backend now processes each vector as two `u64` words
(SWAR). The public API, the per-lane results, and the x86_64, aarch64,
and wasm32 `simd128` backends are unchanged. Call sites are unchanged.
- The scalar module is now compiled for tests on every host. New tests
place every byte pair in every lane position and compare each operation
with a per-lane reference. They also cover lane boundaries, `first_set`,
and `any`.
- On wasm32 without `simd128` (`opt-level = "z"`, fat LTO), compared
with `main`:
  - Parse of a 1 MiB single-line block comment: 5.39 ms → 0.79 ms.
  - Parse of 1 MiB of JSDoc comments: 2.16 ms → 0.45 ms.
- `strip_types` on `tasks/benchmark/files/astro-config.ts`: 0.57 ms →
0.11 ms.
  - No input is slower.
- The `strip_types` output and diagnostics are identical to `main` for
22 inputs: the benchmark files, the inputs from #1418, and edge cases
for line terminators, `0xE2` sequences, unterminated comments, and
16-byte boundaries.

## Related Links

- Close #1418
- nodejs/amaro#507

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
`strip_types` erases every `module` declaration, so compare the input
with the stripped output and check only `module` words in erased text.
This replaces the full-text `match_indices` search and the UTF-16 offset
mapping. Also skip a `module` word that follows a non-keyword word on
the same line, because it cannot start a statement. This avoids the
second parse for prose such as `its module dependency` in comments
inside erased interfaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 added a commit to swc-project/swc-next that referenced this pull request Nov 3, 2026
## Motivation

Size-optimized builds keep the `#[inline]` wrappers in `swc_simd` as
function calls. Amaro builds `swc_next_type_strip` for
`wasm32-unknown-unknown` with `opt-level = "z"` and without `simd128`,
so the scalar backend runs. In a profile of that build, `simd_eq` and
`first_set` stay separate functions inside the block comment scanner,
and each call passes a 16-byte vector through memory. A 1 MB block
comment on one line takes 2.44 ms with `main`, compared with 1.72 ms on
Amaro `main`, which uses `swc_ts_fast_strip`.

## Changes

- Mark the vector and mask operations in `crates/swc_simd/src/lib.rs`
`#[inline(always)]`, like the backend functions that they call. Behavior
does not change.
- Amaro `transformSync`, wasm32 without `simd128`, this branch compared
with `main` (2fe537f). Medians of 6 rounds, measured locally:

  | Input | `main` | This PR | Speedup |
  | --- | ---: | ---: | ---: |
  | 1 MB block comment, one line | 2.44 ms | 2.19 ms | 1.11× |
  | 1 MB block comment, many lines | 2.00 ms | 1.91 ms | 1.05× |
  | 1 MB of JSDoc blocks | 2.14 ms | 2.04 ms | 1.05× |
  | 1 MB of plain block comments | 2.07 ms | 1.97 ms | 1.05× |
  | `typescript.js` (8 MB) | 56.61 ms | 54.88 ms | 1.03× |

The other measured inputs (jQuery, three.js, `checker.ts`, cal.com,
Hono, Effect, and 1 MB string, type, line comment, and whitespace
inputs) changed by less than 1.5%.
- The output matched the `main` build for 400 inputs: the Amaro
benchmark corpus and synthetic inputs, each also with comments, line
separators, and whitespace inserted.
- `cargo fmt --check`, `cargo clippy -- -D warnings` (host and
`wasm32-unknown-unknown`), and the tests of `swc_next_simd`,
`swc_next_ecma_parser`, `swc_next_type_strip`, and
`swc_next_ecma_codegen` pass. Native wall time was not measured.

## Related Links

- #1419
- nodejs/amaro#507

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@kdy1

kdy1 commented Nov 3, 2026

Copy link
Copy Markdown
Member Author

@marco-ippolito Is removing mode: "transform" fine? I'm not sure if it's acceptable at this stage.

@kdy1

kdy1 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member Author

Performance profiling result

  • idle linux machine
Input main PR + 0.5.0 PR + swc-next main vs main vs previous run
typescript.js (8 MB) 135.98 ms 55.94 ms 50.53 ms 2.69× 1.09×
checker-noenum.ts 44.61 ms 18.92 ms 17.80 ms 2.51× 1.09×
three-0.138.3.js 22.16 ms 10.05 ms 8.80 ms 2.52× 1.09×
cal.com-noenum.tsx 19.68 ms 7.92 ms 6.96 ms 2.83× 1.08×
jquery.mobile-1.4.2.js 7.95 ms 3.50 ms 3.23 ms 2.46× 1.07×
angular-1.2.5.js 6.82 ms 3.92 ms 3.07 ms 2.22× 1.15×
jquery-1.9.1.js 5.21 ms 2.32 ms 2.20 ms 2.37× 1.08×
mootools-1.4.5.js 4.02 ms 1.71 ms 1.59 ms 2.53× 1.07×
yui-3.12.0.js 3.97 ms 2.14 ms 1.72 ms 2.31× 1.14×
effect-schema-ast.ts 2.43 ms 1.13 ms 989.9 µs 2.46× 1.07×
err-large-enum.ts 1.70 ms 814.2 µs 651.7 µs 2.61× 1.64×
hono-types.ts 1.60 ms 671.6 µs 504.2 µs 3.17× 1.01×
backbone-1.1.0.js 907.1 µs 358.9 µs 337.8 µs 2.69× 1.14×
underscore-1.5.2.js 764.4 µs 278.9 µs 264.6 µs 2.89× 1.10×
astro-config.ts 388.5 µs 724.2 µs 239.0 µs 1.63× 1.53×
err-small.ts 7.6 µs 7.4 µs 7.4 µs 1.03× 1.00×
hello.ts 2.3 µs 1.6 µs 1.5 µs 1.55× 1.03×
hono-types.ts (check off) 1.61 ms 647.3 µs 481.2 µs 3.34× 1.02×
astro-config.ts (check off) 389.0 µs 676.4 µs 195.4 µs 1.99× 1.08×
checker-noenum.ts (check off) 45.24 ms 18.46 ms 17.37 ms 2.60× 1.01×
Synthetic: block-comments 1 MB 1.92 ms 3.60 ms 1.77 ms 1.09× 1.21×
Synthetic: plain-block-comments 1 MB 1.92 ms 3.39 ms 1.74 ms 1.10× 1.19×
Synthetic: line-comments 1 MB 2.05 ms 1.77 ms 1.77 ms 1.16× 1.11×
Synthetic: one-big-comment-lines 1 MB 1.71 ms 3.42 ms 1.68 ms 1.02× 1.19×
Synthetic: one-big-comment 1 MB 1.71 ms 6.76 ms 1.87 ms 0.91× 1.29×
Synthetic: strings 1 MB 7.27 ms 7.29 ms 3.73 ms 1.95× 1.07×
Synthetic: types-only 1 MB 26.60 ms 11.33 ms 9.33 ms 2.85× 0.99×
Synthetic: whitespace 1 MB 1.73 ms 1.96 ms 1.98 ms 0.87× 1.04×

@marco-ippolito

Copy link
Copy Markdown
Member

@marco-ippolito Is removing mode: "transform" fine? I'm not sure if it's acceptable at this stage.

I think its acceptable as semver major change

kdy1 added a commit to swc-project/swc-next that referenced this pull request Nov 3, 2026
…ckend (#1443)

## Motivation

#1422 made block comments slower in Amaro's `wasm32-unknown-unknown`
build (`opt-level = "z"`, no `simd128`), which uses the scalar backend.
In Amaro `transformSync`, a 1 MB block comment on one line went from
1.87 ms to 5.09 ms. In a profile of that build,
`Lexer::scan_block_comment` is no longer inlined into `next_token`.
Inside it, `<[u8; 16] as IndexMut<RangeTo<usize>>>::index_mut` stays a
separate call and takes 10% of the time. This call comes from
`lanes[..8].copy_from_slice(..)` in the scalar backend. `Option::expect`
in `Simd::from_slice` also stays a call. The 1 MB comment inputs do not
call `TokenReader::read`, so the slowdown comes from code generation,
not from extra work.

## Changes

- The scalar backend's `lanes()` builds the array by destructuring the
two words, and `Simd::from_slice` matches on `first_chunk` instead of
calling `expect`. Behavior does not change. Only the scalar backend uses
`lanes()`. The `from_slice` change applies to all backends.
- Amaro `transformSync`, wasm32 without `simd128`. I measured the same
diff locally on `69b31108`. Since then, `main` changed only
`crates/swc_type_strip` (#1442). Medians of 4 rounds:

| Input | Before #1422 (87adca5) | `69b31108` | `69b31108` + this PR |
  | --- | ---: | ---: | ---: |
  | 1 MB block comment, one line | 1.87 ms | 5.09 ms | 1.85 ms |
  | 1 MB block comment, many lines | 1.69 ms | 2.39 ms | 1.68 ms |
  | 1 MB of JSDoc blocks | 1.77 ms | 2.56 ms | 1.76 ms |
  | 1 MB of plain block comments | 1.73 ms | 2.42 ms | 1.71 ms |
  | Astro config types (125 KB) | 238.1 µs | 320.0 µs | 236.6 µs |
  | AngularJS 1.2.5 | 3.06 ms | 3.39 ms | 3.02 ms |
  | YUI 3.12.0 | 1.72 ms | 1.77 ms | 1.72 ms |
  | `typescript.js` (8 MB) | 49.95 ms | 51.03 ms | 48.89 ms |

- For 650 inputs, the output matched the `69b31108` build. The inputs
are the Amaro benchmark corpus and synthetic inputs, each also with
TypeScript `module` declarations and comments inserted at several
positions.
- The `swc_next_simd` tests and Amaro's 99 tests pass with this change.
`cargo fmt`, `cargo clippy`, and the tests of the other crates were not
run locally. Native wall time was not measured.

## Related Links

- #1422
- #1426
- nodejs/amaro#507

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 and others added 2 commits November 4, 2026 11:10
`SourceMap::new_source_file` removes a leading BOM, but SWC Next spans
include it. With a BOM, every diagnostic was shifted by three bytes, and
a shifted span inside a multi-byte character made snippet rendering
panic, which traps in WebAssembly. A BOM before a hashbang also caused
an `InvalidSyntax` error. Remove the BOM before stripping, as
`swc_ts_fast_strip` did, and clamp diagnostic spans to character
boundaries.

Sort all diagnostics so that the first reported error is the first one
in the source, also for the errors that `validate` adds. Keep an
explicit error code with each diagnostic instead of matching messages,
and remove the unused `Unsupported transform option` match.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kdy1

kdy1 commented Nov 4, 2026

Copy link
Copy Markdown
Member Author

Verification results

I pushed 558cbb2 to this branch. It fixes a leading-BOM problem and the order of reported errors. This comment covers what I checked and what still needs a decision.

What I checked

  • Differential test against swc_ts_fast_strip 59.0.2 (the current implementation). I ran both implementations on the same inputs and compared the output code and the errors.
    • Real-world code: 40,356 units from vscode, Deno std, vite, hono, zod, trpc, and type-fest. There were 0 output differences and 0 inputs that the old implementation accepts and the new one rejects.
    • TypeScript v5.9.3 tests/cases: I ran each unit in the Unambiguous, Module, and Script modes.
      • The 63 inputs that the old implementation accepts and the new one rejects are, with one exception, invalid TypeScript. The exception is import type X = require("...") (see below).
      • The new implementation accepts 285 inputs that the old one rejects. For 128 of them, V8 accepts the output. All 128 outputs have the same semantics as the tsc emit (overloads, abstract, ambient declarations, modifiers). V8 rejects the other outputs.
      • The old implementation erases namespace\nn\n{}, which is runtime code. The new implementation keeps it, which is correct.
      • The two old panics (JSX namespaced names) do not occur with the new implementation.
  • Fast path for the deprecated module check: I fuzzed about 275k inputs, including 39,560 real module declarations and variants with LS/PS, NBSP, BOM, escapes, and comments. There were 0 inputs where the parser finds a module X {} declaration but the fast path skips the check.
  • cargo test, clippy, cargo fmt --check, the wasm build, npm test (99/99), typecheck, and biome all pass.

Open items

  1. import type X = require("...") is now rejected. This matches erasableSyntaxOnly in tsc 5.9. But the current amaro erases this statement, so this is a breaking change for Node users. We must decide if this is acceptable and, if yes, document it.
  2. The error messages are less specific. All unsupported syntax now gives SyntaxError: This syntax is not allowed when \erasableSyntaxOnly` is enabled. Previously, the message identified the construct, for example TypeScript enum is not supported in strip-only mode. With the error name, the result is UnsupportedSyntaxError: SyntaxError: ...`, which has a duplicated prefix. Node core tests that match the old messages will need updates.
  3. Upstream: SourceType::Script accepts import and export without a diagnostic. This PR adds its own check in validate as a workaround. When SWC Next fixes this, we can remove the workaround.
  4. Upstream: the parser does not report strict-mode early errors (for example, with statements and legacy octal literals in Module mode). V8 rejects this output when it loads it, so the runtime behavior does not change. Only the error source and message change.
  5. Escaped keyword: module Foo {} now gives an InvalidSyntax error instead of the deprecated module keyword error. This is a minor difference, but it is a behavior change.
  6. Parser options: only tsx from the parser option has an effect. Other fields, for example dts, are ignored. If callers can set these fields, we should document this or reject them.

kdy1 added a commit to swc-project/swc-next that referenced this pull request Nov 4, 2026
## Motivation

#1443 made the scalar backend's `lanes()` build its `[u8; 16]` result by
destructuring two words into 16 bytes. This removed a call from the
block comment scanner, but it made string scanning slower in Amaro's
`wasm32-unknown-unknown` build (`opt-level = "z"`, no `simd128`). In
Amaro `transformSync`, 1 MB of string declarations went from 3.57 ms to
4.45 ms. When I revert only the `lanes()` change, this time returns to
3.57 ms, and the block comment slowdown comes back. When I revert only
the `from_slice` change, nothing changes. A possible cause is that the
inlined string scanner now does byte-wise work where it had two 8-byte
stores before. I did not confirm this in the generated code.

## Changes

- `lanes()` converts both words with one `u128::to_le_bytes`. It has no
range indexing and no per-byte array. Behavior does not change. Only the
scalar backend uses `lanes()`.
- Amaro `transformSync`, wasm32 without `simd128`, measured locally.
Medians of 4 rounds:

  | Input | Before #1443 (`69b31108`) | `main` (`0334f9d7`) | This PR |
  | --- | ---: | ---: | ---: |
  | 1 MB of string declarations | 3.57 ms | 4.45 ms | 3.58 ms |
  | 1 MB block comment, one line | 5.09 ms | 1.86 ms | 1.85 ms |
  | 1 MB block comment, many lines | 2.40 ms | 1.66 ms | 1.67 ms |
  | 1 MB of JSDoc blocks | 2.55 ms | 1.77 ms | 1.77 ms |
  | Astro config types (125 KB) | 321.1 µs | 241.4 µs | 236.9 µs |
  | AngularJS 1.2.5 | 3.40 ms | 3.05 ms | 3.01 ms |
  | `typescript.js` (8 MB) | 51.50 ms | 49.80 ms | 49.05 ms |
  | cal.com TSX (1 MB) | 7.10 ms | 7.21 ms | 6.94 ms |

I also tried `u128` in `words()`. With that change, the string input
took 3.90 ms, so this PR does not include it.
- For 650 inputs, the output matched the `main` build. The inputs are
the Amaro benchmark corpus and synthetic inputs, each also with
TypeScript `module` declarations and comments inserted at several
positions.
- The `swc_next_simd` tests pass. `cargo fmt`, `cargo clippy`, the tests
of the other crates, and the other Amaro benchmark inputs were not run
for this change. Native wall time was not measured.

## Related Links

- #1443
- #1422
- nodejs/amaro#507

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 and others added 4 commits November 9, 2026 15:48
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SourceMap::new_source_file removes a BOM that follows the one the
adapter drains, which shifted every diagnostic by three bytes and
failed a debug assertion. Map offsets into the text that SourceMap
keeps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SWC Next's strip_types prefixes its own diagnostics with
"SyntaxError: ", while parser diagnostics have no prefix. Amaro's
JavaScript errors already carry the type, so users saw
"UnsupportedSyntaxError: SyntaxError: ...".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 and others added 2 commits November 16, 2026 14:46
SWC Next always parses decorators, so the adapter ignored
`parser.decorators: false` and returned decorators unchanged.
Report each decorator as InvalidSyntax when the option is off, as the
previous parser did. Make `Options::default()` match the serde
defaults, so Rust tests see the same parser settings as JavaScript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SWC Next rejects every `import =` declaration, including
`import type X = require(...)`, which swc_ts_fast_strip erased.
TypeScript's erasableSyntaxOnly rejects them too (TS1294). Document
the change and pin it with a test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kdy1

kdy1 commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Note

This text was drafted with AI and posted after human approval.

  • Harnesses: T3 Code, Claude Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

Decisions needed before merge

@marco-ippolito These items need your decision before this PR can merge. They apply to 550dafe.

This PR removes mode: "transform" and the amaro/transform loader, so it is a breaking change for Amaro and must be released as a new major version (2.0.0). The PR title uses feat!:, so release-please will bump the major version.

1. Rejection of type-only import = declarations

These inputs are now rejected with UnsupportedSyntax:

  • import type fs = require("node:fs");
  • export import type A = N.B;

Amaro 1.3.0 erases both statements. The new behavior matches erasableSyntaxOnly in tsc 5.9, and the README now documents it. This is a second breaking change for Node.js users. Do we accept it in the same major release?

2. Node.js core updates and release lines

  • The error messages change. For example, TypeScript enum is not supported in strip-only mode becomes This syntax is not allowed when `erasableSyntaxOnly` is enabled. These Node.js core files match the old messages and must be updated when Node.js updates to Amaro 2.0.0:

    • test/es-module/test-typescript.mjs
    • test/fixtures/eval/eval_typescript.snapshot
    • test/fixtures/eval/stdin_typescript.snapshot

    Who makes these updates, and in which Node.js PR?

  • On main, lib/internal/modules/typescript.js accepts only mode: 'strip'. On v22.x, v24.x, and v25.x, it still accepts ['strip', 'transform'], so these release lines must stay on Amaro 1.x. Do we agree to this?

3. New dependencies and license exception

  • The swc_next_* crates (0.6.1, pinned exactly): the SWC core team maintains both SWC and SWC Next, so I think these dependencies are acceptable.
  • deny.toml adds a license exception for foldhash 0.2.0 (Zlib). It comes in through hashbrown 0.17.1, which swc_next_allocator and indexmap (used by swc_next_ecma_parser) depend on. Is a Zlib exception acceptable for this project?

@kdy1 kdy1 changed the title feat!: use SWC Next for stripping and remove transform mode feat!: use SWC Next for type stripping and remove transform mode Nov 20, 2026
@marco-ippolito

Copy link
Copy Markdown
Member

This syntax is not allowed whenerasableSyntaxOnly is enabled. is confusing for node users given they might not have a tsconfig

kdy1 added a commit to swc-project/swc-next that referenced this pull request Nov 28, 2026
## Motivation

`strip_types` reports all non-erasable syntax with one message: ``This
syntax is not allowed when `erasableSyntaxOnly` is enabled``. Users who
run TypeScript without a tsconfig, for example through Node.js, do not
know this option. The message also does not tell which construct is the
problem. This feedback came from the Amaro integration:
nodejs/amaro#507 (comment).

Consumers also cannot tell these errors from parser errors without a
match on the message text. Amaro does this now to select its
`UnsupportedSyntax` error code.

## Changes

- Add `UnsupportedSyntaxKind` (`Enum`, `Namespace`, `ImportEquals`,
`ExportAssignment`, `ParameterProperty`, `TypeAssertion`).
`UnsupportedSyntaxKind::message` gives a message for each kind, for
example `SyntaxError: TypeScript enum declarations are not supported in
type stripping`.
- Add `StripResult::unsupported_syntax: Option<UnsupportedSyntax>`. It
holds the kind and the span of the reported diagnostic. The diagnostic
stays in `StripResult::diagnostics`.
- Add the help text ``Use `expr as T` instead.`` to the diagnostic for
angle-bracket type assertions.
- An error inside a namespace keeps its own kind. For example,
`namespace N { const enum E { A } }` reports `Enum`.

Breaking changes:

- `StripResult` has a new public field, so code that constructs or
destructures it exhaustively must change.
- The diagnostic messages for non-erasable syntax change.

Validation:

- `cargo test -p swc_next_type_strip`: passed (48 tests). A new test
checks the kind for each construct and that other errors give `None`.
The shared error assertion checks that `unsupported_syntax` matches the
diagnostic span and message.
- `cargo clippy -p swc_next_type_strip --all-targets --all-features --
-D warnings` and `cargo fmt`: passed.

## Related Links

- nodejs/amaro#507 (comment)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
kdy1 and others added 3 commits December 3, 2026 12:15
SWC Next 0.9.1 reports the kind of non-erasable syntax in
`StripResult::unsupported_syntax` (swc-project/swc-next#1702), and its
messages name the construct, for example "TypeScript enum declarations
are not supported in type stripping". They no longer mention
`erasableSyntaxOnly`, which Node.js users may not know.

Select the `UnsupportedSyntax` code from `unsupported_syntax` instead of
a match on the message text. `Diagnostic` has no lifetime parameter
now, so remove it from the adapter types.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SWC Next gives the help text "Use `expr as T` instead." with the
diagnostic for `<T>expr`, but Amaro reports only the message. Add the
help text of the non-erasable syntax diagnostic to the message, as the
deprecated `module` keyword error already does. Parser help text refers
to the snippet position, so it is not added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The README said that Amaro rejects every `import =` declaration. An
`import =` declaration inside an ambient declaration, for example
`declare module "pkg" { import type fs = require("node:fs"); }`, is
erased, and TypeScript's `erasableSyntaxOnly` accepts it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kdy1 kdy1 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agents: coordinator: Claude Opus 5.5 (claude-opus-5-5), reviewer: GPT-6.2 Astra (gpt-6.2-astra), reviewer: GPT-6.2 Sol (gpt-6.2-sol), reviewer: Claude Opus 5.5 (claude-opus-5-5), reviewer: Claude Sonnet 5.5 (claude-sonnet-5-5)

Verdict: Approve

This review approves the pull request. The P3 items are optional and do not block the merge.

@marco-ippolito This PR is ready to merge from my side. SWC Next 0.9.1 (swc-project/swc-next#1702) gives messages that name the unsupported construct, for example TypeScript enum declarations are not supported in type stripping, so the messages no longer mention erasableSyntaxOnly. If you prefer, I will squash the commits before the merge. The questions in #507 (comment) still need your decision. The notes below are non-blocking. The directive note and the nested type-only import note come from swc-next.

Reviewed commit: 9daedd0a82d0dc9784eb3dd4b3d7b32b3c230f3a

  • P3 (non-blocking): 3 (1 inline)

This review is posted as a comment because GitHub does not let the account kdy1 approve or request changes on its own pull request. The verdict above still applies.

Items that cannot be inline

  • P3 · Non-blocking · Type-only import and export declarations inside a function or block are erased without an error — the whole change

    function load() { import type { T } from './dep'; return 42 as T; } strips without an error. Amaro 1.3.0 reports InvalidSyntax ('import', and 'export' cannot be used outside of module code). The same applies to { import type ... }, if (a) import type ..., and export interface inside a function. The SWC Next parser does not check that these declarations are at the top level. The stripped output keeps the intended runtime behavior, and a value import that remains in the output is rejected by V8.

  • P3 · Non-blocking · The Dependabot swc group also matches the swc_next_* crates — .github/dependabot.yml:19

    The swc_* pattern of the swc group matches swc_next_allocator, swc_next_ecma_ast, swc_next_ecma_parser, and swc_next_type_strip. The comment above the group says that the group exists because SWC publishes all of its crates from one release. SWC Next has a separate release train, so one Dependabot PR will update both trains, and a failure in one blocks the other.

Comment thread crates/amaro/src/strip.rs Resolved
@kdy1

kdy1 commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Note

This text was drafted with AI and posted after human approval.

  • Harness: T3 Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

Addendum to my review: The note "Type-only import and export declarations inside a function or block are erased without an error" needs a qualification.

In SWC Next, the parser accepts import and export declarations in any statement position. The semantic analysis reports the position error (check_import_export_position in swc_ecma_semantic, "... may only appear at the top level"). strip_types runs only the parser, so it does not report this error by design. This is the same kind of early error as the strict-mode errors that the PR description lists as not reported.

When a value import remains in the output, V8 reports the error. A type-only declaration is erased, and the output keeps the intended runtime behavior. TypeScript and editors already report the error for this source.

So this note is not a defect in SWC Next, and no change is needed. With the correction in #507 (comment), neither of the two notes that the review attributed to swc-next needs a change.

@marco-ippolito

Copy link
Copy Markdown
Member

How does it compare with https://tangled.org/nibling.dev/typestrip

@kdy1

kdy1 commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Note

This text was drafted with AI and posted after human approval.

  • Harnesses: T3 Code, Claude Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

@marco-ippolito I compared this PR with typestrip. I ran the same inputs through three implementations:

  • This PR at 9daedd0a8 (SWC Next 0.9.1), with a local WebAssembly build
  • @nibling/typestrip 0.1.0 from npm, in its default erase mode. Its main branch has 3 more commits, for performance, source maps, and documentation.
  • Amaro 1.3.0, as the baseline

Machine: x86-64 Linux, Node.js 24.22.1.

Summary

  • Speed: The two are approximately equal. Both are approximately 2.6× faster than Amaro 1.3.0.
  • Size: typestrip is approximately 29× smaller (approximately 23 kB gzip, against 673 kB).
  • Output: On real-world code, the JavaScript output has the same meaning. I found 0 AST differences in 43,202 files that both accept.
  • Validation and errors: This PR is stricter and gives better errors. typestrip does not validate syntax. It has some gaps that Node.js users can see (details below).

Design

This PR (SWC Next) typestrip
Approach Full TypeScript parser and AST. Reports syntax errors. One forward pass with a token-level state machine and no AST. Passes the rest of the input through without checks.
Implementation Rust → wasm32 Zena → WasmGC. Requires WasmGC and SIMD.
Features Strip only Erase, downlevel (enum, namespace, parameter properties), JSX, streaming, source maps
Error data A message that names the construct, a code snippet, line and column, and UnsupportedSyntax or InvalidSyntax typestrip: TypeScript syntax that cannot be erased at offset N with start/end. A parse failure is could not parse input, with no location.
Project Built on swc-next One author. First commit on 2026-11-02. Version 0.1.0. It keeps workarounds for Zena compiler bugs.

Performance

Each value is a median.

Input This PR typestrip Amaro 1.3.0
typescript.js (9 MB) 57.7 ms 67 ms 158 ms
Largest erasable OSS .ts file (3.4 MB) 18.0 ms 19.1 ms 55 ms
lib.dom.d.ts 6.9 ms 5.4 ms 22.3 ms
43,312 OSS files (284 MB) 152 MB/s 146 MB/s 57 MB/s
Load / first call 9–12 ms / 2.4 ms 2.1 ms / 1.1 ms 18 ms / 3.6 ms
Package size, gzip 673 kB ~23 kB 1.13 MB
  • The typestrip README compares typestrip with Amaro 1.3.0. This PR gets a similar speedup over Amaro 1.3.0. The typestrip README numbers are from an Apple M1. My numbers are from x86-64.
  • The 673 kB of this PR includes the SWC crates that it keeps for the REPL helpers and for diagnostic formatting.

Differential test

Corpus Result
TypeScript 6.0.3 src/ (709 files) Both accept 612 files and reject 97. 0 AST differences. The only byte differences are the ; that this PR writes where it erases an overload signature.
The OSS corpus of typestrip (17 projects, 50,200 files) Both accept 43,317 files. Of these, 43,202 have the same AST (31,748 are byte-identical). The other 115 use decorators, which my AST checker (acorn) cannot parse. 9 files get different results (see below).
TypeScript 6.0.3 tests/cases (22,497 units) See the table below.

The table shows the cases where the results are different. "Real code" means that the case occurs in the OSS corpus.

Input This PR typestrip
A contextual keyword as a parameter name in a function type or method signature: type T = (unique: X) => string, interface I { f(readonly: boolean): void } (real code: prisma, vscode) Strips it Rejects valid TypeScript with could not parse input. This is a known issue in typestrip.
constructor(override x) (real code: vscode) UnsupportedSyntax. tsc treats this as a parameter property and emits this.x = x. No error. It keeps override in the output, so V8 throws SyntaxError: Unexpected identifier 'x', which does not tell the user the cause.
'use strict' as any; at the start of a body Rejects it with InvalidSyntax Erases the suffix. The string becomes a directive, so the code changes from sloppy mode to strict mode without an error. Amaro 1.3.0 does the same.
declare function f(): void; followed by "use strict" (2 TypeScript tests) Writes ; at the erased declaration, so the string is not a directive The string becomes a directive. This matches the tsc emit and Amaro 1.3.0.
Invalid TypeScript (836 TypeScript test units) InvalidSyntax with a location Accepts the input. 677 outputs are not valid JavaScript, so V8 reports the error later. 159 outputs parse as JavaScript, so the error is not reported (for example, duplicate modifiers or async interface).
export as namespace X (24 units, almost all in .d.ts files) Erases it Keeps it, and the output is not valid JavaScript. Amaro 1.3.0 does the same.
A non-exported import A = B alias in a namespace (5 units) Rejects it, like erasableSyntaxOnly Erases it, because tsc does not treat the namespace as instantiated

The "use strict" row is the only case where this PR does not match the tsc emit and Amaro 1.3.0. We must decide if that behavior is correct. In the other rows, this PR is stricter, or it reports a clearer error.

Conclusion for Node.js

  • typestrip is much smaller. Part of the size of this PR comes from the SWC crates that Amaro keeps for the REPL helpers and diagnostics. I did not measure how much.
  • On this machine, the two have approximately the same throughput.
  • This PR validates the input and gives the location and snippet data that Node.js uses for ERR_INVALID_TYPESCRIPT_SYNTAX and ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX. typestrip passes invalid input through by design and reports only an offset.
  • To use typestrip in Node.js, we would need the Zena toolchain in the build. The project is one month old and has one author.

@kdy1

kdy1 commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

I'll post the Wasm size without the SWC crates, too

@kdy1

kdy1 commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Note

This text was drafted with AI and posted after human approval.

  • Harnesses: T3 Code, Claude Code
  • AI agent: Claude Opus 5.5 (claude-opus-5-5)

Follow-up to my comparison with typestrip: I measured how much of the package size comes from the SWC crates that this PR keeps besides SWC Next.

This PR keeps two groups of published SWC crates:

  • Diagnostics: swc_common (SourceMap, Handler), swc_error_reporters, miette, and owo-colors make the error snippet, line, and column.
  • REPL helpers: swc_ecma_parser, swc_ecma_ast, swc_ecma_visit, and swc_common implement transformModuleSyntax, getFirstExpression, isValidSyntax, isRecoverableError, tokenize, and findTopLevelAwaits.

I built three experimental variants of crates/amaro at 9daedd0a8 with the same release profile and wasm-bindgen 0.2.100:

  • No diagnostic crates: The strip path uses only SWC Next. A small replacement module (about 95 lines, with unicode-width) computes the line, the column, and the snippet from the SWC Next diagnostic offsets. The REPL helpers do not change, so swc_common stays because they use it.
  • No REPL helpers: I removed the REPL helper exports. The diagnostics do not change.
  • Both removed: Only the SWC Next crates remain.
Variant .wasm raw .wasm gzip wasm.js (Base64-embedded) raw wasm.js gzip Change in wasm.js gzip
This PR 1,316 kB 479 kB 1,775 kB 673 kB —
No diagnostic crates 1,216 kB 438 kB 1,641 kB 618 kB −8%
No REPL helpers 630 kB 241 kB 856 kB 328 kB −51%
Both removed (SWC Next only) 510 kB 193 kB 695 kB 264 kB −61%
typestrip 0.1.0 (for reference) 35 kB 14 kB — — —

Results

  • The diagnostic crates are a small part. Removing them saves approximately 55 kB gzip.
  • The REPL helpers are the largest part. They still use the old swc_ecma_parser, so the package contains two TypeScript parsers. Removing them saves approximately 345 kB gzip.
  • With SWC Next only, the package is approximately 264 kB gzip. The wasm file with only SWC Next and the Amaro adapter is approximately 193 kB gzip. typestrip is still approximately 14× smaller when we compare the gzipped wasm files.
  • To get this size and keep the REPL helpers, we must port the REPL helpers to the SWC Next parser. I did not do this port. The port adds code, so the real size will be larger than the "SWC Next only" row. I did not measure how much.

Validation of the replacement diagnostics

I ran this PR and the "both removed" variant on 54,909 inputs (TypeScript 6.0.3 src/, the typestrip OSS corpus, and 4,000 units from TypeScript tests/cases):

  • The stripped code, the error code, and the error message are the same for all inputs.
  • startLine, startColumn, endLine, and endColumn are the same for all inputs except 5.
  • The snippet text is the same for 7,033 of the 7,888 errors (89%). In 835 errors, the only difference is where a \r stays. The other 20 errors have differences in tab alignment or at the end of the file. I did not try to match these cases.

These variants are experiments to measure size. They are not part of this PR.

@marco-ippolito

Copy link
Copy Markdown
Member

Lets do it

@marco-ippolito
marco-ippolito merged commit b0df777 into nodejs:main Dec 3, 2026
11 of 12 checks passed
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.

2 participants