tree-sitter-ts 0.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 +21 -0
- package/README.md +246 -0
- package/dist/index.cjs +5529 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +735 -0
- package/dist/index.d.ts +735 -0
- package/dist/index.js +5464 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hieutran512
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
# tree-sitter-ts
|
|
2
|
+
|
|
3
|
+
Pure TypeScript parser library with declarative language profiles.
|
|
4
|
+
|
|
5
|
+
- No WASM runtime
|
|
6
|
+
- Works in Node.js and modern bundlers
|
|
7
|
+
- Supports tokenization and symbol extraction
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install tree-sitter-ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
> Requires Node.js 18+
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { tokenize, extractSymbols } from "tree-sitter-ts";
|
|
21
|
+
|
|
22
|
+
const source = `
|
|
23
|
+
export class Service {
|
|
24
|
+
run(): void {}
|
|
25
|
+
}
|
|
26
|
+
`;
|
|
27
|
+
|
|
28
|
+
const tokens = tokenize(source, "typescript");
|
|
29
|
+
const symbols = extractSymbols(source, "typescript");
|
|
30
|
+
|
|
31
|
+
console.log(tokens[0]);
|
|
32
|
+
// {
|
|
33
|
+
// type: "keyword",
|
|
34
|
+
// value: "export",
|
|
35
|
+
// category: "keyword",
|
|
36
|
+
// range: { start: { line: 2, column: 1, offset: 1 }, end: ... }
|
|
37
|
+
// }
|
|
38
|
+
|
|
39
|
+
console.log(symbols);
|
|
40
|
+
// [{ name: "Service", kind: "class", startLine: 2, endLine: 4 }]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
You can resolve language by:
|
|
44
|
+
|
|
45
|
+
- Profile name (for example, `"typescript"`)
|
|
46
|
+
- File extension (for example, `".ts"`)
|
|
47
|
+
|
|
48
|
+
## API
|
|
49
|
+
|
|
50
|
+
### Core functions
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import {
|
|
54
|
+
tokenize,
|
|
55
|
+
extractSymbols,
|
|
56
|
+
tokenizeWithProfile,
|
|
57
|
+
extractSymbolsWithProfile,
|
|
58
|
+
} from "tree-sitter-ts";
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- `tokenize(source, language): Token[]`
|
|
62
|
+
- Converts source text to a token stream using a registered profile.
|
|
63
|
+
- `extractSymbols(source, language): CodeSymbol[]`
|
|
64
|
+
- Extracts symbols like functions/classes (depending on profile structure rules).
|
|
65
|
+
- `tokenizeWithProfile(source, profile): Token[]`
|
|
66
|
+
- Tokenizes directly with a `LanguageProfile` object.
|
|
67
|
+
- `extractSymbolsWithProfile(source, profile): CodeSymbol[]`
|
|
68
|
+
- Extracts symbols directly with a `LanguageProfile` object.
|
|
69
|
+
|
|
70
|
+
### Registry utilities
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import {
|
|
74
|
+
registerProfile,
|
|
75
|
+
getProfile,
|
|
76
|
+
getRegisteredLanguages,
|
|
77
|
+
getSupportedExtensions,
|
|
78
|
+
builtinProfiles,
|
|
79
|
+
} from "tree-sitter-ts";
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- `registerProfile(profile)`
|
|
83
|
+
- Registers a custom language profile at runtime.
|
|
84
|
+
- `getProfile(nameOrExt)`
|
|
85
|
+
- Gets a profile by language name or file extension.
|
|
86
|
+
- `getRegisteredLanguages()`
|
|
87
|
+
- Lists currently registered profile names.
|
|
88
|
+
- `getSupportedExtensions()`
|
|
89
|
+
- Lists registered file extensions.
|
|
90
|
+
- `builtinProfiles`
|
|
91
|
+
- Array of all built-in profiles.
|
|
92
|
+
|
|
93
|
+
## Output types
|
|
94
|
+
|
|
95
|
+
### Token
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
interface Token {
|
|
99
|
+
type: string;
|
|
100
|
+
value: string;
|
|
101
|
+
category: TokenCategory;
|
|
102
|
+
range: Range;
|
|
103
|
+
}
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### CodeSymbol
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
interface CodeSymbol {
|
|
110
|
+
name: string;
|
|
111
|
+
kind: SymbolKind;
|
|
112
|
+
startLine: number;
|
|
113
|
+
endLine: number;
|
|
114
|
+
path?: string[];
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Built-in languages
|
|
119
|
+
|
|
120
|
+
Current built-in profiles:
|
|
121
|
+
|
|
122
|
+
- `json`
|
|
123
|
+
- `css`
|
|
124
|
+
- `scss`
|
|
125
|
+
- `python`
|
|
126
|
+
- `go`
|
|
127
|
+
- `javascript`
|
|
128
|
+
- `typescript`
|
|
129
|
+
- `cpp`
|
|
130
|
+
- `html`
|
|
131
|
+
- `markdown`
|
|
132
|
+
- `yaml`
|
|
133
|
+
- `xml`
|
|
134
|
+
- `java`
|
|
135
|
+
- `csharp`
|
|
136
|
+
- `rust`
|
|
137
|
+
- `ruby`
|
|
138
|
+
- `php`
|
|
139
|
+
- `kotlin`
|
|
140
|
+
- `swift`
|
|
141
|
+
- `shell`
|
|
142
|
+
- `sql`
|
|
143
|
+
- `toml`
|
|
144
|
+
|
|
145
|
+
To inspect at runtime:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
import { getRegisteredLanguages, getSupportedExtensions } from "tree-sitter-ts";
|
|
149
|
+
|
|
150
|
+
console.log(getRegisteredLanguages());
|
|
151
|
+
console.log(getSupportedExtensions());
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Custom language profile example
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import {
|
|
158
|
+
registerProfile,
|
|
159
|
+
tokenize,
|
|
160
|
+
extractSymbols,
|
|
161
|
+
type LanguageProfile,
|
|
162
|
+
} from "tree-sitter-ts";
|
|
163
|
+
|
|
164
|
+
const toyProfile: LanguageProfile = {
|
|
165
|
+
name: "toytest",
|
|
166
|
+
displayName: "Toy Test",
|
|
167
|
+
version: "1.0.0",
|
|
168
|
+
fileExtensions: [".toy"],
|
|
169
|
+
lexer: {
|
|
170
|
+
charClasses: {
|
|
171
|
+
identStart: { union: [{ predefined: "letter" }, { chars: "_" }] },
|
|
172
|
+
identPart: { union: [{ predefined: "alphanumeric" }, { chars: "_" }] },
|
|
173
|
+
},
|
|
174
|
+
tokenTypes: {
|
|
175
|
+
keyword: { category: "keyword" },
|
|
176
|
+
identifier: { category: "identifier" },
|
|
177
|
+
punctuation: { category: "punctuation" },
|
|
178
|
+
whitespace: { category: "whitespace" },
|
|
179
|
+
newline: { category: "newline" },
|
|
180
|
+
},
|
|
181
|
+
initialState: "default",
|
|
182
|
+
skipTokens: ["whitespace", "newline"],
|
|
183
|
+
states: {
|
|
184
|
+
default: {
|
|
185
|
+
rules: [
|
|
186
|
+
{ match: { kind: "keywords", words: ["fn"] }, token: "keyword" },
|
|
187
|
+
{
|
|
188
|
+
match: {
|
|
189
|
+
kind: "charSequence",
|
|
190
|
+
first: { ref: "identStart" },
|
|
191
|
+
rest: { ref: "identPart" },
|
|
192
|
+
},
|
|
193
|
+
token: "identifier",
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
match: { kind: "string", value: ["{", "}", "(", ")", ",", ";"] },
|
|
197
|
+
token: "punctuation",
|
|
198
|
+
},
|
|
199
|
+
],
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
structure: {
|
|
204
|
+
blocks: [{ name: "braces", open: "{", close: "}" }],
|
|
205
|
+
symbols: [
|
|
206
|
+
{
|
|
207
|
+
name: "function_declaration",
|
|
208
|
+
kind: "function",
|
|
209
|
+
pattern: [
|
|
210
|
+
{ token: "keyword", value: "fn" },
|
|
211
|
+
{ token: "identifier", capture: "name" },
|
|
212
|
+
],
|
|
213
|
+
hasBody: true,
|
|
214
|
+
bodyStyle: "braces",
|
|
215
|
+
},
|
|
216
|
+
],
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
registerProfile(toyProfile);
|
|
221
|
+
|
|
222
|
+
const source = "fn add(a, b) {\n}\n";
|
|
223
|
+
console.log(tokenize(source, "toytest"));
|
|
224
|
+
console.log(extractSymbols(source, ".toy"));
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Advanced exports
|
|
228
|
+
|
|
229
|
+
For advanced use cases, the package also exports lexer/parser internals and schema types, including:
|
|
230
|
+
|
|
231
|
+
- `CompiledLexer`, `getCompiledLexer`
|
|
232
|
+
- `CharReader`, `compileMatcher`, `compileCharClass`
|
|
233
|
+
- `findBlockSpans`, `extractSymbolsFromTokens`
|
|
234
|
+
- Schema and output type exports from `schema/*` and `types/*`
|
|
235
|
+
|
|
236
|
+
## Error behavior
|
|
237
|
+
|
|
238
|
+
If you pass an unknown language name/extension to `tokenize` or `extractSymbols`, the library throws an error:
|
|
239
|
+
|
|
240
|
+
```txt
|
|
241
|
+
Unknown language: "...". Use getRegisteredLanguages() to see available languages.
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
MIT
|