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

@ -13,12 +13,14 @@ const PREDICATE_STEP_TYPE_STRING = 2;
const LANGUAGE_FUNCTION_REGEX = /^_?tree_sitter_\w+/;
var VERSION;
var MIN_COMPATIBLE_VERSION;
var TRANSFER_BUFFER;
var currentParseCallback;
var currentLogCallback;
let VERSION;
let MIN_COMPATIBLE_VERSION;
let TRANSFER_BUFFER;
let currentParseCallback;
// eslint-disable-next-line no-unused-vars
let currentLogCallback;
// eslint-disable-next-line no-unused-vars
class ParserImpl {
static init() {
TRANSFER_BUFFER = C._ts_init();
@ -50,7 +52,7 @@ class ParserImpl {
if (version < MIN_COMPATIBLE_VERSION || VERSION < version) {
throw new Error(
`Incompatible language version ${version}. ` +
`Compatibility range ${MIN_COMPATIBLE_VERSION} through ${VERSION}.`
`Compatibility range ${MIN_COMPATIBLE_VERSION} through ${VERSION}.`,
);
}
} else {
@ -62,7 +64,7 @@ class ParserImpl {
}
getLanguage() {
return this.language
return this.language;
}
parse(callback, oldTree, options) {
@ -71,7 +73,7 @@ class ParserImpl {
} else if (typeof callback === 'function') {
currentParseCallback = callback;
} else {
throw new Error("Argument must be a string or a function");
throw new Error('Argument must be a string or a function');
}
if (this.logCallback) {
@ -99,7 +101,7 @@ class ParserImpl {
this[1],
oldTree ? oldTree[0] : 0,
rangeAddress,
rangeCount
rangeCount,
);
if (!treeAddress) {
@ -129,8 +131,8 @@ class ParserImpl {
setLogger(callback) {
if (!callback) {
callback = null;
} else if (typeof callback !== "function") {
throw new Error("Logger callback must be a function");
} else if (typeof callback !== 'function') {
throw new Error('Logger callback must be a function');
}
this.logCallback = callback;
return this;
@ -403,7 +405,7 @@ class Node {
startPosition.row,
startPosition.column,
endPosition.row,
endPosition.column
endPosition.column,
);
// Instantiate the nodes based on the data returned.
@ -460,7 +462,7 @@ class Node {
}
marshalNode(this);
let address = TRANSFER_BUFFER + SIZE_OF_NODE;
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
setValue(address, start, 'i32');
setValue(address + SIZE_OF_INT, end, 'i32');
C._ts_node_descendant_for_index_wasm(this.tree[0]);
@ -473,7 +475,7 @@ class Node {
}
marshalNode(this);
let address = TRANSFER_BUFFER + SIZE_OF_NODE;
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
setValue(address, start, 'i32');
setValue(address + SIZE_OF_INT, end, 'i32');
C._ts_node_named_descendant_for_index_wasm(this.tree[0]);
@ -486,7 +488,7 @@ class Node {
}
marshalNode(this);
let address = TRANSFER_BUFFER + SIZE_OF_NODE;
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
marshalPoint(address, start);
marshalPoint(address + SIZE_OF_POINT, end);
C._ts_node_descendant_for_position_wasm(this.tree[0]);
@ -499,7 +501,7 @@ class Node {
}
marshalNode(this);
let address = TRANSFER_BUFFER + SIZE_OF_NODE;
const address = TRANSFER_BUFFER + SIZE_OF_NODE;
marshalPoint(address, start);
marshalPoint(address + SIZE_OF_POINT, end);
C._ts_node_named_descendant_for_position_wasm(this.tree[0]);
@ -747,7 +749,7 @@ class Language {
sourceAddress,
sourceLength,
TRANSFER_BUFFER,
TRANSFER_BUFFER + SIZE_OF_INT
TRANSFER_BUFFER + SIZE_OF_INT,
);
if (!address) {
@ -769,11 +771,11 @@ class Language {
break;
case 5:
error = new TypeError(`Bad pattern structure at offset ${errorIndex}: '${suffix}'...`);
word = "";
word = '';
break;
default:
error = new SyntaxError(`Bad syntax at offset ${errorIndex}: '${suffix}'...`);
word = "";
word = '';
break;
}
error.index = errorIndex;
@ -792,7 +794,7 @@ class Language {
const nameAddress = C._ts_query_capture_name_for_id(
address,
i,
TRANSFER_BUFFER
TRANSFER_BUFFER,
);
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
captureNames[i] = UTF8ToString(nameAddress, nameLength);
@ -802,7 +804,7 @@ class Language {
const valueAddress = C._ts_query_string_value_for_id(
address,
i,
TRANSFER_BUFFER
TRANSFER_BUFFER,
);
const nameLength = getValue(TRANSFER_BUFFER, 'i32');
stringValues[i] = UTF8ToString(valueAddress, nameLength);
@ -817,7 +819,7 @@ class Language {
const predicatesAddress = C._ts_query_predicates_for_pattern(
address,
i,
TRANSFER_BUFFER
TRANSFER_BUFFER,
);
const stepCount = getValue(TRANSFER_BUFFER, 'i32');
@ -849,103 +851,121 @@ class Language {
isPositive = false;
case 'any-eq?':
case 'eq?':
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}`
);
if (steps[1].type !== 'capture') throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}"`
);
if (steps.length !== 3) {
throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}`,
);
}
if (steps[1].type !== 'capture') {
throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}"`,
);
}
matchAll = !operator.startsWith('any-');
if (steps[2].type === 'capture') {
const captureName1 = steps[1].name;
const captureName2 = steps[2].name;
textPredicates[i].push(function (captures) {
let nodes_1 = [];
let nodes_2 = [];
textPredicates[i].push(function(captures) {
const nodes1 = [];
const nodes2 = [];
for (const c of captures) {
if (c.name === captureName1) nodes_1.push(c.node);
if (c.name === captureName2) nodes_2.push(c.node);
if (c.name === captureName1) nodes1.push(c.node);
if (c.name === captureName2) nodes2.push(c.node);
}
let compare = (n1, n2, positive) => {
const compare = (n1, n2, positive) => {
return positive ?
n1.text === n2.text :
n1.text !== n2.text;
};
return matchAll
? nodes_1.every(n1 => nodes_2.some(n2 => compare(n1, n2, isPositive)))
: nodes_1.some(n1 => nodes_2.some(n2 => compare(n1, n2, isPositive)));
return matchAll ?
nodes1.every((n1) => nodes2.some((n2) => compare(n1, n2, isPositive))) :
nodes1.some((n1) => nodes2.some((n2) => compare(n1, n2, isPositive)));
});
} else {
captureName = steps[1].name;
const stringValue = steps[2].value;
let matches = (n) => n.text === stringValue;
let doesNotMatch = (n) => n.text !== stringValue;
textPredicates[i].push(function (captures) {
let nodes = [];
const matches = (n) => n.text === stringValue;
const doesNotMatch = (n) => n.text !== stringValue;
textPredicates[i].push(function(captures) {
const nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node);
}
let test = isPositive ? matches : doesNotMatch;
return matchAll
? nodes.every(test)
: nodes.some(test);
const test = isPositive ? matches : doesNotMatch;
return matchAll ?
nodes.every(test) :
nodes.some(test);
});
}
break;
case "any-not-match?":
case "not-match?":
isPositive = false;
case "any-match?":
case "match?":
if (steps.length !== 3) throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}.`
);
if (steps[1].type !== 'capture') throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}".`
);
if (steps[2].type !== 'string') throw new Error(
`Second argument of \`#${operator}\` predicate must be a string. Got @${steps[2].value}.`
);
case 'any-not-match?':
case 'not-match?':
isPositive = false;
case 'any-match?':
case 'match?':
if (steps.length !== 3) {
throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 2, got ${steps.length - 1}.`,
);
}
if (steps[1].type !== 'capture') {
throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}".`,
);
}
if (steps[2].type !== 'string') {
throw new Error(
`Second argument of \`#${operator}\` predicate must be a string. Got @${steps[2].value}.`,
);
}
captureName = steps[1].name;
const regex = new RegExp(steps[2].value);
matchAll = !operator.startsWith('any-');
textPredicates[i].push(function (captures) {
textPredicates[i].push(function(captures) {
const nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node.text);
}
let test = (text, positive) => {
const test = (text, positive) => {
return positive ?
regex.test(text) :
!regex.test(text);
};
if (nodes.length === 0) return !isPositive;
return matchAll
? nodes.every(text => test(text, isPositive))
: nodes.some(text => test(text, isPositive))
return matchAll ?
nodes.every((text) => test(text, isPositive)) :
nodes.some((text) => test(text, isPositive));
});
break;
case 'set!':
if (steps.length < 2 || steps.length > 3) throw new Error(
`Wrong number of arguments to \`#set!\` predicate. Expected 1 or 2. Got ${steps.length - 1}.`
);
if (steps.some(s => s.type !== 'string')) throw new Error(
`Arguments to \`#set!\` predicate must be a strings.".`
);
if (steps.length < 2 || steps.length > 3) {
throw new Error(
`Wrong number of arguments to \`#set!\` predicate. Expected 1 or 2. Got ${steps.length - 1}.`,
);
}
if (steps.some((s) => s.type !== 'string')) {
throw new Error(
`Arguments to \`#set!\` predicate must be a strings.".`,
);
}
if (!setProperties[i]) setProperties[i] = {};
setProperties[i][steps[1].value] = steps[2] ? steps[2].value : null;
break;
case 'is?':
case 'is-not?':
if (steps.length < 2 || steps.length > 3) throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 1 or 2. Got ${steps.length - 1}.`
);
if (steps.some(s => s.type !== 'string')) throw new Error(
`Arguments to \`#${operator}\` predicate must be a strings.".`
);
if (steps.length < 2 || steps.length > 3) {
throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected 1 or 2. Got ${steps.length - 1}.`,
);
}
if (steps.some((s) => s.type !== 'string')) {
throw new Error(
`Arguments to \`#${operator}\` predicate must be a strings.".`,
);
}
const properties = operator === 'is?' ? assertedProperties : refutedProperties;
if (!properties[i]) properties[i] = {};
properties[i][steps[1].value] = steps[2] ? steps[2].value : null;
@ -954,26 +974,32 @@ class Language {
case 'not-any-of?':
isPositive = false;
case 'any-of?':
if (steps.length < 2) throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected at least 1. Got ${steps.length - 1}.`
);
if (steps[1].type !== 'capture') throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}".`
);
for (let i = 2; i < steps.length; i++) {
if (steps[i].type !== 'string') throw new Error(
`Arguments to \`#${operator}\` predicate must be a strings.".`
if (steps.length < 2) {
throw new Error(
`Wrong number of arguments to \`#${operator}\` predicate. Expected at least 1. Got ${steps.length - 1}.`,
);
}
if (steps[1].type !== 'capture') {
throw new Error(
`First argument of \`#${operator}\` predicate must be a capture. Got "${steps[1].value}".`,
);
}
for (let i = 2; i < steps.length; i++) {
if (steps[i].type !== 'string') {
throw new Error(
`Arguments to \`#${operator}\` predicate must be a strings.".`,
);
}
}
captureName = steps[1].name;
const values = steps.slice(2).map(s => s.value);
textPredicates[i].push(function (captures) {
const values = steps.slice(2).map((s) => s.value);
textPredicates[i].push(function(captures) {
const nodes = [];
for (const c of captures) {
if (c.name === captureName) nodes.push(c.node.text);
}
if (nodes.length === 0) return !isPositive;
return nodes.every(text => values.includes(text)) === isPositive;
return nodes.every((text) => values.includes(text)) === isPositive;
});
break;
@ -999,7 +1025,7 @@ class Language {
predicates,
Object.freeze(setProperties),
Object.freeze(assertedProperties),
Object.freeze(refutedProperties)
Object.freeze(refutedProperties),
);
}
@ -1018,28 +1044,28 @@ class Language {
bytes = Promise.resolve(fs.readFileSync(url));
} else {
bytes = fetch(url)
.then(response => response.arrayBuffer()
.then(buffer => {
.then((response) => response.arrayBuffer()
.then((buffer) => {
if (response.ok) {
return new Uint8Array(buffer);
} else {
const body = new TextDecoder('utf-8').decode(buffer);
throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`)
throw new Error(`Language.load failed with status ${response.status}.\n\n${body}`);
}
}));
}
}
return bytes
.then(bytes => loadWebAssemblyModule(bytes, {loadAsync: true}))
.then(mod => {
const symbolNames = Object.keys(mod)
const functionName = symbolNames.find(key =>
.then((bytes) => loadWebAssemblyModule(bytes, {loadAsync: true}))
.then((mod) => {
const symbolNames = Object.keys(mod);
const functionName = symbolNames.find((key) =>
LANGUAGE_FUNCTION_REGEX.test(key) &&
!key.includes("external_scanner_")
!key.includes('external_scanner_'),
);
if (!functionName) {
console.log(`Couldn't find language function in WASM file. Symbols:\n${JSON.stringify(symbolNames, null, 2)}`)
console.log(`Couldn't find language function in WASM file. Symbols:\n${JSON.stringify(symbolNames, null, 2)}`);
}
const languageAddress = mod[functionName]();
return new Language(INTERNAL, languageAddress);
@ -1059,7 +1085,7 @@ class LookaheadIterable {
}
get currentType() {
return this.language.types[this.currentTypeId] || 'ERROR'
return this.language.types[this.currentTypeId] || 'ERROR';
}
delete() {
@ -1085,11 +1111,11 @@ class LookaheadIterable {
return {
next() {
if (C._ts_lookahead_iterator_next(self[0])) {
return { done: false, value: self.currentType };
return {done: false, value: self.currentType};
}
return { done: true, value: "" };
}
return {done: true, value: ''};
},
};
}
}
@ -1097,7 +1123,7 @@ class LookaheadIterable {
class Query {
constructor(
internal, address, captureNames, textPredicates, predicates,
setProperties, assertedProperties, refutedProperties
setProperties, assertedProperties, refutedProperties,
) {
assertInternal(internal);
this[0] = address;
@ -1136,7 +1162,7 @@ class Query {
startPosition.column,
endPosition.row,
endPosition.column,
matchLimit
matchLimit,
);
const rawCount = getValue(TRANSFER_BUFFER, 'i32');
@ -1155,7 +1181,7 @@ class Query {
const captures = new Array(captureCount);
address = unmarshalCaptures(this, node.tree, address, captures);
if (this.textPredicates[pattern].every(p => p(captures))) {
if (this.textPredicates[pattern].every((p) => p(captures))) {
result[filteredCount++] = {pattern, captures};
const setProperties = this.setProperties[pattern];
if (setProperties) result[i].setProperties = setProperties;
@ -1192,7 +1218,7 @@ class Query {
startPosition.column,
endPosition.row,
endPosition.column,
matchLimit
matchLimit,
);
const count = getValue(TRANSFER_BUFFER, 'i32');
@ -1211,10 +1237,10 @@ class Query {
const captureIndex = getValue(address, 'i32');
address += SIZE_OF_INT;
captures.length = captureCount
captures.length = captureCount;
address = unmarshalCaptures(this, node.tree, address, captures);
if (this.textPredicates[pattern].every(p => p(captures))) {
if (this.textPredicates[pattern].every((p) => p(captures))) {
const capture = captures[captureIndex];
const setProperties = this.setProperties[pattern];
if (setProperties) capture.setProperties = setProperties;
@ -1231,7 +1257,7 @@ class Query {
}
predicatesForPattern(patternIndex) {
return this.predicates[patternIndex]
return this.predicates[patternIndex];
}
didExceedMatchLimit() {
@ -1270,7 +1296,7 @@ function unmarshalCaptures(query, tree, address, result) {
}
function assertInternal(x) {
if (x !== INTERNAL) throw new Error('Illegal constructor')
if (x !== INTERNAL) throw new Error('Illegal constructor');
}
function isPoint(point) {
@ -1319,25 +1345,25 @@ function unmarshalNode(tree, address = TRANSFER_BUFFER) {
function marshalTreeCursor(cursor, address = TRANSFER_BUFFER) {
setValue(address + 0 * SIZE_OF_INT, cursor[0], 'i32'),
setValue(address + 1 * SIZE_OF_INT, cursor[1], 'i32'),
setValue(address + 2 * SIZE_OF_INT, cursor[2], 'i32')
setValue(address + 2 * SIZE_OF_INT, cursor[2], 'i32');
}
function unmarshalTreeCursor(cursor) {
cursor[0] = getValue(TRANSFER_BUFFER + 0 * SIZE_OF_INT, 'i32'),
cursor[1] = getValue(TRANSFER_BUFFER + 1 * SIZE_OF_INT, 'i32'),
cursor[2] = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32')
cursor[2] = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32');
}
function marshalPoint(address, point) {
setValue(address, point.row, 'i32')
setValue(address + SIZE_OF_INT, point.column, 'i32')
setValue(address, point.row, 'i32');
setValue(address + SIZE_OF_INT, point.column, 'i32');
}
function unmarshalPoint(address) {
return {
row: getValue(address, 'i32'),
column: getValue(address + SIZE_OF_INT, 'i32')
}
column: getValue(address + SIZE_OF_INT, 'i32'),
};
}
function marshalRange(address, range) {