feat(web): add missing API functions

Co-authored-by: Will Lillis <will.lillis24@gmail.com>
This commit is contained in:
Amaan Qureshi 2025-01-05 22:06:33 -05:00
parent dcdd6ce2d2
commit 45fa028201
11 changed files with 436 additions and 35 deletions

View file

@ -1,5 +1,5 @@
const {assert} = require('chai');
let Parser; let JavaScript; let JSON; let EmbeddedTemplate; let Python;
let Parser; let C; let JavaScript; let JSON; let EmbeddedTemplate; let Python;
const JSON_EXAMPLE = `
@ -35,7 +35,7 @@ describe('Node', () => {
let parser; let tree;
before(async () =>
({Parser, EmbeddedTemplate, JavaScript, JSON, Python} = await require('./helper')),
({Parser, C, EmbeddedTemplate, JavaScript, JSON, Python} = await require('./helper')),
);
beforeEach(() => {
@ -620,17 +620,57 @@ describe('Node', () => {
describe('.fieldNameForChild(index)', () => {
it('returns the field of a child or null', () => {
tree = parser.parse('let a = 5');
parser.setLanguage(C);
tree = parser.parse('int w = x + /* y is special! */ y;');
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);
const translationUnitNode = tree.rootNode;
const declarationNode = translationUnitNode.firstChild;
const binaryExpressionNode = declarationNode
.childForFieldName('declarator')
.childForFieldName('value');
assert.equal(noField, null);
assert.equal(name, 'name');
assert.equal(value, 'value');
assert.equal(overflow, null);
// -------------------
// left: (identifier) 0
// operator: "+" _ <--- (not a named child)
// (comment) 1 <--- (is an extra)
// right: (identifier) 2
// -------------------
assert.equal(binaryExpressionNode.fieldNameForChild(0), 'left');
assert.equal(binaryExpressionNode.fieldNameForChild(1), 'operator');
// The comment should not have a field name, as it's just an extra
assert.equal(binaryExpressionNode.fieldNameForChild(2), null);
assert.equal(binaryExpressionNode.fieldNameForChild(3), 'right');
// Negative test - Not a valid child index
assert.equal(binaryExpressionNode.fieldNameForChild(4), null);
});
});
describe('.fieldNameForNamedChild(index)', () => {
it('returns the field of a named child or null', () => {
parser.setLanguage(C);
tree = parser.parse('int w = x + /* y is special! */ y;');
const translationUnitNode = tree.rootNode;
const declarationNode = translationUnitNode.firstNamedChild;
const binaryExpressionNode = declarationNode
.childForFieldName('declarator')
.childForFieldName('value');
// -------------------
// left: (identifier) 0
// operator: "+" _ <--- (not a named child)
// (comment) 1 <--- (is an extra)
// right: (identifier) 2
// -------------------
assert.equal(binaryExpressionNode.fieldNameForNamedChild(0), 'left');
// The comment should not have a field name, as it's just an extra
assert.equal(binaryExpressionNode.fieldNameForNamedChild(1), null);
// The operator is not a named child, so the named child at index 2 is the right child
assert.equal(binaryExpressionNode.fieldNameForNamedChild(2), 'right');
// Negative test - Not a valid child index
assert.equal(binaryExpressionNode.fieldNameForNamedChild(3), null);
});
});
});