Skip to content

Commit 0adad25

Browse files
authored
feat(typescript): Align isolatedDeclaration implementation with tsc (#9715)
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required --> <!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. --> **Description:** - [x] **Basic implementation.** This also includes diagnostics and type inference. - [x] Function - [x] Class - [x] Variable declaration - [x] Enum - [x] **Remove unused imports and declarations.** Based on the result ast of fast dts transformation, we collect the usage of ids which also contain syntax context. Then we prune the ast to cut off the unused imports and declarations. - [x] **Port tests and bug fixes.** To ensure correctness, I'd like to port the fixtures as oxc does https://github.com/oxc-project/oxc/tree/main/crates/oxc_isolated_declarations/tests/fixtures. - [x] **Benmarks** - [x] Strip internal **Implementation:** Most code is direct translation of https://github.com/oxc-project/oxc/tree/main/crates/oxc_isolated_declarations. The differences come from: 1. swc and oxc use **different ast**. 2. The transformation of swc is **mutation-based**, which directly mutates the whole cloned ast, while the transformation of oxc is **immutation-based**, which constructs and copies the ast nodes on demand. Maybe mutation-based transformation is not better, but I think it's also annoyed to construct ast nodes and it could be easy to refactor if there is some demands in the future. 3. oxc transforms while collecting references information to prune unused idents. However, the ast nodes of swc contain **syntax context**, we don't need to collect the information again. So I build a reference graph and calculate the reachability of identifiers. 4. I think oxc could have bugs. I manually compare the result with `tsc --declaration --isolatedDeclarations --emitDeclarationOnly --isolatedModules --noResolve --noCheck --strictNullChecks test.ts`. I also reguard those cases where tsc can't compile as undefined behaviors. 😅 Actually I find my implementation of point 2 is not too good either. I meet many bad cases after my basic implementation and write many ugly patches. So issues are welcome. **Benchmarks:** Roughly test with https://raw.githubusercontent.com/oxc-project/benchmark-files/main/vue-id.ts under M3Pro. | | parse without syntax context resolution | isolated declarations | | -------------- | ------ | --------------------------------- | | swc | 282.68ms | 335.44ms | | oxc | 53.286ms | 63.917ms | | tsc | - | 3.875s | **Known problem:** - Due to the comment emitting algorithm in swc, isolated declaration may also generate comments with wrong positions. **Related issue:** - Closes #9705 - Closes #9718
1 parent bd77f44 commit 0adad25

File tree

150 files changed

+507614
-1689
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+507614
-1689
lines changed

.changeset/tiny-snakes-train.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_typescript: major
3+
---
4+
5+
feat(typescript): Align `isolatedDeclaration` implementation with tsc

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,8 @@ name = "oxc"
166166
[[bench]]
167167
harness = false
168168
name = "typescript"
169+
170+
171+
[[bench]]
172+
harness = false
173+
name = "isolated_declarations"

0 commit comments

Comments
 (0)