chore(web): add and apply eslint formatting

This commit is contained in:
Amaan Qureshi 2024-02-07 11:32:27 -05:00
parent 5f63074057
commit 96a440af35
11 changed files with 622 additions and 569 deletions

View file

@ -1,11 +1,11 @@
const {assert} = require('chai');
let Parser, JavaScript;
let Parser; let JavaScript;
describe("Node", () => {
let parser, tree;
describe('Node', () => {
let parser; let tree;
before(async () =>
({Parser, JavaScript} = await require('./helper'))
({Parser, JavaScript} = await require('./helper')),
);
beforeEach(() => {
@ -18,83 +18,83 @@ describe("Node", () => {
tree.delete();
});
describe(".children", () => {
it("returns an array of child nodes", () => {
tree = parser.parse("x10 + 1000");
describe('.children', () => {
it('returns an array of child nodes', () => {
tree = parser.parse('x10 + 1000');
assert.equal(1, tree.rootNode.children.length);
const sumNode = tree.rootNode.firstChild.firstChild;
assert.deepEqual(
sumNode.children.map(child => child.type),
["identifier", "+", "number"]
sumNode.children.map((child) => child.type),
['identifier', '+', 'number'],
);
});
});
describe(".namedChildren", () => {
it("returns an array of named child nodes", () => {
tree = parser.parse("x10 + 1000");
describe('.namedChildren', () => {
it('returns an array of named child nodes', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.equal(1, tree.rootNode.namedChildren.length);
assert.deepEqual(
["identifier", "number"],
sumNode.namedChildren.map(child => child.type)
['identifier', 'number'],
sumNode.namedChildren.map((child) => child.type),
);
});
});
describe(".startIndex and .endIndex", () => {
it("returns the character index where the node starts/ends in the text", () => {
tree = parser.parse("a👍👎1 / b👎c👎");
describe('.startIndex and .endIndex', () => {
it('returns the character index where the node starts/ends in the text', () => {
tree = parser.parse('a👍👎1 / b👎c👎');
const quotientNode = tree.rootNode.firstChild.firstChild;
assert.equal(0, quotientNode.startIndex);
assert.equal(15, quotientNode.endIndex);
assert.deepEqual(
[0, 7, 9],
quotientNode.children.map(child => child.startIndex)
quotientNode.children.map((child) => child.startIndex),
);
assert.deepEqual(
[6, 8, 15],
quotientNode.children.map(child => child.endIndex)
quotientNode.children.map((child) => child.endIndex),
);
});
});
describe(".startPosition and .endPosition", () => {
it("returns the row and column where the node starts/ends in the text", () => {
tree = parser.parse("x10 + 1000");
describe('.startPosition and .endPosition', () => {
it('returns the row and column where the node starts/ends in the text', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.equal("binary_expression", sumNode.type);
assert.equal('binary_expression', sumNode.type);
assert.deepEqual({ row: 0, column: 0 }, sumNode.startPosition);
assert.deepEqual({ row: 0, column: 10 }, sumNode.endPosition);
assert.deepEqual({row: 0, column: 0}, sumNode.startPosition);
assert.deepEqual({row: 0, column: 10}, sumNode.endPosition);
assert.deepEqual(
[{ row: 0, column: 0 }, { row: 0, column: 4 }, { row: 0, column: 6 }],
sumNode.children.map(child => child.startPosition)
[{row: 0, column: 0}, {row: 0, column: 4}, {row: 0, column: 6}],
sumNode.children.map((child) => child.startPosition),
);
assert.deepEqual(
[{ row: 0, column: 3 }, { row: 0, column: 5 }, { row: 0, column: 10 }],
sumNode.children.map(child => child.endPosition)
[{row: 0, column: 3}, {row: 0, column: 5}, {row: 0, column: 10}],
sumNode.children.map((child) => child.endPosition),
);
});
it("handles characters that occupy two UTF16 code units", () => {
tree = parser.parse("a👍👎1 /\n b👎c👎");
it('handles characters that occupy two UTF16 code units', () => {
tree = parser.parse('a👍👎1 /\n b👎c👎');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.deepEqual(
[
[{ row: 0, column: 0 }, { row: 0, column: 6 }],
[{ row: 0, column: 7 }, { row: 0, column: 8 }],
[{ row: 1, column: 1 }, { row: 1, column: 7 }]
[{row: 0, column: 0}, {row: 0, column: 6}],
[{row: 0, column: 7}, {row: 0, column: 8}],
[{row: 1, column: 1}, {row: 1, column: 7}],
],
sumNode.children.map(child => [child.startPosition, child.endPosition])
sumNode.children.map((child) => [child.startPosition, child.endPosition]),
);
});
});
describe(".parent", () => {
it("returns the node's parent", () => {
tree = parser.parse("x10 + 1000");
describe('.parent', () => {
it('returns the node\'s parent', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild;
const variableNode = sumNode.firstChild;
assert.notEqual(sumNode.id, variableNode.id);
@ -105,7 +105,7 @@ describe("Node", () => {
describe('.child(), .firstChild, .lastChild', () => {
it('returns null when the node has no children', () => {
tree = parser.parse("x10 + 1000");
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
const variableNode = sumNode.firstChild;
assert.equal(variableNode.firstChild, null);
@ -113,12 +113,12 @@ describe("Node", () => {
assert.equal(variableNode.firstNamedChild, null);
assert.equal(variableNode.lastNamedChild, null);
assert.equal(variableNode.child(1), null);
})
});
});
describe('.childForFieldName()', () => {
it('returns null when the node has no children', () => {
tree = parser.parse("class A { b() {} }");
tree = parser.parse('class A { b() {} }');
const classNode = tree.rootNode.firstChild;
assert.equal(classNode.type, 'class_declaration');
@ -145,119 +145,119 @@ describe("Node", () => {
});
});
describe(".nextSibling and .previousSibling", () => {
it("returns the node's next and previous sibling", () => {
tree = parser.parse("x10 + 1000");
describe('.nextSibling and .previousSibling', () => {
it('returns the node\'s next and previous sibling', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.equal(sumNode.children[1].id, sumNode.children[0].nextSibling.id);
assert.equal(sumNode.children[2].id, sumNode.children[1].nextSibling.id);
assert.equal(
sumNode.children[0].id,
sumNode.children[1].previousSibling.id
sumNode.children[1].previousSibling.id,
);
assert.equal(
sumNode.children[1].id,
sumNode.children[2].previousSibling.id
sumNode.children[2].previousSibling.id,
);
});
});
describe(".nextNamedSibling and .previousNamedSibling", () => {
it("returns the node's next and previous named sibling", () => {
tree = parser.parse("x10 + 1000");
describe('.nextNamedSibling and .previousNamedSibling', () => {
it('returns the node\'s next and previous named sibling', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.equal(
sumNode.namedChildren[1].id,
sumNode.namedChildren[0].nextNamedSibling.id
sumNode.namedChildren[0].nextNamedSibling.id,
);
assert.equal(
sumNode.namedChildren[0].id,
sumNode.namedChildren[1].previousNamedSibling.id
sumNode.namedChildren[1].previousNamedSibling.id,
);
});
});
describe(".descendantForIndex(min, max)", () => {
it("returns the smallest node that spans the given range", () => {
tree = parser.parse("x10 + 1000");
describe('.descendantForIndex(min, max)', () => {
it('returns the smallest node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild.firstChild;
assert.equal("identifier", sumNode.descendantForIndex(1, 2).type);
assert.equal("+", sumNode.descendantForIndex(4, 4).type);
assert.equal('identifier', sumNode.descendantForIndex(1, 2).type);
assert.equal('+', sumNode.descendantForIndex(4, 4).type);
assert.throws(() => {
sumNode.descendantForIndex(1, {});
}, "Arguments must be numbers");
}, 'Arguments must be numbers');
assert.throws(() => {
sumNode.descendantForIndex();
}, "Arguments must be numbers");
}, 'Arguments must be numbers');
});
});
describe(".namedDescendantForIndex", () => {
it("returns the smallest node that spans the given range", () => {
tree = parser.parse("x10 + 1000");
describe('.namedDescendantForIndex', () => {
it('returns the smallest node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild;
assert.equal("identifier", sumNode.descendantForIndex(1, 2).type);
assert.equal("+", sumNode.descendantForIndex(4, 4).type);
assert.equal('identifier', sumNode.descendantForIndex(1, 2).type);
assert.equal('+', sumNode.descendantForIndex(4, 4).type);
});
});
describe(".descendantForPosition(min, max)", () => {
it("returns the smallest node that spans the given range", () => {
tree = parser.parse("x10 + 1000");
describe('.descendantForPosition(min, max)', () => {
it('returns the smallest node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild;
assert.equal(
"identifier",
'identifier',
sumNode.descendantForPosition(
{ row: 0, column: 1 },
{ row: 0, column: 2 }
).type
{row: 0, column: 1},
{row: 0, column: 2},
).type,
);
assert.equal(
"+",
sumNode.descendantForPosition({ row: 0, column: 4 }).type
'+',
sumNode.descendantForPosition({row: 0, column: 4}).type,
);
assert.throws(() => {
sumNode.descendantForPosition(1, {});
}, "Arguments must be {row, column} objects");
}, 'Arguments must be {row, column} objects');
assert.throws(() => {
sumNode.descendantForPosition();
}, "Arguments must be {row, column} objects");
}, 'Arguments must be {row, column} objects');
});
});
describe(".namedDescendantForPosition(min, max)", () => {
it("returns the smallest named node that spans the given range", () => {
tree = parser.parse("x10 + 1000");
describe('.namedDescendantForPosition(min, max)', () => {
it('returns the smallest named node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild;
assert.equal(
sumNode.namedDescendantForPosition(
{ row: 0, column: 1 },
{ row: 0, column: 2 }
{row: 0, column: 1},
{row: 0, column: 2},
).type,
"identifier",
'identifier',
);
assert.equal(
sumNode.namedDescendantForPosition({ row: 0, column: 4 }).type,
'binary_expression'
sumNode.namedDescendantForPosition({row: 0, column: 4}).type,
'binary_expression',
);
});
});
describe(".hasError()", () => {
it("returns true if the node contains an error", () => {
tree = parser.parse("1 + 2 * * 3");
describe('.hasError()', () => {
it('returns true if the node contains an error', () => {
tree = parser.parse('1 + 2 * * 3');
const node = tree.rootNode;
assert.equal(
node.toString(),
'(program (expression_statement (binary_expression left: (number) right: (binary_expression left: (number) (ERROR) right: (number)))))'
'(program (expression_statement (binary_expression left: (number) right: (binary_expression left: (number) (ERROR) right: (number)))))',
);
const sum = node.firstChild.firstChild;
@ -268,13 +268,13 @@ describe("Node", () => {
});
});
describe(".isError()", () => {
it("returns true if the node is an error", () => {
tree = parser.parse("2 * * 3");
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))))'
'(program (expression_statement (binary_expression left: (number) (ERROR) right: (number))))',
);
const multi = node.firstChild.firstChild;
@ -286,17 +286,17 @@ describe("Node", () => {
});
});
describe(".isMissing()", () => {
it("returns true if the node is missing from the source and was inserted via error recovery", () => {
tree = parser.parse("(2 ||)");
describe('.isMissing()', () => {
it('returns true if the node is missing from the source and was inserted via error recovery', () => {
tree = parser.parse('(2 ||)');
const node = tree.rootNode;
assert.equal(
node.toString(),
"(program (expression_statement (parenthesized_expression (binary_expression left: (number) right: (MISSING identifier)))))"
'(program (expression_statement (parenthesized_expression (binary_expression left: (number) right: (MISSING identifier)))))',
);
const sum = node.firstChild.firstChild.firstNamedChild;
assert.equal(sum.type, 'binary_expression')
assert.equal(sum.type, 'binary_expression');
assert(sum.hasError());
assert(!sum.children[0].isMissing());
assert(!sum.children[1].isMissing());
@ -304,33 +304,33 @@ describe("Node", () => {
});
});
describe(".text", () => {
const text = "α0 / b👎c👎";
describe('.text', () => {
const text = 'α0 / b👎c👎';
Object.entries({
'.parse(String)': text,
'.parse(Function)': offset => text.substr(offset, 4)
'.parse(Function)': (offset) => text.substr(offset, 4),
}).forEach(([method, parse]) =>
it(`returns the text of a node generated by ${method}`, async () => {
const [numeratorSrc, denominatorSrc] = text.split(/\s*\/\s+/)
tree = await parser.parse(text)
const [numeratorSrc, denominatorSrc] = text.split(/\s*\/\s+/);
tree = await parser.parse(text);
const quotientNode = tree.rootNode.firstChild.firstChild;
const [numerator, slash, denominator] = quotientNode.children;
assert.equal(text, tree.rootNode.text, 'root node text');
assert.equal(text, tree.rootNode.text, 'root node text');
assert.equal(denominatorSrc, denominator.text, 'denominator text');
assert.equal(text, quotientNode.text, 'quotient text');
assert.equal(numeratorSrc, numerator.text, 'numerator text');
assert.equal('/', slash.text, '"/" text');
})
assert.equal(text, quotientNode.text, 'quotient text');
assert.equal(numeratorSrc, numerator.text, 'numerator text');
assert.equal('/', slash.text, '"/" text');
}),
);
});
describe(".parseState, .nextParseState", () => {
const text = "10 / 5";
describe('.parseState, .nextParseState', () => {
const text = '10 / 5';
it("returns node parse state ids", async () => {
tree = await parser.parse(text)
it('returns node parse state ids', async () => {
tree = await parser.parse(text);
const quotientNode = tree.rootNode.firstChild.firstChild;
const [numerator, slash, denominator] = quotientNode.children;
@ -340,15 +340,15 @@ describe("Node", () => {
assert.isAbove(numerator.parseState, 0);
assert.isAbove(slash.parseState, 0);
assert.isAbove(denominator.parseState, 0);
})
});
it("returns next parse state equal to the language", async () => {
it('returns next parse state equal to the language', async () => {
tree = await parser.parse(text);
const quotientNode = tree.rootNode.firstChild.firstChild;
quotientNode.children.forEach(node => {
quotientNode.children.forEach((node) => {
assert.equal(
node.nextParseState,
JavaScript.nextState(node.parseState, node.grammarId)
JavaScript.nextState(node.parseState, node.grammarId),
);
});
});
@ -356,82 +356,82 @@ describe("Node", () => {
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");
tree = parser.parse('a + 1 * b * 2 + c + 3');
const outerSum = tree.rootNode.firstChild.firstChild;
let descendants = outerSum.descendantsOfType('number', {row: 0, column: 2}, {row: 0, column: 15})
let descendants = outerSum.descendantsOfType('number', {row: 0, column: 2}, {row: 0, column: 15});
assert.deepEqual(
descendants.map(node => node.startIndex),
[4, 12]
descendants.map((node) => node.startIndex),
[4, 12],
);
assert.deepEqual(
descendants.map(node => node.endPosition),
[{row: 0, column: 5}, {row: 0, column: 13}]
descendants.map((node) => node.endPosition),
[{row: 0, column: 5}, {row: 0, column: 13}],
);
descendants = outerSum.descendantsOfType('identifier', {row: 0, column: 2}, {row: 0, column: 15})
descendants = outerSum.descendantsOfType('identifier', {row: 0, column: 2}, {row: 0, column: 15});
assert.deepEqual(
descendants.map(node => node.startIndex),
[8]
descendants.map((node) => node.startIndex),
[8],
);
descendants = outerSum.descendantsOfType('identifier', {row: 0, column: 0}, {row: 0, column: 30})
descendants = outerSum.descendantsOfType('identifier', {row: 0, column: 0}, {row: 0, column: 30});
assert.deepEqual(
descendants.map(node => node.startIndex),
[0, 8, 16]
descendants.map((node) => node.startIndex),
[0, 8, 16],
);
descendants = outerSum.descendantsOfType('number', {row: 0, column: 0}, {row: 0, column: 30})
descendants = outerSum.descendantsOfType('number', {row: 0, column: 0}, {row: 0, column: 30});
assert.deepEqual(
descendants.map(node => node.startIndex),
[4, 12, 20]
descendants.map((node) => node.startIndex),
[4, 12, 20],
);
descendants = outerSum.descendantsOfType(
['identifier', 'number'],
{row: 0, column: 0},
{row: 0, column: 30}
)
{row: 0, column: 30},
);
assert.deepEqual(
descendants.map(node => node.startIndex),
[0, 4, 8, 12, 16, 20]
descendants.map((node) => node.startIndex),
[0, 4, 8, 12, 16, 20],
);
descendants = outerSum.descendantsOfType('number')
descendants = outerSum.descendantsOfType('number');
assert.deepEqual(
descendants.map(node => node.startIndex),
[4, 12, 20]
descendants.map((node) => node.startIndex),
[4, 12, 20],
);
descendants = outerSum.firstChild.descendantsOfType('number', {row: 0, column: 0}, {row: 0, column: 30})
descendants = outerSum.firstChild.descendantsOfType('number', {row: 0, column: 0}, {row: 0, column: 30});
assert.deepEqual(
descendants.map(node => node.startIndex),
[4, 12]
descendants.map((node) => node.startIndex),
[4, 12],
);
})
});
});
describe.skip('.closest(type)', () => {
it('returns the closest ancestor of the given type', () => {
tree = parser.parse("a(b + -d.e)");
const property = tree.rootNode.descendantForIndex("a(b + -d.".length);
tree = parser.parse('a(b + -d.e)');
const property = tree.rootNode.descendantForIndex('a(b + -d.'.length);
assert.equal(property.type, 'property_identifier');
const unary = property.closest('unary_expression')
assert.equal(unary.type, 'unary_expression')
assert.equal(unary.startIndex, 'a(b + '.length)
assert.equal(unary.endIndex, 'a(b + -d.e'.length)
const unary = property.closest('unary_expression');
assert.equal(unary.type, 'unary_expression');
assert.equal(unary.startIndex, 'a(b + '.length);
assert.equal(unary.endIndex, 'a(b + -d.e'.length);
const sum = property.closest(['binary_expression', 'call_expression'])
assert.equal(sum.type, 'binary_expression')
assert.equal(sum.startIndex, 2)
assert.equal(sum.endIndex, 'a(b + -d.e'.length)
const sum = property.closest(['binary_expression', 'call_expression']);
assert.equal(sum.type, 'binary_expression');
assert.equal(sum.startIndex, 2);
assert.equal(sum.endIndex, 'a(b + -d.e'.length);
});
it('throws an exception when an invalid argument is given', () => {
tree = parser.parse("a + 1 * b * 2 + c + 3");
const number = tree.rootNode.descendantForIndex(4)
tree = parser.parse('a + 1 * b * 2 + c + 3');
const number = tree.rootNode.descendantForIndex(4);
assert.throws(() => number.closest({a: 1}), /Argument must be a string or array of strings/)
assert.throws(() => number.closest({a: 1}), /Argument must be a string or array of strings/);
});
});