feat: add and apply eslint config

This commit is contained in:
Amaan Qureshi 2025-01-16 01:10:54 -05:00
parent 2cae67892e
commit 31ceb99603
23 changed files with 1349 additions and 771 deletions

View file

@ -1,8 +1,8 @@
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
import TSParser, { type Language, type Tree, type SyntaxNode, type Point } from 'web-tree-sitter';
import type { default as ParserType, Language, Tree, SyntaxNode } from 'web-tree-sitter';
import helper from './helper';
let Parser: typeof TSParser;
let Parser: typeof ParserType;
let C: Language;
let JavaScript: Language;
let JSON: Language;
@ -40,7 +40,7 @@ function getAllNodes(tree: Tree): SyntaxNode[] {
}
describe('Node', () => {
let parser: TSParser;
let parser: ParserType;
let tree: Tree | null;
beforeAll(async () => {
@ -220,15 +220,17 @@ describe('Node', () => {
it('returns the smallest node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild!.firstChild;
expect(sumNode!.descendantForIndex(1, 2)!.type).toBe('identifier');
expect(sumNode!.descendantForIndex(4, 4)!.type).toBe('+');
expect(sumNode!.descendantForIndex(1, 2).type).toBe('identifier');
expect(sumNode!.descendantForIndex(4, 4).type).toBe('+');
expect(() => {
sumNode!.descendantForIndex(1, {} as any);
// @ts-expect-error Testing invalid arguments
sumNode!.descendantForIndex(1, {});
}).toThrow('Arguments must be numbers');
expect(() => {
sumNode!.descendantForIndex(undefined as any);
// @ts-expect-error Testing invalid arguments
sumNode!.descendantForIndex(undefined);
}).toThrow('Arguments must be numbers');
});
});
@ -237,8 +239,8 @@ describe('Node', () => {
it('returns the smallest named node that spans the given range', () => {
tree = parser.parse('x10 + 1000');
const sumNode = tree.rootNode.firstChild;
expect(sumNode!.descendantForIndex(1, 2)!.type).toBe('identifier');
expect(sumNode!.descendantForIndex(4, 4)!.type).toBe('+');
expect(sumNode!.descendantForIndex(1, 2).type).toBe('identifier');
expect(sumNode!.descendantForIndex(4, 4).type).toBe('+');
});
});
@ -251,19 +253,21 @@ describe('Node', () => {
sumNode!.descendantForPosition(
{ row: 0, column: 1 },
{ row: 0, column: 2 }
)!.type
).type
).toBe('identifier');
expect(
sumNode!.descendantForPosition({ row: 0, column: 4 })!.type
sumNode!.descendantForPosition({ row: 0, column: 4 }).type
).toBe('+');
expect(() => {
sumNode!.descendantForPosition(1 as any, {} as any);
// @ts-expect-error Testing invalid arguments
sumNode!.descendantForPosition(1, {});
}).toThrow('Arguments must be {row, column} objects');
expect(() => {
sumNode!.descendantForPosition(undefined as any);
// @ts-expect-error Testing invalid arguments
sumNode!.descendantForPosition(undefined);
}).toThrow('Arguments must be {row, column} objects');
});
});
@ -343,9 +347,9 @@ describe('Node', () => {
const commentNode = node.descendantForIndex(7, 7);
expect(node.type).toBe('program');
expect(commentNode!.type).toBe('comment');
expect(commentNode.type).toBe('comment');
expect(node.isExtra).toBe(false);
expect(commentNode!.isExtra).toBe(true);
expect(commentNode.isExtra).toBe(true);
});
});
@ -355,20 +359,20 @@ describe('Node', () => {
Object.entries({
'.parse(String)': text,
'.parse(Function)': (offset: number) => text.slice(offset, offset + 4),
}).forEach(([method, _parse]) =>
it(`returns the text of a node generated by ${method}`, async () => {
}).forEach(([method, _parse]) => {
it(`returns the text of a node generated by ${method}`, () => {
const [numeratorSrc, denominatorSrc] = text.split(/\s*\/\s+/);
tree = parser.parse(_parse);
const quotientNode = tree.rootNode!.firstChild!.firstChild!;
const quotientNode = tree.rootNode.firstChild!.firstChild!;
const [numerator, slash, denominator] = quotientNode.children;
expect(tree.rootNode.text).toBe(text);
expect(denominator.text).toBe(denominatorSrc);
expect(quotientNode!.text).toBe(text);
expect(quotientNode.text).toBe(text);
expect(numerator.text).toBe(numeratorSrc);
expect(slash.text).toBe('/');
}),
);
});
});
});
describe('.descendantCount', () => {
@ -475,7 +479,7 @@ describe('Node', () => {
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 });
const descendants = outerSum!.descendantsOfType('number', { row: 0, column: 2 }, { row: 0, column: 15 });
expect(descendants.map(node => node.startIndex)).toEqual([4, 12]);
expect(descendants.map(node => node.endPosition)).toEqual([
{ row: 0, column: 5 },