Skip to content

Commit

Permalink
feat(typescript): Align isolatedDeclaration implementation with tsc (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
CPunisher authored Dec 2, 2024
1 parent bd77f44 commit 0adad25
Show file tree
Hide file tree
Showing 150 changed files with 507,614 additions and 1,689 deletions.
5 changes: 5 additions & 0 deletions .changeset/tiny-snakes-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_typescript: major
---

feat(typescript): Align `isolatedDeclaration` implementation with tsc
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions crates/swc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,8 @@ name = "oxc"
[[bench]]
harness = false
name = "typescript"


[[bench]]
harness = false
name = "isolated_declarations"
Loading

0 comments on commit 0adad25

Please sign in to comment.