Skip to content

Commit

Permalink
Added missing ts tree-sitter.d.ts declarations (#80)
Browse files Browse the repository at this point in the history
* Fix typo

* tree-sitter.d.ts - define missing Parser class fields

* tree-sitter.d.ts - define missing SyntaxNode typeId field

* tree-sitter.d.ts - define missed TreeCursor currentFieldName field

* tree-sitter.d.ts - define missed Tree printDotGraph method

* tree-sitter.d.ts - define missed Query class and related types

* tree-sitter.d.ts - fix Query.captures return type

* tree-sitter.d.ts - sync query parameter names with wasm binding

* fix types checking for node types
  • Loading branch information
ahlinc committed Mar 3, 2021
1 parent 812cd15 commit 738bb12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
14 changes: 7 additions & 7 deletions index.js
Expand Up @@ -388,7 +388,7 @@ const ZERO_POINT = { row: 0, column: 0 };

Query.prototype._init = function() {
/*
* Initializa predicate functions
* Initialize predicate functions
* format: [type1, value1, type2, value2, ...]
*/
const predicateDescriptions = this._getPredicates();
Expand Down Expand Up @@ -510,11 +510,11 @@ Query.prototype._init = function() {
this.refutedProperties = Object.freeze(refutedProperties);
}

Query.prototype.matches = function(rootNode, start = ZERO_POINT, end = ZERO_POINT) {
Query.prototype.matches = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _matches.call(this, rootNode.tree,
start.row, start.column,
end.row, end.column
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const results = [];
Expand Down Expand Up @@ -548,11 +548,11 @@ Query.prototype.matches = function(rootNode, start = ZERO_POINT, end = ZERO_POIN
return results;
}

Query.prototype.captures = function(rootNode, start = ZERO_POINT, end = ZERO_POINT) {
Query.prototype.captures = function(rootNode, startPosition = ZERO_POINT, endPosition = ZERO_POINT) {
marshalNode(rootNode);
const [returnedMatches, returnedNodes] = _captures.call(this, rootNode.tree,
start.row, start.column,
end.row, end.column
startPosition.row, startPosition.column,
endPosition.row, endPosition.column
);
const nodes = unmarshalNodes(returnedNodes, rootNode.tree);
const results = [];
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"prebuild-install": "^5.0.0"
},
"devDependencies": {
"@types/node": "^14.14.31",
"chai": "3.5.x",
"mocha": "^5.2.0",
"prebuild": "^7.6.0",
Expand Down
42 changes: 40 additions & 2 deletions tree-sitter.d.ts
@@ -1,10 +1,13 @@
declare module "tree-sitter" {
class Parser {
parse(input: string | Parser.Input, previousTree?: Parser.Tree, options?: {bufferSize?: number, includedRanges?: Parser.Range[]}): Parser.Tree;
parse(input: string | Parser.Input | Parser.InputReader, oldTree?: Parser.Tree, options?: { bufferSize?: number, includedRanges?: Parser.Range[] }): Parser.Tree;
parseTextBuffer(buffer: Parser.TextBuffer, oldTree?: Parser.Tree, options?: { syncTimeoutMicros?: number, includedRanges?: Parser.Range[] }): Parser.Tree | Promise<Parser.Tree>;
parseTextBufferSync(buffer: Parser.TextBuffer, oldTree?: Parser.Tree, options?: { includedRanges?: Parser.Range[] }): Parser.Tree;
getLanguage(): any;
setLanguage(language: any): void;
getLogger(): Parser.Logger;
setLogger(logFunc: Parser.Logger): void;
printDotGraphs(enabled: boolean): void;
}

namespace Parser {
Expand Down Expand Up @@ -35,6 +38,12 @@ declare module "tree-sitter" {
type: "parse" | "lex"
) => void;

export type TextBuffer = Buffer;

export interface InputReader {
(index: any, position: Point): string;
}

export interface Input {
seek(index: number): void;
read(): any;
Expand All @@ -43,6 +52,7 @@ declare module "tree-sitter" {
export interface SyntaxNode {
tree: Tree;
type: string;
typeId: string;
isNamed: boolean;
text: string;
startPosition: Point;
Expand Down Expand Up @@ -94,7 +104,8 @@ declare module "tree-sitter" {
endPosition: Point;
startIndex: number;
endIndex: number;
readonly currentNode: SyntaxNode
readonly currentNode: SyntaxNode;
readonly currentFieldName: string;

reset(node: SyntaxNode): void
gotoParent(): boolean;
Expand All @@ -110,6 +121,33 @@ declare module "tree-sitter" {
walk(): TreeCursor;
getChangedRanges(other: Tree): Range[];
getEditedRange(other: Tree): Range;
printDotGraph(): void;
}

export interface QueryMatch {
pattern: number,
captures: QueryCapture[],
}

export interface QueryCapture {
name: string,
text?: string,
node: SyntaxNode,
setProperties?: {[prop: string]: string | null},
assertedProperties?: {[prop: string]: string | null},
refutedProperties?: {[prop: string]: string | null},
}

export class Query {
readonly predicates: { [name: string]: Function }[];
readonly setProperties: any[];
readonly assertedProperties: any[];
readonly refutedProperties: any[];

constructor(language: any, source: string | Buffer);

matches(rootNode: SyntaxNode, startPosition?: Point, endPosition?: Point): QueryMatch[];
captures(rootNode: SyntaxNode, startPosition?: Point, endPosition?: Point): QueryCapture[];
}
}

Expand Down

0 comments on commit 738bb12

Please sign in to comment.