Skip to content

Commit 74d114f

Browse files
fix: improve type declarations
1 parent 1399c46 commit 74d114f

File tree

8 files changed

+90
-65
lines changed

8 files changed

+90
-65
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

test/lookahead_iterable_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/** @type {typeof import('tree-sitter')} */
1+
/// <reference path="../tree-sitter.d.ts" />
2+
/** @type {typeof import("tree-sitter")} */
23
const Parser = require("../index.js");
34
const Rust = require("tree-sitter-rust");
45
const assert = require('node:assert');

test/node_test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/** @type {typeof import('tree-sitter')} */
2-
const Parser = require("../index.js");
1+
/// <reference path="../tree-sitter.d.ts" />
2+
/** @type {typeof import("tree-sitter")} */
3+
const Parser = require("../index");
34

45
const C = require('tree-sitter-c');
56
const EmbeddedTemplate = require('tree-sitter-embedded-template');
@@ -64,17 +65,15 @@ describe("Node", () => {
6465
`);
6566

6667
const classNode = tree.rootNode.firstChild;
67-
// @ts-ignore
6868
assert.deepEqual(classNode.fields, ['bodyNode', 'decoratorNodes', 'nameNode'])
6969

70-
// @ts-ignore
7170
const methodNode = classNode.bodyNode.firstNamedChild;
7271
assert.equal(methodNode.constructor.name, 'MethodDefinitionNode');
7372
assert.equal(methodNode.nameNode.text, 'b');
7473
assert.deepEqual(methodNode.fields, ['bodyNode', 'decoratorNodes', 'nameNode', 'parametersNode'])
7574

7675
const decoratorNodes = methodNode.decoratorNodes;
77-
assert.deepEqual(decoratorNodes.map(_ => _.text), ['@autobind', '@something'])
76+
assert.deepEqual(decoratorNodes.map(n => n.text), ['@autobind', '@something'])
7877

7978
const paramsNode = methodNode.parametersNode;
8079
assert.equal(paramsNode.constructor.name, 'FormalParametersNode');
@@ -707,14 +706,14 @@ describe("Node", () => {
707706

708707
describe(".text", () => {
709708
Object.entries({
710-
'.parse(String)': (parser, src) => parser.parse(src),
711-
'.parse(Function)': (parser, src) =>
712-
parser.parse(offset => src.substr(offset, 4)),
709+
'.parse(String)': (/** @type {import('tree-sitter')} */ parser, /** @type {string} */ src) => parser.parse(src),
710+
'.parse(Function)': (/** @type {import('tree-sitter')} */ parser, /** @type {string} */ src) =>
711+
parser.parse(offset => src.substring(offset, offset + 4)),
713712
}).forEach(([method, parse]) =>
714713
it(`returns the text of a node generated by ${method}`, async () => {
715714
const src = "α0 / b👎c👎"
716715
const [numeratorSrc, denominatorSrc] = src.split(/\s*\/\s+/)
717-
const tree = await parse(parser, src)
716+
const tree = parse(parser, src)
718717
const quotientNode = tree.rootNode.firstChild.firstChild;
719718
const [numerator, slash, denominator] = quotientNode.children;
720719

@@ -921,10 +920,8 @@ describe("Node", () => {
921920
it("shouldn't segfault when accessing properties on the prototype", () => {
922921
const tree = parser.parse('2 + 2');
923922
const nodePrototype = Object.getPrototypeOf(tree.rootNode);
924-
// const nodePrototype = tree.rootNode.__proto__;
925923

926924
const properties = [
927-
"type",
928925
"typeId",
929926
"isNamed",
930927
"isMissing",
@@ -950,6 +947,9 @@ describe("Node", () => {
950947
"previousSibling",
951948
"previousNamedSibling",
952949
];
950+
if (nodePrototype.constructor.name === "SyntaxNode") {
951+
assert.throws(() => { nodePrototype.type; }, TypeError)
952+
}
953953
for (const property of properties) {
954954
assert.throws(() => { nodePrototype[property]; }, TypeError)
955955
}

test/parser_test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/** @type {typeof import('tree-sitter')} */
1+
/// <reference path="../tree-sitter.d.ts" />
2+
/** @type {typeof import("tree-sitter")} */
23
const Parser = require("../index.js");
34
const HTML = require('tree-sitter-html');
45
const JavaScript = require('tree-sitter-javascript');
@@ -17,6 +18,7 @@ describe("Parser", () => {
1718

1819
describe(".setLanguage", () => {
1920
it("throws an exception when the supplied object is not a tree-sitter language", () => {
21+
// @ts-expect-error: Invalid language
2022
assert.throws(() => parser.setLanguage({}), /Invalid language/);
2123
assert.throws(() => parser.setLanguage(undefined), /Invalid language/);
2224
});
@@ -166,7 +168,7 @@ describe("Parser", () => {
166168

167169
describe("when the input callback returns something other than a string", () => {
168170
it("stops reading", () => {
169-
const parts = ["abc", "def", "ghi", {}, {}, {}, "second-word", " "];
171+
const parts = ["abc", "def", "ghi", null, null, null, "second-word", " "];
170172
const tree = parser.parse(() => parts.shift());
171173
assert.equal(
172174
tree.rootNode.toString(),

test/query_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/** @type {typeof import('tree-sitter')} */
1+
/// <reference path="../tree-sitter.d.ts" />
2+
/** @type {typeof import("tree-sitter")} */
23
const Parser = require("../index.js");
34

45
const C = require("tree-sitter-c");

test/tree_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
/** @type {typeof import('tree-sitter')} */
1+
/// <reference path="../tree-sitter.d.ts" />
2+
/** @type {typeof import("tree-sitter")} */
23
const Parser = require("../index.js");
34
const JavaScript = require('tree-sitter-javascript');
45
const Rust = require('tree-sitter-rust');

0 commit comments

Comments
 (0)