Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information