API extensions

This commit is contained in:
Daumantas Kavolis 2023-05-17 10:39:37 +03:00
parent 08f4e82bb2
commit c47e217e73
14 changed files with 741 additions and 29 deletions

View file

@ -268,6 +268,24 @@ describe("Node", () => {
});
});
describe(".isError()", () => {
it("returns true if the node is an error", () => {
tree = parser.parse("2 * * 3");
const node = tree.rootNode;
assert.equal(
node.toString(),
'(program (expression_statement (binary_expression left: (number) (ERROR) right: (number))))'
);
const multi = node.firstChild.firstChild;
assert(multi.hasError());
assert(!multi.children[0].isError());
assert(!multi.children[1].isError());
assert(multi.children[2].isError());
assert(!multi.children[3].isError());
});
});
describe(".isMissing()", () => {
it("returns true if the node is missing from the source and was inserted via error recovery", () => {
tree = parser.parse("(2 ||)");
@ -308,6 +326,21 @@ describe("Node", () => {
);
});
describe(".parseState", () => {
const text = "10 * 5";
it(`returns node parse state ids`, async () => {
tree = await parser.parse(text)
const quotientNode = tree.rootNode.firstChild.firstChild;
const [numerator, slash, denominator] = quotientNode.children;
assert.equal(tree.rootNode.parseState, 0);
assert.equal(numerator.parseState, 1);
assert.equal(slash.parseState, 553);
assert.equal(denominator.parseState, 185);
})
});
describe('.descendantsOfType(type, min, max)', () => {
it('finds all of the descendants of the given type in the given range', () => {
tree = parser.parse("a + 1 * b * 2 + c + 3");