Skip to content

Commit 5b624a6

Browse files
fix: improve type declarations
1 parent 923c398 commit 5b624a6

File tree

2 files changed

+66
-46
lines changed

2 files changed

+66
-46
lines changed

index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ Object.defineProperty(Tree.prototype, 'rootNode', {
1818
Due to a race condition arising from Jest's worker pool, "this"
1919
has no knowledge of the native extension if the extension has not
2020
yet loaded when multiple Jest tests are being run simultaneously.
21-
If the extension has correctly loaded, "this" should be an instance
21+
If the extension has correctly loaded, "this" should be an instance
2222
of the class whose prototype we are acting on (in this case, Tree).
23-
Furthermore, the race condition sometimes results in the function in
24-
question being undefined even when the context is correct, so we also
23+
Furthermore, the race condition sometimes results in the function in
24+
question being undefined even when the context is correct, so we also
2525
perform a null function check.
2626
*/
2727
if (this instanceof Tree && rootNode) {
@@ -61,7 +61,10 @@ Tree.prototype.walk = function() {
6161

6262
class SyntaxNode {
6363
constructor(tree) {
64-
this.tree = tree;
64+
Object.defineProperty(this, 'tree', {
65+
value: tree,
66+
enumerable: true
67+
});
6568
}
6669

6770
[util.inspect.custom]() {
@@ -903,8 +906,16 @@ function initializeLanguageNodeClasses(language) {
903906

904907
const className = camelCase(typeName, true) + 'Node';
905908
const nodeSubclass = eval(`class ${className} extends SyntaxNode {${classBody}}; ${className}`);
906-
nodeSubclass.prototype.type = typeName;
907-
nodeSubclass.prototype.fields = Object.freeze(fieldNames.sort())
909+
Object.defineProperties(nodeSubclass.prototype, {
910+
type: {
911+
value: typeName,
912+
enumerable: true
913+
},
914+
fields: {
915+
value: Object.freeze(fieldNames.sort()),
916+
enumerable: true
917+
}
918+
});
908919
nodeSubclasses[id] = nodeSubclass;
909920
}
910921

tree-sitter.d.ts

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -187,153 +187,160 @@ declare module "tree-sitter" {
187187
* @param position - Optional position in the text as {row, column}
188188
* @returns A string chunk, or null/undefined if no text at this index
189189
*/
190-
(index: number, position?: Point): string | null | undefined | {};
190+
(index: number, position?: Point): string | null | undefined;
191191
}
192192

193+
type NodeName = `${string}Node` | `${string}Nodes`;
194+
193195
/** The syntax tree that contains this node */
194196
export interface SyntaxNode {
195197
/** The syntax tree that contains this node */
196-
tree: Tree;
198+
readonly tree: Tree;
197199

198200
/**
199201
* A unique numeric identifier for this node.
200202
* Within a given syntax tree, no two nodes have the same id.
201203
* If a new tree is created based on an older tree and reuses
202204
* a node, that node will have the same id in both trees.
203205
*/
204-
id: number;
206+
readonly id: number;
205207

206208
/**
207209
* This node's type as a numeric id
208210
*/
209-
typeId: number;
211+
readonly typeId: number;
210212

211213
/**
212214
* This node's type as a numeric id as it appears in the grammar,
213215
* ignoring aliases
214216
*/
215-
grammarId: number;
217+
readonly grammarId: number;
216218

217219
/**
218220
* This node's type as a string
219221
*/
220-
type: string;
222+
readonly type: string;
221223

222224
/**
223225
* This node's symbol name as it appears in the grammar,
224226
* ignoring aliases
225227
*/
226-
grammarType: string;
228+
readonly grammarType: string;
227229

228230
/**
229231
* Whether this node is named.
230232
* Named nodes correspond to named rules in the grammar,
231233
* whereas anonymous nodes correspond to string literals in the grammar.
232234
*/
233-
isNamed: boolean;
235+
readonly isNamed: boolean;
234236

235237
/**
236238
* Whether this node is missing.
237239
* Missing nodes are inserted by the parser in order to
238240
* recover from certain kinds of syntax errors.
239241
*/
240-
isMissing: boolean;
242+
readonly isMissing: boolean;
241243

242244
/**
243245
* Whether this node is extra.
244246
* Extra nodes represent things like comments, which are not
245247
* required by the grammar but can appear anywhere.
246248
*/
247-
isExtra: boolean;
249+
readonly isExtra: boolean;
248250

249251
/**
250252
* Whether this node has been edited
251253
*/
252-
hasChanges: boolean;
254+
readonly hasChanges: boolean;
253255

254256
/**
255257
* Whether this node represents a syntax error or contains
256258
* any syntax errors within it
257259
*/
258-
hasError: boolean;
260+
readonly hasError: boolean;
259261

260262
/**
261263
* Whether this node represents a syntax error.
262264
* Syntax errors represent parts of the code that could not
263265
* be incorporated into a valid syntax tree.
264266
*/
265-
isError: boolean;
267+
readonly isError: boolean;
266268

267269
/** The text content for this node from the source code */
268-
text: string;
270+
readonly text: string;
269271

270272
/** The parse state of this node */
271-
parseState: number;
273+
readonly parseState: number;
272274

273275
/** The parse state that follows this node */
274-
nextParseState: number;
276+
readonly nextParseState: number;
275277

276278
/** The position where this node starts in terms of rows and columns */
277-
startPosition: Point;
279+
readonly startPosition: Point;
278280

279281
/** The position where this node ends in terms of rows and columns */
280-
endPosition: Point;
282+
readonly endPosition: Point;
281283

282284
/** The byte offset where this node starts */
283-
startIndex: number;
285+
readonly startIndex: number;
284286

285287
/** The byte offset where this node ends */
286-
endIndex: number;
288+
readonly endIndex: number;
287289

288290
/**
289291
* This node's immediate parent.
290292
* For iterating over ancestors, prefer using {@link childWithDescendant}
291293
*/
292-
parent: SyntaxNode | null;
294+
readonly parent: SyntaxNode | null;
293295

294296
/** Array of all child nodes */
295-
children: Array<SyntaxNode>;
297+
readonly children: Array<SyntaxNode>;
296298

297299
/** Array of all named child nodes */
298-
namedChildren: Array<SyntaxNode>;
300+
readonly namedChildren: Array<SyntaxNode>;
299301

300302
/** The number of children this node has */
301-
childCount: number;
303+
readonly childCount: number;
302304

303305
/**
304306
* The number of named children this node has.
305307
* @see {@link isNamed}
306308
*/
307-
namedChildCount: number;
309+
readonly namedChildCount: number;
308310

309311
/** The first child of this node */
310-
firstChild: SyntaxNode | null;
312+
readonly firstChild: SyntaxNode | null;
311313

312314
/** The first named child of this node */
313-
firstNamedChild: SyntaxNode | null;
315+
readonly firstNamedChild: SyntaxNode | null;
314316

315317
/** The last child of this node */
316-
lastChild: SyntaxNode | null;
318+
readonly lastChild: SyntaxNode | null;
317319

318320
/** The last child of this node */
319-
lastNamedChild: SyntaxNode | null;
321+
readonly lastNamedChild: SyntaxNode | null;
320322

321323
/** This node's next sibling */
322-
nextSibling: SyntaxNode | null;
324+
readonly nextSibling: SyntaxNode | null;
323325

324326
/** This node's next named sibling */
325-
nextNamedSibling: SyntaxNode | null;
327+
readonly nextNamedSibling: SyntaxNode | null;
326328

327329
/** This node's previous sibling */
328-
previousSibling: SyntaxNode | null;
330+
readonly previousSibling: SyntaxNode | null;
329331

330332
/** This node's previous named sibling */
331-
previousNamedSibling: SyntaxNode | null;
333+
readonly previousNamedSibling: SyntaxNode | null;
332334

333335
/**
334336
* The number of descendants this node has, including itself
335337
*/
336-
descendantCount: number;
338+
readonly descendantCount: number;
339+
340+
/**
341+
* The names of extra fields available in the subclass, if any.
342+
*/
343+
readonly fields: Array<NodeName>;
337344

338345
/**
339346
* Convert this node to its string representation
@@ -549,6 +556,8 @@ declare module "tree-sitter" {
549556
* @returns A new cursor positioned at this node
550557
*/
551558
walk(): TreeCursor;
559+
560+
readonly [name: NodeName]: SyntaxNode;
552561
}
553562

554563
/** A stateful object for walking a syntax {@link Tree} efficiently */
@@ -631,7 +640,7 @@ declare module "tree-sitter" {
631640

632641
/**
633642
* Move this cursor to the last child of its current node.
634-
* Note: This may be slower than gotoFirstChild() as it needs to iterate
643+
* Note: This may be slower than gotoFirstChild() as it needs to iterate
635644
* through all children to compute the position.
636645
*
637646
* @returns true if cursor successfully moved, false if there were no children
@@ -701,7 +710,7 @@ declare module "tree-sitter" {
701710

702711
/**
703712
* Edit the syntax tree to keep it in sync with source code that has been edited.
704-
* The edit must be described both in terms of byte offsets and in terms of
713+
* The edit must be described both in terms of byte offsets and in terms of
705714
* row/column coordinates.
706715
*
707716
* @param edit - The edit to apply to the tree
@@ -725,11 +734,11 @@ declare module "tree-sitter" {
725734
getText(node: SyntaxNode): string;
726735

727736
/**
728-
* Compare this edited syntax tree to a new syntax tree representing the
737+
* Compare this edited syntax tree to a new syntax tree representing the
729738
* same document, returning ranges whose syntactic structure has changed.
730739
*
731740
* For this to work correctly, this tree must have been edited to match
732-
* the new tree's ranges. Generally, you'll want to call this right after
741+
* the new tree's ranges. Generally, you'll want to call this right after
733742
* parsing, using the old tree that was passed to parse and the new tree
734743
* that was returned.
735744
*
@@ -776,7 +785,7 @@ declare module "tree-sitter" {
776785
* A match of a {@link Query} to a particular set of {@link SyntaxNode}s.
777786
*/
778787
export interface QueryMatch {
779-
/**
788+
/**
780789
* The index of the pattern that was matched.
781790
* Each pattern in a query is assigned a numeric index in sequence.
782791
*/
@@ -955,7 +964,7 @@ declare module "tree-sitter" {
955964
* This returns `null` if the state is invalid for this language.
956965
*
957966
* Iterating {@link LookaheadIterator} will yield valid symbols in the given
958-
* parse state. Newly created lookahead iterators will have {@link currentType}
967+
* parse state. Newly created lookahead iterators will have {@link currentType}
959968
* populated with the `ERROR` symbol.
960969
*
961970
* Lookahead iterators can be useful to generate suggestions and improve

0 commit comments

Comments
 (0)