Merge pull request #2466 from tom95/add-field-name-for-child

Web Bindings: Expose fieldNameForChild C function
This commit is contained in:
Amaan Qureshi 2023-08-18 12:20:39 -04:00 committed by GitHub
commit 7d953eb5ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 0 deletions

View file

@ -361,6 +361,11 @@ uint16_t ts_node_symbol_wasm(const TSTree *tree) {
return ts_node_symbol(node);
}
const char *ts_node_field_name_for_child_wasm(const TSTree *tree, uint32_t index) {
TSNode node = unmarshal_node(tree);
return ts_node_field_name_for_child(node, index);
}
uint16_t ts_node_grammar_symbol_wasm(const TSTree *tree) {
TSNode node = unmarshal_node(tree);
return ts_node_grammar_symbol(node);

View file

@ -282,6 +282,17 @@ class Node {
return unmarshalNode(this.tree);
}
fieldNameForChild(index) {
marshalNode(this);
const address = C._ts_node_field_name_for_child_wasm(this.tree[0], index);
if (!address) {
return null;
}
const result = AsciiToString(address);
// must not free, the string memory is owned by the language
return result;
}
namedChild(index) {
marshalNode(this);
C._ts_node_named_child_wasm(this.tree[0], index);

View file

@ -44,6 +44,7 @@
"_ts_language_symbol_type",
"_ts_language_version",
"_ts_language_next_state",
"_ts_node_field_name_for_child_wasm",
"_ts_node_child_by_field_id_wasm",
"_ts_node_child_count_wasm",
"_ts_node_child_wasm",

View file

@ -454,4 +454,20 @@ describe("Node", () => {
assert(!node1.equals(node2));
});
});
describe('.fieldNameForChild(index)', () => {
it('returns the field of a child or null', () => {
tree = parser.parse('let a = 5');
const noField = tree.rootNode.fieldNameForChild(0);
const name = tree.rootNode.firstChild.children[1].fieldNameForChild(0);
const value = tree.rootNode.firstChild.children[1].fieldNameForChild(2);
const overflow = tree.rootNode.firstChild.children[1].fieldNameForChild(3);
assert.equal(noField, null);
assert.equal(name, 'name');
assert.equal(value, 'value');
assert.equal(overflow, null);
});
});
});