Skip to content

Commit

Permalink
Merge pull request #96 from char0n/char0n/expose-isNodeMissing-getter
Browse files Browse the repository at this point in the history
feat: expose nodeIsMissing getter
  • Loading branch information
verhovsky committed Jun 24, 2023
2 parents 72536c2 + 9eb780e commit e345206
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/tree_cursor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ void TreeCursor::Init(v8::Local<v8::Object> exports) {
{"endIndex", EndIndex},
{"nodeType", NodeType},
{"nodeIsNamed", NodeIsNamed},
{"nodeIsMissing", NodeIsMissing},
{"currentFieldName", CurrentFieldName},
};

Expand Down Expand Up @@ -148,6 +149,13 @@ void TreeCursor::NodeIsNamed(v8::Local<v8::String> prop, const Nan::PropertyCall
info.GetReturnValue().Set(Nan::New(ts_node_is_named(node)));
}

void TreeCursor::NodeIsMissing(v8::Local<v8::String> prop, const Nan::PropertyCallbackInfo<v8::Value> &info) {
TreeCursor *cursor = Nan::ObjectWrap::Unwrap<TreeCursor>(info.This());
TSNode node = ts_tree_cursor_current_node(&cursor->cursor_);

info.GetReturnValue().Set(Nan::New(ts_node_is_missing(node)));
}

void TreeCursor::CurrentFieldName(v8::Local<v8::String> prop, const Nan::PropertyCallbackInfo<v8::Value> &info) {
TreeCursor *cursor = Nan::ObjectWrap::Unwrap<TreeCursor>(info.This());
const char *field_name = ts_tree_cursor_current_field_name(&cursor->cursor_);
Expand Down
1 change: 1 addition & 0 deletions src/tree_cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class TreeCursor : public Nan::ObjectWrap {

static void NodeType(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void NodeIsNamed(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void NodeIsMissing(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void CurrentFieldName(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void StartIndex(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
static void EndIndex(v8::Local<v8::String>, const Nan::PropertyCallbackInfo<v8::Value> &);
Expand Down
18 changes: 18 additions & 0 deletions test/tree_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'program',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 13},
startIndex: 0,
Expand All @@ -169,6 +170,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'expression_statement',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 13},
startIndex: 0,
Expand All @@ -179,6 +181,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'binary_expression',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 13},
startIndex: 0,
Expand All @@ -189,6 +192,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'binary_expression',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 5},
startIndex: 0,
Expand All @@ -199,6 +203,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'identifier',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 1},
startIndex: 0,
Expand All @@ -210,6 +215,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: '*',
nodeIsNamed: false,
nodeIsMissing: false,
startPosition: {row: 0, column: 2},
endPosition: {row: 0, column: 3},
startIndex: 2,
Expand All @@ -220,6 +226,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'identifier',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 4},
endPosition: {row: 0, column: 5},
startIndex: 4,
Expand All @@ -231,6 +238,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'binary_expression',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 5},
startIndex: 0,
Expand All @@ -241,6 +249,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: '+',
nodeIsNamed: false,
nodeIsMissing: false,
startPosition: {row: 0, column: 6},
endPosition: {row: 0, column: 7},
startIndex: 6,
Expand All @@ -251,6 +260,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'binary_expression',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 8},
endPosition: {row: 0, column: 13},
startIndex: 8,
Expand All @@ -261,6 +271,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'identifier',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 12},
endPosition: {row: 0, column: 13},
startIndex: 12,
Expand All @@ -285,6 +296,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'binary_expression',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 5},
startIndex: 0,
Expand All @@ -295,6 +307,7 @@ describe("Tree", () => {
assertCursorState(cursor, {
nodeType: 'identifier',
nodeIsNamed: true,
nodeIsMissing: false,
startPosition: {row: 0, column: 0},
endPosition: {row: 0, column: 1},
startIndex: 0,
Expand All @@ -308,8 +321,12 @@ describe("Tree", () => {
});

function assertCursorState(cursor, params) {
assert.isBoolean(cursor.nodeIsNamed);
assert.isBoolean(cursor.nodeIsMissing);

assert.equal(cursor.nodeType, params.nodeType);
assert.equal(cursor.nodeIsNamed, params.nodeIsNamed);
assert.equal(cursor.nodeIsMissing, params.nodeIsMissing);
assert.deepEqual(cursor.startPosition, params.startPosition);
assert.deepEqual(cursor.endPosition, params.endPosition);
assert.deepEqual(cursor.startIndex, params.startIndex);
Expand All @@ -318,6 +335,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.deepEqual(node.startPosition, params.startPosition);
assert.deepEqual(node.endPosition, params.endPosition);
assert.deepEqual(node.startIndex, params.startIndex);
Expand Down
1 change: 1 addition & 0 deletions tree-sitter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ declare module "tree-sitter" {
nodeType: string;
nodeText: string;
nodeIsNamed: boolean;
nodeIsMissing: boolean;
startPosition: Point;
endPosition: Point;
startIndex: number;
Expand Down

0 comments on commit e345206

Please sign in to comment.