@willbooster/tree-sitter-typescript 0.0.0-placeholder.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +121 -1
- package/common/defineGrammar.js +1022 -0
- package/package.json +68 -2
- package/tree-sitter-tsx.wasm +0 -0
- package/tree-sitter-typescript.wasm +0 -0
- package/tree-sitter.json +102 -0
- package/tsx/grammar.js +4 -0
- package/tsx/src/node-types.json +6290 -0
- package/typescript/grammar.js +4 -0
- package/typescript/src/node-types.json +6040 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Max Brunsfeld
|
|
4
|
+
Copyright (c) 2026 WillBooster Inc.
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,123 @@
|
|
|
1
1
|
# @willbooster/tree-sitter-typescript
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-typescript)
|
|
4
|
+
[](https://www.npmjs.com/package/@willbooster/tree-sitter-typescript)
|
|
5
|
+
[](https://github.com/WillBooster/tree-sitter-typescript/actions/workflows/test.yml)
|
|
6
|
+
[](https://github.com/WillBooster/tree-sitter-typescript/actions/workflows/test-rust.yml)
|
|
7
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
8
|
+
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
9
|
+
[](https://crates.io/crates/willbooster-tree-sitter-typescript)
|
|
10
|
+
|
|
11
|
+
TypeScript and TSX grammars for [tree-sitter](https://github.com/tree-sitter/tree-sitter), forked from
|
|
12
|
+
[tree-sitter/tree-sitter-typescript](https://github.com/tree-sitter/tree-sitter-typescript). We are grateful
|
|
13
|
+
to its authors and contributors. This is not an official release of that project.
|
|
14
|
+
|
|
15
|
+
This fork fixes parsing bugs and raises conformance with [TypeScript](https://www.typescriptlang.org/docs/handbook/intro.html)
|
|
16
|
+
and, for TSX, [JSX](https://facebook.github.io/jsx/).
|
|
17
|
+
|
|
18
|
+
TypeScript and TSX are two different dialects (`<T>x` is a type assertion in TypeScript but a JSX element in TSX),
|
|
19
|
+
so this package defines two grammars: `typescript` in `typescript/` and `tsx` in `tsx/`, both generated from
|
|
20
|
+
`common/defineGrammar.js`. The `tsx` grammar also parses JavaScript with [Flow](https://flow.org/) type annotations.
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
The npm package ships `tree-sitter-typescript.wasm` and `tree-sitter-tsx.wasm` for
|
|
25
|
+
[@willbooster/web-tree-sitter](https://www.npmjs.com/package/@willbooster/web-tree-sitter), which runs in Node.js,
|
|
26
|
+
Bun, browsers, and Cloudflare Workers. Load `tree-sitter-typescript.wasm` for `.ts` files and `tree-sitter-tsx.wasm`
|
|
27
|
+
for `.tsx` files.
|
|
28
|
+
|
|
29
|
+
In Node.js and Bun:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import { fileURLToPath } from 'node:url';
|
|
33
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
34
|
+
|
|
35
|
+
await Parser.init();
|
|
36
|
+
const parser = new Parser();
|
|
37
|
+
const wasmPath = fileURLToPath(import.meta.resolve('@willbooster/tree-sitter-typescript/tree-sitter-typescript.wasm'));
|
|
38
|
+
parser.setLanguage(await Language.load(wasmPath));
|
|
39
|
+
const tree = parser.parse('const x: number = 1;\n');
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
In browsers, load the `.wasm` files by URL. With Vite:
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
46
|
+
import runtimeUrl from '@willbooster/web-tree-sitter/web-tree-sitter.wasm?url';
|
|
47
|
+
import tsxUrl from '@willbooster/tree-sitter-typescript/tree-sitter-tsx.wasm?url';
|
|
48
|
+
|
|
49
|
+
await Parser.init({ locateFile: () => runtimeUrl });
|
|
50
|
+
const parser = new Parser();
|
|
51
|
+
parser.setLanguage(await Language.load(tsxUrl));
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
In Cloudflare Workers, which do not allow compiling Wasm at run time, import the `.wasm` files as modules (with or
|
|
55
|
+
without Node.js compatibility):
|
|
56
|
+
|
|
57
|
+
```js
|
|
58
|
+
import { Language, Parser } from '@willbooster/web-tree-sitter';
|
|
59
|
+
import runtime from '@willbooster/web-tree-sitter/web-tree-sitter.wasm';
|
|
60
|
+
import typescript from '@willbooster/tree-sitter-typescript/tree-sitter-typescript.wasm';
|
|
61
|
+
|
|
62
|
+
await Parser.init({ wasmModule: runtime });
|
|
63
|
+
const parser = new Parser();
|
|
64
|
+
parser.setLanguage(await Language.load(typescript));
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The package also ships the node types in `typescript/src/node-types.json` and `tsx/src/node-types.json`.
|
|
68
|
+
|
|
69
|
+
In Rust, depend on the [crate](https://crates.io/crates/willbooster-tree-sitter-typescript) and on
|
|
70
|
+
[willbooster-tree-sitter](https://crates.io/crates/willbooster-tree-sitter), the runtime this package is tested and
|
|
71
|
+
fuzzed with (the grammar also loads in the upstream `tree-sitter` crate 0.27, whose error recovery never ends on some
|
|
72
|
+
malformed input):
|
|
73
|
+
|
|
74
|
+
```toml
|
|
75
|
+
[dependencies]
|
|
76
|
+
tree-sitter = { package = "willbooster-tree-sitter", version = "1" }
|
|
77
|
+
tree-sitter-typescript = { package = "willbooster-tree-sitter-typescript", version = "1" }
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```rust
|
|
81
|
+
let mut parser = tree_sitter::Parser::new();
|
|
82
|
+
parser.set_language(&tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())?;
|
|
83
|
+
// or `tree_sitter_typescript::LANGUAGE_TSX` for TSX
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Development
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
mise install
|
|
90
|
+
bun install --frozen-lockfile
|
|
91
|
+
bun run test/ci-setup
|
|
92
|
+
bun run build/ci
|
|
93
|
+
bun run test
|
|
94
|
+
script/parse-examples
|
|
95
|
+
cargo test
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`bun run test/ci-setup` installs Chromium for the browser tests. `bun run build/ci` regenerates `typescript/src/` and
|
|
99
|
+
`tsx/src/` and builds both Wasm files. `bun run test` runs:
|
|
100
|
+
|
|
101
|
+
- the corpus in `test/corpus`, with the native build and with the Wasm build (the first run downloads the WASI SDK).
|
|
102
|
+
Cases run with the `typescript` grammar unless they carry `:language(tsx)`;
|
|
103
|
+
- an incremental-parsing check (`test/unit/incremental.test.ts`): `tree-sitter fuzz` edits each corpus case of the
|
|
104
|
+
`typescript` grammar at random, reparses it, undoes the edits, and reparses again. `TREE_SITTER_SEED`,
|
|
105
|
+
`TREE_SITTER_ITERATIONS`, and `TREE_SITTER_EDITS` run other or more edits;
|
|
106
|
+
- a check that the real-world files in `examples/`, the checked-in ones and those of the cloned repositories, fail to
|
|
107
|
+
parse exactly as listed in `script/known-failures.txt`. `.ts` files are parsed with the `typescript` grammar and
|
|
108
|
+
`.tsx` files with the `tsx` grammar. The first run clones the repositories. The example repositories are pinned to
|
|
109
|
+
commits in `script/parse-examples`. After a grammar change or a moved pin alters that list, `script/parse-examples`
|
|
110
|
+
rewrites it; review its diff before committing;
|
|
111
|
+
- a performance check (`test/unit/performance.test.ts`) that recovering from an error on each of 10,000 lines takes
|
|
112
|
+
linear time with each grammar, since consumers parse files while they are being edited. It loads the Wasm builds
|
|
113
|
+
through @willbooster/web-tree-sitter, which `bun run build/ci` rebuilds after regenerating the parsers;
|
|
114
|
+
- checks that both Wasm builds load and parse through @willbooster/web-tree-sitter in Chromium
|
|
115
|
+
(`test/unit/browser/`) and in Cloudflare Workers with and without Node.js compatibility
|
|
116
|
+
(`test/unit/workers.test.ts`, running the Worker in `test/fixtures/worker/`).
|
|
117
|
+
|
|
118
|
+
CI also runs these tests on Linux arm64 and macOS, where the Rust binding compiles the parsers natively, and fuzzes
|
|
119
|
+
both parsers with libFuzzer and sanitizers (`.github/workflows/robustness.yml`).
|
|
120
|
+
|
|
121
|
+
### References
|
|
122
|
+
|
|
123
|
+
- [TypeScript Language Spec](https://github.com/microsoft/TypeScript/blob/30cb20434a6b117e007a4959b2a7c16489f86069/doc/spec-ARCHIVED.md)
|