Skip to content

Commit

Permalink
refactor!: make is* and has* methods properties
Browse files Browse the repository at this point in the history
  • Loading branch information
amaanq committed Mar 8, 2024
1 parent 121189f commit 5827523
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
50 changes: 25 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,36 @@ class SyntaxNode {
return NodeMethods.grammarName(this.tree);
}

get isExtra() {
marshalNode(this);
return NodeMethods.isExtra(this.tree);
}

get isNamed() {
marshalNode(this);
return NodeMethods.isNamed(this.tree);
}

get isMissing() {
marshalNode(this);
return NodeMethods.isMissing(this.tree);
}

get hasChanges() {
marshalNode(this);
return NodeMethods.hasChanges(this.tree);
}

get hasError() {
marshalNode(this);
return NodeMethods.hasError(this.tree);
}

get isError() {
marshalNode(this);
return NodeMethods.isError(this.tree);
}

get text() {
return this.tree.getText(this);
}
Expand Down Expand Up @@ -215,31 +240,6 @@ class SyntaxNode {
return NodeMethods.descendantCount(this.tree);
}

hasChanges() {
marshalNode(this);
return NodeMethods.hasChanges(this.tree);
}

hasError() {
marshalNode(this);
return NodeMethods.hasError(this.tree);
}

isMissing() {
marshalNode(this);
return NodeMethods.isMissing(this.tree);
}

isExtra() {
marshalNode(this);
return NodeMethods.isExtra(this.tree);
}

isError() {
marshalNode(this);
return NodeMethods.isError(this.tree);
}

toString() {
marshalNode(this);
return NodeMethods.toString(this.tree);
Expand Down
33 changes: 17 additions & 16 deletions test/node_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ describe("Node", () => {
});
});

describe(".hasError()", () => {
describe(".hasError", () => {
it("returns true if the node contains an error", () => {
const tree = parser.parse("1 + 2 * * 3");
const node = tree.rootNode;
Expand All @@ -631,14 +631,14 @@ describe("Node", () => {
);

const sum = node.firstChild.firstChild;
assert(sum.hasError());
assert(!sum.children[0].hasError());
assert(!sum.children[1].hasError());
assert(sum.children[2].hasError());
assert(sum.hasError);
assert(!sum.children[0].hasError);
assert(!sum.children[1].hasError);
assert(sum.children[2].hasError);
});
});

describe(".isMissing()", () => {
describe(".isMissing)", () => {
it("returns true if the node is missing from the source and was inserted via error recovery", () => {
const tree = parser.parse("(2 ||)");
const node = tree.rootNode;
Expand All @@ -649,23 +649,23 @@ describe("Node", () => {

const sum = node.firstChild.firstChild.firstNamedChild;
assert.equal(sum.type, 'binary_expression')
assert(sum.hasError());
assert(!sum.children[0].isMissing());
assert(!sum.children[1].isMissing());
assert(sum.children[2].isMissing());
assert(sum.hasError);
assert(!sum.children[0].isMissing);
assert(!sum.children[1].isMissing);
assert(sum.children[2].isMissing);
});
});

describe(".isExtra()", () => {
describe(".isExtra", () => {
it("returns true if the node is an extra node like comments", () => {
const tree = parser.parse("foo(/* hi */);");
const node = tree.rootNode;
const comment_node = node.descendantForIndex(7, 7);

assert.equal(node.type, "program");
assert.equal(comment_node.type, "comment");
assert(!node.isExtra());
assert(comment_node.isExtra());
assert(!node.isExtra);
assert(comment_node.isExtra);
});
});

Expand Down Expand Up @@ -907,6 +907,10 @@ describe("Node", () => {
"type",
"typeId",
"isNamed",
"isMissing",
"hasChanges",
"hasError",
"isError",
"text",
"startPosition",
"endPosition",
Expand All @@ -931,9 +935,6 @@ describe("Node", () => {
}

const methods = [
"hasChanges",
"hasError",
"isMissing",
"toString",
"walk",
// these take arguments but the "this" check happens first
Expand Down
2 changes: 1 addition & 1 deletion test/tree_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ function assertCursorState(cursor, params) {
const node = cursor.currentNode
assert.equal(node.type, params.nodeType);
assert.equal(node.isNamed, params.nodeIsNamed);
assert.equal(node.isMissing(), params.nodeIsMissing);
assert.equal(node.isMissing, params.nodeIsMissing);
assert.deepEqual(node.startPosition, params.startPosition);
assert.deepEqual(node.endPosition, params.endPosition);
assert.deepEqual(node.startIndex, params.startIndex);
Expand Down

0 comments on commit 5827523

Please sign in to comment.