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,10 +1,10 @@
const { assert } = require("chai");
let Parser, JavaScript;
const {assert} = require('chai');
let Parser; let JavaScript;
describe("Query", () => {
let parser, tree, query;
describe('Query', () => {
let parser; let tree; let query;
before(async () => ({ Parser, JavaScript } = await require("./helper")));
before(async () => ({Parser, JavaScript} = await require('./helper')));
beforeEach(() => {
parser = new Parser().setLanguage(JavaScript);
@ -16,73 +16,73 @@ describe("Query", () => {
if (query) query.delete();
});
describe("construction", () => {
it("throws an error on invalid patterns", () => {
describe('construction', () => {
it('throws an error on invalid patterns', () => {
assert.throws(() => {
JavaScript.query("(function_declaration wat)");
}, "Bad syntax at offset 22: 'wat)'...");
JavaScript.query('(function_declaration wat)');
}, 'Bad syntax at offset 22: \'wat)\'...');
assert.throws(() => {
JavaScript.query("(non_existent)");
}, "Bad node name 'non_existent'");
JavaScript.query('(non_existent)');
}, 'Bad node name \'non_existent\'');
assert.throws(() => {
JavaScript.query("(a)");
}, "Bad node name 'a'");
JavaScript.query('(a)');
}, 'Bad node name \'a\'');
assert.throws(() => {
JavaScript.query("(function_declaration non_existent:(identifier))");
}, "Bad field name 'non_existent'");
JavaScript.query('(function_declaration non_existent:(identifier))');
}, 'Bad field name \'non_existent\'');
assert.throws(() => {
JavaScript.query("(function_declaration name:(statement_block))");
}, "Bad pattern structure at offset 22: 'name:(statement_block))'");
JavaScript.query('(function_declaration name:(statement_block))');
}, 'Bad pattern structure at offset 22: \'name:(statement_block))\'');
});
it("throws an error on invalid predicates", () => {
it('throws an error on invalid predicates', () => {
assert.throws(() => {
JavaScript.query("((identifier) @abc (#eq? @ab hi))");
}, "Bad capture name @ab");
JavaScript.query('((identifier) @abc (#eq? @ab hi))');
}, 'Bad capture name @ab');
assert.throws(() => {
JavaScript.query("((identifier) @abc (#eq? @ab hi))");
}, "Bad capture name @ab");
JavaScript.query('((identifier) @abc (#eq? @ab hi))');
}, 'Bad capture name @ab');
assert.throws(() => {
JavaScript.query("((identifier) @abc (#eq?))");
}, "Wrong number of arguments to `#eq?` predicate. Expected 2, got 0");
JavaScript.query('((identifier) @abc (#eq?))');
}, 'Wrong number of arguments to `#eq?` predicate. Expected 2, got 0');
assert.throws(() => {
JavaScript.query("((identifier) @a (eq? @a @a @a))");
}, "Wrong number of arguments to `#eq?` predicate. Expected 2, got 3");
JavaScript.query('((identifier) @a (eq? @a @a @a))');
}, 'Wrong number of arguments to `#eq?` predicate. Expected 2, got 3');
});
});
describe(".matches", () => {
it("returns all of the matches for the given query", () => {
tree = parser.parse("function one() { two(); function three() {} }");
describe('.matches', () => {
it('returns all of the matches for the given query', () => {
tree = parser.parse('function one() { two(); function three() {} }');
query = JavaScript.query(`
(function_declaration name: (identifier) @fn-def)
(call_expression function: (identifier) @fn-ref)
`);
const matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(matches), [
{ pattern: 0, captures: [{ name: "fn-def", text: "one" }] },
{ pattern: 1, captures: [{ name: "fn-ref", text: "two" }] },
{ pattern: 0, captures: [{ name: "fn-def", text: "three" }] },
{pattern: 0, captures: [{name: 'fn-def', text: 'one'}]},
{pattern: 1, captures: [{name: 'fn-ref', text: 'two'}]},
{pattern: 0, captures: [{name: 'fn-def', text: 'three'}]},
]);
});
it("can search in a specified ranges", () => {
tree = parser.parse("[a, b,\nc, d,\ne, f,\ng, h]");
query = JavaScript.query("(identifier) @element");
it('can search in a specified ranges', () => {
tree = parser.parse('[a, b,\nc, d,\ne, f,\ng, h]');
query = JavaScript.query('(identifier) @element');
const matches = query.matches(
tree.rootNode,
{ row: 1, column: 1 },
{ row: 3, column: 1 }
{row: 1, column: 1},
{row: 3, column: 1},
);
assert.deepEqual(formatMatches(matches), [
{ pattern: 0, captures: [{ name: "element", text: "d" }] },
{ pattern: 0, captures: [{ name: "element", text: "e" }] },
{ pattern: 0, captures: [{ name: "element", text: "f" }] },
{ pattern: 0, captures: [{ name: "element", text: "g" }] },
{pattern: 0, captures: [{name: 'element', text: 'd'}]},
{pattern: 0, captures: [{name: 'element', text: 'e'}]},
{pattern: 0, captures: [{name: 'element', text: 'f'}]},
{pattern: 0, captures: [{name: 'element', text: 'g'}]},
]);
});
it("handles predicates that compare the text of capture to literal strings", () => {
it('handles predicates that compare the text of capture to literal strings', () => {
tree = parser.parse(`
giraffe(1, 2, []);
helment([false]);
@ -103,14 +103,14 @@ describe("Query", () => {
const matches = query.matches(tree.rootNode);
assert.deepEqual(formatMatches(matches), [
{ pattern: 0, captures: [{ name: "name", text: "giraffe" }] },
{ pattern: 0, captures: [{ name: "name", text: "gross" }] },
{pattern: 0, captures: [{name: 'name', text: 'giraffe'}]},
{pattern: 0, captures: [{name: 'name', text: 'gross'}]},
]);
});
});
describe(".captures", () => {
it("returns all of the captures for the given query, in order", () => {
describe('.captures', () => {
it('returns all of the captures for the given query, in order', () => {
tree = parser.parse(`
a({
bc: function de() {
@ -138,22 +138,22 @@ describe("Query", () => {
const captures = query.captures(tree.rootNode);
assert.deepEqual(formatCaptures(captures), [
{ name: "method.def", text: "bc" },
{ name: "delimiter", text: ":" },
{ name: "method.alias", text: "de" },
{ name: "function.def", text: "fg" },
{ name: "operator", text: "=" },
{ name: "function.alias", text: "hi" },
{ name: "method.def", text: "jk" },
{ name: "delimiter", text: ":" },
{ name: "method.alias", text: "lm" },
{ name: "function.def", text: "no" },
{ name: "operator", text: "=" },
{ name: "function.alias", text: "pq" },
{name: 'method.def', text: 'bc'},
{name: 'delimiter', text: ':'},
{name: 'method.alias', text: 'de'},
{name: 'function.def', text: 'fg'},
{name: 'operator', text: '='},
{name: 'function.alias', text: 'hi'},
{name: 'method.def', text: 'jk'},
{name: 'delimiter', text: ':'},
{name: 'method.alias', text: 'lm'},
{name: 'function.def', text: 'no'},
{name: 'operator', text: '='},
{name: 'function.alias', text: 'pq'},
]);
});
it("handles conditions that compare the text of capture to literal strings", () => {
it('handles conditions that compare the text of capture to literal strings', () => {
tree = parser.parse(`
lambda
panda
@ -179,20 +179,20 @@ describe("Query", () => {
const captures = query.captures(tree.rootNode);
assert.deepEqual(formatCaptures(captures), [
{ name: "variable", text: "panda" },
{ name: "variable", text: "toad" },
{ name: "variable", text: "ab" },
{ name: "variable", text: "require" },
{ name: "function.builtin", text: "require" },
{ name: "variable", text: "Cd" },
{ name: "constructor", text: "Cd" },
{ name: "variable", text: "EF" },
{ name: "constructor", text: "EF" },
{ name: "constant", text: "EF" },
{name: 'variable', text: 'panda'},
{name: 'variable', text: 'toad'},
{name: 'variable', text: 'ab'},
{name: 'variable', text: 'require'},
{name: 'function.builtin', text: 'require'},
{name: 'variable', text: 'Cd'},
{name: 'constructor', text: 'Cd'},
{name: 'variable', text: 'EF'},
{name: 'constructor', text: 'EF'},
{name: 'constant', text: 'EF'},
]);
});
it("handles conditions that compare the text of capture to each other", () => {
it('handles conditions that compare the text of capture to each other', () => {
tree = parser.parse(`
ab = abc + 1;
def = de + 1;
@ -211,12 +211,12 @@ describe("Query", () => {
const captures = query.captures(tree.rootNode);
assert.deepEqual(formatCaptures(captures), [
{ name: "id1", text: "ghi" },
{ name: "id2", text: "ghi" },
{name: 'id1', text: 'ghi'},
{name: 'id2', text: 'ghi'},
]);
});
it("handles patterns with properties", () => {
it('handles patterns with properties', () => {
tree = parser.parse(`a(b.c);`);
query = JavaScript.query(`
((call_expression (identifier) @func)
@ -230,18 +230,18 @@ describe("Query", () => {
const captures = query.captures(tree.rootNode);
assert.deepEqual(formatCaptures(captures), [
{ name: "func", text: "a", setProperties: { foo: null, bar: "baz" } },
{name: 'func', text: 'a', setProperties: {foo: null, bar: 'baz'}},
{
name: "prop",
text: "c",
assertedProperties: { foo: null },
refutedProperties: { bar: "baz" },
name: 'prop',
text: 'c',
assertedProperties: {foo: null},
refutedProperties: {bar: 'baz'},
},
]);
assert.ok(!query.didExceedMatchLimit());
});
it("detects queries with too many permutations to track", () => {
it('detects queries with too many permutations to track', () => {
tree = parser.parse(`
[
hello, hello, hello, hello, hello, hello, hello, hello, hello, hello,
@ -256,11 +256,11 @@ describe("Query", () => {
(array (identifier) @pre (identifier) @post)
`);
const captures = query.captures(tree.rootNode, null, null, { matchLimit: 32 });
query.captures(tree.rootNode, null, null, {matchLimit: 32});
assert.ok(query.didExceedMatchLimit());
});
it("handles quantified captures properly", () => {
it('handles quantified captures properly', () => {
let captures;
tree = parser.parse(`
@ -276,7 +276,7 @@ describe("Query", () => {
)
`);
let expectCount = (tree, queryText, expectedCount) => {
const expectCount = (tree, queryText, expectedCount) => {
query = JavaScript.query(queryText);
captures = query.captures(tree.rootNode, null, null);
assert.equal(captures.length, expectedCount);
@ -284,68 +284,68 @@ describe("Query", () => {
expectCount(
tree,
` ( (comment)+ @foo (#any-eq? @foo "/// foo") ) `,
3
`((comment)+ @foo (#any-eq? @foo "/// foo"))`,
3,
);
expectCount(
tree,
` ( (comment)+ @foo (#eq? @foo "/// foo") ) `,
0
`((comment)+ @foo (#eq? @foo "/// foo"))`,
0,
);
expectCount(
tree,
` ( (comment)+ @foo (#any-not-eq? @foo "/// foo") ) `,
3
`((comment)+ @foo (#any-not-eq? @foo "/// foo"))`,
3,
);
expectCount(
tree,
` ( (comment)+ @foo (#not-eq? @foo "/// foo") ) `,
0
`((comment)+ @foo (#not-eq? @foo "/// foo"))`,
0,
);
expectCount(
tree,
` ( (comment)+ @foo (#match? @foo "^/// foo") ) `,
0
`((comment)+ @foo (#match? @foo "^/// foo"))`,
0,
);
expectCount(
tree,
` ( (comment)+ @foo (#any-match? @foo "^/// foo") ) `,
3
`((comment)+ @foo (#any-match? @foo "^/// foo"))`,
3,
);
expectCount(
tree,
` ( (comment)+ @foo (#not-match? @foo "^/// foo") ) `,
0
`((comment)+ @foo (#not-match? @foo "^/// foo"))`,
0,
);
expectCount(
tree,
` ( (comment)+ @foo (#not-match? @foo "fsdfsdafdfs") ) `,
3
`((comment)+ @foo (#not-match? @foo "fsdfsdafdfs"))`,
3,
);
expectCount(
tree,
` ( (comment)+ @foo (#any-not-match? @foo "^///") ) `,
0
`((comment)+ @foo (#any-not-match? @foo "^///"))`,
0,
);
expectCount(
tree,
` ( (comment)+ @foo (#any-not-match? @foo "^/// foo") ) `,
3
`((comment)+ @foo (#any-not-match? @foo "^/// foo"))`,
3,
);
})
});
});
describe(".predicatesForPattern(index)", () => {
it("returns all of the predicates as objects", () => {
describe('.predicatesForPattern(index)', () => {
it('returns all of the predicates as objects', () => {
query = JavaScript.query(`
(
(binary_expression
@ -364,26 +364,26 @@ describe("Query", () => {
assert.deepEqual(query.predicatesForPattern(0), [
{
operator: "something?",
operator: 'something?',
operands: [
{ type: "capture", name: "a" },
{ type: "capture", name: "b" },
{type: 'capture', name: 'a'},
{type: 'capture', name: 'b'},
],
},
{
operator: "something-else?",
operator: 'something-else?',
operands: [
{ type: "capture", name: "a" },
{ type: "string", value: "A" },
{ type: "capture", name: "b" },
{ type: "string", value: "B" },
{type: 'capture', name: 'a'},
{type: 'string', value: 'A'},
{type: 'capture', name: 'b'},
{type: 'string', value: 'B'},
],
},
]);
assert.deepEqual(query.predicatesForPattern(1), [
{
operator: "hello!",
operands: [{ type: "capture", name: "c" }],
operator: 'hello!',
operands: [{type: 'capture', name: 'c'}],
},
]);
assert.deepEqual(query.predicatesForPattern(2), []);
@ -392,7 +392,7 @@ describe("Query", () => {
});
function formatMatches(matches) {
return matches.map(({ pattern, captures }) => ({
return matches.map(({pattern, captures}) => ({
pattern,
captures: formatCaptures(captures),
}));