|
| 1 | +--- |
| 2 | +title: Oxlint Type-Aware Preview |
| 3 | +outline: deep |
| 4 | +authors: |
| 5 | + - boshen |
| 6 | + - camchenry |
| 7 | + - auvred |
| 8 | +--- |
| 9 | + |
| 10 | +<AppBlogPostHeader /> |
| 11 | + |
| 12 | +<br> |
| 13 | + |
| 14 | +We're thrilled to announce type-aware rules support in Oxlint! |
| 15 | + |
| 16 | +This preview release aims to engage with the community for collaboration and |
| 17 | +discussion by documenting our decision process and technical details. |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +If oxlint isn't already installed, please visit our [usage guide](https://oxc.rs/docs/guide/usage/linter.html). |
| 22 | + |
| 23 | +To get started, run: |
| 24 | + |
| 25 | +```bash |
| 26 | +pnpm add -D oxlint-tsgolint |
| 27 | +oxlint --tsconfig tsconfig.json |
| 28 | +``` |
| 29 | + |
| 30 | +You'll then see two of our most requested type-aware rules in action: |
| 31 | + |
| 32 | +- no-floating-promises |
| 33 | +- no-misused-promises |
| 34 | + |
| 35 | +The next version will enable all type-aware rules. |
| 36 | + |
| 37 | +## Performance |
| 38 | + |
| 39 | +Our testing shows that large repositories, which previously took several minutes to run with `typescript-eslint`, |
| 40 | +now complete in less than 10 seconds. |
| 41 | + |
| 42 | +We achieved this by leveraging [`typescript-go`](https://github.com/microsoft/typescript-go), |
| 43 | +the [10x faster TypeScript](https://devblogs.microsoft.com/typescript/typescript-native-port) written in Go. |
| 44 | + |
| 45 | +## Type-Aware linting |
| 46 | + |
| 47 | +Please refer to |
| 48 | +[Rust-Based JavaScript Linters: Fast, But No Typed Linting Right Now](https://www.joshuakgoldberg.com/blog/rust-based-javascript-linters-fast-but-no-typed-linting-right-now) |
| 49 | +to understand the current status of type-aware linting in the ecosystem. |
| 50 | + |
| 51 | +## Technical Details |
| 52 | + |
| 53 | +The core of this new functionality is [oxc-project/tsgolint](https://github.com/oxc-project/tsgolint). |
| 54 | + |
| 55 | +The tsgolint project was initially prototyped as [typescript-eslint/tsgolint](https://github.com/typescript-eslint/tsgolint). |
| 56 | +However, the `typescript-eslint` team decided not to allocate development resources to this prototype, as they plan to continue their work on `typescript-eslint` for typed linting with ESLint. |
| 57 | + |
| 58 | +@boshen then reached out to @auvred for a forked, scoped-down version adapted for oxlint. |
| 59 | +This version would only contain type-aware rules without the sophisticated configuration resolution a full linter would require. |
| 60 | + |
| 61 | +@auvred generously offered to continue its development under the Oxc organization. |
| 62 | + |
| 63 | +### Architecture |
| 64 | + |
| 65 | +`oxlint` (written in Rust) and `tsgolint` (written in Go) are compiled into their own binaries. |
| 66 | + |
| 67 | +`oxlint` serves as the "frontend" for `tsgolint`, handling the CLI, path traversal, ignore logic, and diagnostic printing. |
| 68 | + |
| 69 | +`tsgolint` acts as the backend for `oxlint`, accepting paths and configuration as input and outputting structured diagnostics. |
| 70 | + |
| 71 | +This creates a simple pipeline: |
| 72 | + |
| 73 | +``` |
| 74 | +Oxlint CLI (returns paths + rules + configuration) |
| 75 | + -> tsgolint (returns diagnostics) |
| 76 | + -> Oxlint CLI |
| 77 | +``` |
| 78 | + |
| 79 | +### `tsgolint` |
| 80 | + |
| 81 | +`tsgolint` does not communicate with typescript-go via public APIs. |
| 82 | + |
| 83 | +Instead, it compiles `typescript-go` by [shimming its internal APIs to make them public](https://github.com/oxc-project/tsgolint/tree/main/shim). |
| 84 | + |
| 85 | +All type-aware rules are written directly against these shimmed APIs. |
| 86 | + |
| 87 | +While this isn't the recommended approach, it works! |
| 88 | + |
| 89 | +## Decision Process |
| 90 | + |
| 91 | +### Write our own type checker |
| 92 | + |
| 93 | +Previous abandoned attempts to implement a type checker included: |
| 94 | + |
| 95 | +- My own attempt at [writing type inference](https://gist.github.com/Boshen/d189de0fe0720a30c5182cb666e3e9a5) |
| 96 | +- [Integrate](https://github.com/oxc-project/oxc/pull/413) [ezno type checker](https://github.com/kaleidawave/ezno) by @kaleidawave |
| 97 | +- [stc](https://github.com/dudykr/stc) by @kdy1 |
| 98 | +- (There are also many attempts in the community that did not go far). |
| 99 | + |
| 100 | +Additionally, there's the work-in-progress: |
| 101 | + |
| 102 | +- [Biome 2.0](https://biomejs.dev/blog/biome-v2/) with its own type inference implementation. |
| 103 | + |
| 104 | +We determined that writing our own type inferencer or type checker was not feasible due to budget and time constraints, |
| 105 | +implementation details, and the challenge of keeping up with a fast-moving target like TypeScript. |
| 106 | + |
| 107 | +### Communication with TypeScript |
| 108 | + |
| 109 | +Prior to `typescript-go`, projects added plugin interfaces to TypeScript's public API by either mapping its AST to `estree` or directly traversing the TypeScript AST. Examples include: |
| 110 | + |
| 111 | +- [typescript-eslint](https://typescript-eslint.io/getting-started/typed-linting) |
| 112 | +- [tsslint](https://github.com/johnsoncodehk/tsslint) |
| 113 | +- [tsl](https://github.com/ArnaudBarre/tsl) |
| 114 | + |
| 115 | +We also explored [inter-process communication with Oxlint](https://github.com/oxc-project/oxc/discussions/2855) but abandoned the idea. |
| 116 | + |
| 117 | +With `typescript-go`, the TypeScript team is [leaning towards](https://github.com/microsoft/typescript-go/discussions/455) |
| 118 | +encoding the TypeScript AST and decoding it on the JavaScript side through inter-process communication. |
| 119 | + |
| 120 | +While these approaches work, they still incur: |
| 121 | + |
| 122 | +- Performance issues of varying degrees that don't suit Oxlint's performance characteristics. |
| 123 | +- The cost of maintaining an AST mapping from TypeScript's AST. |
| 124 | + |
| 125 | +## Considerations |
| 126 | + |
| 127 | +While `tsgolint` solves the performance issue, there are other technical challenges that need to be addressed. |
| 128 | + |
| 129 | +### Requirement for a Different TypeScript Version |
| 130 | + |
| 131 | +We plan to release snapshots of `typescript-go` versions and align their version numbers with TypeScript. |
| 132 | +You will then be able to install `oxlint-typescript` with the correct TypeScript version. |
| 133 | + |
| 134 | +The downside of this approach is that you may need to upgrade TypeScript if `oxlint-tsgolint` requires changes. |
| 135 | + |
| 136 | +### Maintenance cost of `tsgolint` |
| 137 | + |
| 138 | +Shimming TypeScript's internal APIs carries some risk. However, the TypeScript AST and its visitor are actually quite stable. |
| 139 | + |
| 140 | +We accept this risk and will fix breaking changes when upgrading `typescript-go`. |
| 141 | + |
| 142 | +Our `typescript-go` version is synced three times a week. |
| 143 | + |
| 144 | +## Acknowledgements |
| 145 | + |
| 146 | +We'd like to extend our gratitude to: |
| 147 | + |
| 148 | +- The TypeScript team for creating `typescript-go`. |
| 149 | +- [@auvred](https://github.com/auvred) for creaging `tsgolint`. |
| 150 | +- [@camchenry](https://github.com/camchenry) for the `oxlint` + `tsgolint` integration. |
| 151 | +- The `typescript-eslint` team for their heartwarming support. |
| 152 | + |
| 153 | +## Join the Community |
| 154 | + |
| 155 | +We'd love to hear your feedback on Oxlint and type-aware linting and are excited to see how it helps improve your development workflow. |
| 156 | +Connect with us: |
| 157 | + |
| 158 | +- **Discord**: Join our [community server](https://discord.gg/9uXCAwqQZW) for real-time discussions |
| 159 | +- **GitHub**: Share feedback on [GitHub Discussions](https://github.com/oxc-project/oxc/discussions) |
| 160 | +- **Issues**: Report bugs or request features on our [issue tracker](https://github.com/oxc-project/oxc/issues) |
| 161 | + |
| 162 | +## Give It a Try |
| 163 | + |
| 164 | +To get started, follow the [installation guide](https://oxc.rs/docs/guide/usage/linter), or learn more about the [Oxc project](https://oxc.rs/docs/guide/introduction). |
0 commit comments