diff --git a/cli/npm/install.js b/cli/npm/install.js index b008947c..c18ae6b5 100755 --- a/cli/npm/install.js +++ b/cli/npm/install.js @@ -40,7 +40,7 @@ const matrix = { // Determine the URL of the file. const platform = matrix.platform[process.platform]; -const arch = platform && platform.arch[process.arch]; +const arch = platform?.arch[process.arch]; if (!platform || !platform.name || !arch || !arch.name) { console.error( @@ -91,7 +91,7 @@ function get(url, callback) { } }; - const proxyEnv = process.env['HTTPS_PROXY'] || process.env['https_proxy']; + const proxyEnv = process.env.HTTPS_PROXY || process.env.https_proxy; if (!proxyEnv) { https.get(url, processResponse); return; diff --git a/cli/src/generate/dsl.js b/cli/src/generate/dsl.js index 581aeddd..7c376c80 100644 --- a/cli/src/generate/dsl.js +++ b/cli/src/generate/dsl.js @@ -23,7 +23,7 @@ function alias(rule, value) { } } - throw new Error('Invalid alias value ' + value); + throw new Error(`Invalid alias value ${value}`); } function blank() { @@ -35,7 +35,7 @@ function blank() { function field(name, rule) { return { type: "FIELD", - name: name, + name, content: normalize(rule) } } @@ -156,7 +156,7 @@ function seq(...elements) { function sym(name) { return { type: "SYMBOL", - name: name + name }; } @@ -201,17 +201,17 @@ function normalize(value) { if (typeof value.type === 'string') { return value; } else { - throw new TypeError("Invalid rule: " + value.toString()); + throw new TypeError(`Invalid rule: ${value}`); } } } function RuleBuilder(ruleMap) { return new Proxy({}, { - get(target, propertyName) { + get(_, propertyName) { const symbol = sym(propertyName); - if (!ruleMap || ruleMap.hasOwnProperty(propertyName)) { + if (!ruleMap || Object.prototype.hasOwnProperty.call(ruleMap, propertyName)) { return symbol; } else { const error = new ReferenceError(`Undefined symbol '${propertyName}'`); @@ -256,10 +256,10 @@ function grammar(baseGrammar, options) { } const ruleMap = {}; - for (const key in options.rules) { + for (const key of Object.keys(options.rules)) { ruleMap[key] = true; } - for (const key in baseGrammar.rules) { + for (const key of Object.keys(baseGrammar.rules)) { ruleMap[key] = true; } for (const external of externals) { @@ -279,16 +279,16 @@ function grammar(baseGrammar, options) { throw new Error("Grammar's 'name' property must not start with a digit and cannot contain non-word characters."); } - let rules = Object.assign({}, baseGrammar.rules); + const rules = Object.assign({}, baseGrammar.rules); if (options.rules) { if (typeof options.rules !== "object") { throw new Error("Grammar's 'rules' property must be an object."); } - for (const ruleName in options.rules) { + for (const ruleName of Object.keys(options.rules)) { const ruleFn = options.rules[ruleName]; if (typeof ruleFn !== "function") { - throw new Error("Grammar rules must all be functions. '" + ruleName + "' rule is not."); + throw new Error(`Grammar rules must all be functions. '${ruleName}' rule is not.`); } rules[ruleName] = normalize(ruleFn.call(ruleBuilder, ruleBuilder, baseGrammar.rules[ruleName])); } @@ -403,7 +403,7 @@ function grammar(baseGrammar, options) { }); } - if (Object.keys(rules).length == 0) { + if (Object.keys(rules).length === 0) { throw new Error("Grammar must have at least one rule."); } diff --git a/docs/assets/js/playground.js b/docs/assets/js/playground.js index 62bff362..5864d979 100644 --- a/docs/assets/js/playground.js +++ b/docs/assets/js/playground.js @@ -1,7 +1,7 @@ let tree; (async () => { - const CAPTURE_REGEX = /@\s*([\w\._-]+)/g; + const CAPTURE_REGEX = /@\s*([\w._-]+)/g; const COLORS_BY_INDEX = [ 'blue', 'chocolate', @@ -18,8 +18,6 @@ let tree; 'sienna', ]; - const scriptURL = document.currentScript.getAttribute('src'); - const codeInput = document.getElementById('code-input'); const languageSelect = document.getElementById('language-select'); const loggingCheckbox = document.getElementById('logging-checkbox'); @@ -102,8 +100,8 @@ let tree; handleQueryChange(); } - async function handleCodeChange(editor, changes) { - const newText = codeEditor.getValue() + '\n'; + async function handleCodeChange(_editor, changes) { + const newText = `${codeEditor.getValue()}\n`; const edits = tree && changes && changes.map(treeEditForEditorChange); const start = performance.now(); @@ -128,9 +126,9 @@ let tree; isRendering++; const cursor = tree.walk(); - let currentRenderCount = parseCount; + const currentRenderCount = parseCount; let row = ''; - let rows = []; + const rows = []; let finishedRow = false; let visitedChildren = false; let indentLevel = 0; @@ -319,7 +317,7 @@ let tree; start.column > end.column ) ) { - let swap = end; + const swap = end; end = start; start = swap; } @@ -445,14 +443,14 @@ let tree; } function debounce(func, wait, immediate) { - var timeout; + let timeout; return function() { - var context = this, args = arguments; - var later = function() { + const context = this, args = arguments; + const later = function() { timeout = null; if (!immediate) func.apply(context, args); }; - var callNow = immediate && !timeout; + const callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); diff --git a/lib/binding_web/binding.js b/lib/binding_web/binding.js index 295f13b2..2b4696c3 100644 --- a/lib/binding_web/binding.js +++ b/lib/binding_web/binding.js @@ -91,7 +91,7 @@ class ParserImpl { let rangeCount = 0; let rangeAddress = 0; - if (options && options.includedRanges) { + if (options?.includedRanges) { rangeCount = options.includedRanges.length; rangeAddress = C._calloc(rangeCount, SIZE_OF_RANGE); let address = rangeAddress; @@ -349,6 +349,7 @@ class Node { childForFieldName(fieldName) { const fieldId = this.tree.language.fields.indexOf(fieldName); if (fieldId !== -1) return this.childForFieldId(fieldId); + return null; } fieldNameForChild(index) { @@ -365,6 +366,7 @@ class Node { childrenForFieldName(fieldName) { const fieldId = this.tree.language.fields.indexOf(fieldName); if (fieldId !== -1 && fieldId !== 0) return this.childrenForFieldId(fieldId); + return []; } childrenForFieldId(fieldId) { @@ -863,6 +865,7 @@ class Language { lookaheadIterator(stateId) { const address = C._ts_lookahead_iterator_new(this[0], stateId); if (address) return new LookaheadIterable(INTERNAL, address, this); + return null; } query(source) { @@ -991,7 +994,7 @@ class Language { if (steps[2].type === 'capture') { const captureName1 = steps[1].name; const captureName2 = steps[2].name; - textPredicates[i].push(function(captures) { + textPredicates[i].push((captures) => { const nodes1 = []; const nodes2 = []; for (const c of captures) { @@ -1012,7 +1015,7 @@ class Language { const stringValue = steps[2].value; const matches = (n) => n.text === stringValue; const doesNotMatch = (n) => n.text !== stringValue; - textPredicates[i].push(function(captures) { + textPredicates[i].push((captures) => { const nodes = []; for (const c of captures) { if (c.name === captureName) nodes.push(c.node); @@ -1048,7 +1051,7 @@ class Language { captureName = steps[1].name; const regex = new RegExp(steps[2].value); matchAll = !operator.startsWith('any-'); - textPredicates[i].push(function(captures) { + textPredicates[i].push((captures) => { const nodes = []; for (const c of captures) { if (c.name === captureName) nodes.push(c.node.text); @@ -1119,7 +1122,7 @@ class Language { } captureName = steps[1].name; const values = steps.slice(2).map((s) => s.value); - textPredicates[i].push(function(captures) { + textPredicates[i].push((captures) => { const nodes = []; for (const c of captures) { if (c.name === captureName) nodes.push(c.node.text); @@ -1301,7 +1304,7 @@ class Query { const startAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32'); const didExceedMatchLimit = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32'); const result = new Array(rawCount); - this.exceededMatchLimit = !!didExceedMatchLimit; + this.exceededMatchLimit = Boolean(didExceedMatchLimit); let filteredCount = 0; let address = startAddress; @@ -1364,7 +1367,7 @@ class Query { const startAddress = getValue(TRANSFER_BUFFER + SIZE_OF_INT, 'i32'); const didExceedMatchLimit = getValue(TRANSFER_BUFFER + 2 * SIZE_OF_INT, 'i32'); const result = []; - this.exceededMatchLimit = !!didExceedMatchLimit; + this.exceededMatchLimit = Boolean(didExceedMatchLimit); const captures = []; let address = startAddress; @@ -1490,15 +1493,15 @@ 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 + 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 + 3 * SIZE_OF_INT, cursor[3], '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[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[3] = getValue(TRANSFER_BUFFER + 3 * SIZE_OF_INT, 'i32'); } diff --git a/lib/binding_web/imports.js b/lib/binding_web/imports.js index b0d1d9f5..dfb65bbe 100644 --- a/lib/binding_web/imports.js +++ b/lib/binding_web/imports.js @@ -1,5 +1,5 @@ mergeInto(LibraryManager.library, { - tree_sitter_parse_callback: function( + tree_sitter_parse_callback( inputBufferAddress, index, row, @@ -7,7 +7,7 @@ mergeInto(LibraryManager.library, { lengthAddress, ) { const INPUT_BUFFER_SIZE = 10 * 1024; - const string = currentParseCallback(index, {row: row, column: column}); + const string = currentParseCallback(index, {row, column}); if (typeof string === 'string') { setValue(lengthAddress, string.length, 'i32'); stringToUTF16(string, inputBufferAddress, INPUT_BUFFER_SIZE); @@ -16,7 +16,7 @@ mergeInto(LibraryManager.library, { } }, - tree_sitter_log_callback: function(isLexMessage, messageAddress) { + tree_sitter_log_callback(isLexMessage, messageAddress) { if (currentLogCallback) { const message = UTF8ToString(messageAddress); currentLogCallback(message, isLexMessage !== 0); diff --git a/lib/binding_web/test/parser-test.js b/lib/binding_web/test/parser-test.js index 4fa42c2b..4c58d020 100644 --- a/lib/binding_web/test/parser-test.js +++ b/lib/binding_web/test/parser-test.js @@ -251,7 +251,7 @@ describe('Parser', () => { it('handles long input strings', () => { const repeatCount = 10000; - const inputString = '[' + '0,'.repeat(repeatCount) + ']'; + const inputString = `[${Array(repeatCount).fill('0').join(',')}]`; tree = parser.parse(inputString); assert.equal(tree.rootNode.type, 'program'); diff --git a/lib/binding_web/test/tree-test.js b/lib/binding_web/test/tree-test.js index a190c982..c9216eb1 100644 --- a/lib/binding_web/test/tree-test.js +++ b/lib/binding_web/test/tree-test.js @@ -244,62 +244,47 @@ describe('Tree', () => { endIndex: 13, }); - { - const copy = tree.walk(); - copy.resetTo(cursor); + const copy = tree.walk(); + copy.resetTo(cursor); - assert(copy.gotoPreviousSibling()); - assertCursorState(copy, { - nodeType: '+', - nodeIsNamed: false, - startPosition: {row: 0, column: 6}, - endPosition: {row: 0, column: 7}, - startIndex: 6, - endIndex: 7, - }); + assert(copy.gotoPreviousSibling()); + assertCursorState(copy, { + nodeType: '+', + nodeIsNamed: false, + startPosition: {row: 0, column: 6}, + endPosition: {row: 0, column: 7}, + startIndex: 6, + endIndex: 7, + }); - assert(copy.gotoPreviousSibling()); - assertCursorState(copy, { - nodeType: 'binary_expression', - nodeIsNamed: true, - startPosition: {row: 0, column: 0}, - endPosition: {row: 0, column: 5}, - startIndex: 0, - endIndex: 5, - }); + assert(copy.gotoPreviousSibling()); + assertCursorState(copy, { + nodeType: 'binary_expression', + nodeIsNamed: true, + startPosition: {row: 0, column: 0}, + endPosition: {row: 0, column: 5}, + startIndex: 0, + endIndex: 5, + }); - assert(copy.gotoLastChild()); - assertCursorState(copy, { - nodeType: 'identifier', - nodeIsNamed: true, - startPosition: {row: 0, column: 4}, - endPosition: {row: 0, column: 5}, - startIndex: 4, - endIndex: 5, - }); + assert(copy.gotoLastChild()); + assertCursorState(copy, { + nodeType: 'identifier', + nodeIsNamed: true, + startPosition: {row: 0, column: 4}, + endPosition: {row: 0, column: 5}, + startIndex: 4, + endIndex: 5, + }); - assert(copy.gotoParent()); - assert(copy.gotoParent()); - assert.equal(copy.nodeType, 'binary_expression'); - assert(copy.gotoParent()); - assert.equal(copy.nodeType, 'expression_statement'); - assert(copy.gotoParent()); - assert.equal(copy.nodeType, 'program'); - assert(!copy.gotoParent()); - } - - // const childIndex = cursor.gotoFirstChildForIndex(12); - // assertCursorState(cursor, { - // nodeType: 'identifier', - // nodeIsNamed: true, - // startPosition: {row: 0, column: 12}, - // endPosition: {row: 0, column: 13}, - // startIndex: 12, - // endIndex: 13 - // }); - // assert.equal(childIndex, 2); - // assert(!cursor.gotoNextSibling()); - // assert(cursor.gotoParent()); + assert(copy.gotoParent()); + assert(copy.gotoParent()); + assert.equal(copy.nodeType, 'binary_expression'); + assert(copy.gotoParent()); + assert.equal(copy.nodeType, 'expression_statement'); + assert(copy.gotoParent()); + assert.equal(copy.nodeType, 'program'); + assert(!copy.gotoParent()); assert(cursor.gotoParent()); assert.equal(cursor.nodeType, 'binary_expression'); @@ -414,7 +399,7 @@ function spliceInput(input, startIndex, lengthRemoved, newText) { function getExtent(text) { let row = 0; let index; - for (index = 0; index != -1; index = text.indexOf('\n', index)) { + for (index = 0; index !== -1; index = text.indexOf('\n', index)) { index++; row++; }