From f2e54a70f021a65149bcac5bf76ca1695bc3ac30 Mon Sep 17 00:00:00 2001 From: Ashi Krishnan Date: Tue, 24 Jul 2018 13:34:41 -0400 Subject: [PATCH] Lint. --- spec/tree-sitter-language-mode-spec.js | 33 ++++++++++++++++++++++++++ src/tree-sitter-grammar.js | 29 ++++++++++------------ src/tree-sitter-language-mode.js | 2 +- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/spec/tree-sitter-language-mode-spec.js b/spec/tree-sitter-language-mode-spec.js index c23849d30..90c90654b 100644 --- a/spec/tree-sitter-language-mode-spec.js +++ b/spec/tree-sitter-language-mode-spec.js @@ -313,6 +313,39 @@ describe('TreeSitterLanguageMode', () => { ]) }) + it('applies rules when specified', async () => { + const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, { + parser: 'tree-sitter-javascript', + scopes: { + 'identifier': [ + {match: '^(exports|document|window|global)$', scopes: 'global'}, + {match: '^[A-Z_]+$', scopes: 'constant'}, + {match: '^[A-Z]', scopes: 'constructor'}, + 'variable' + ], + } + }) + + buffer.setText(`exports.object = Class(SOME_CONSTANT, x)`) + + const languageMode = new TreeSitterLanguageMode({buffer, grammar}) + buffer.setLanguageMode(languageMode) + await nextHighlightingUpdate(languageMode) + + expectTokensToEqual(editor, [ + [ + {text: 'exports', scopes: ['global']}, + {text: '.object = ', scopes: []}, + {text: 'Class', scopes: ['constructor']}, + {text: '(', scopes: []}, + {text: 'SOME_CONSTANT', scopes: ['constant']}, + {text: ', ', scopes: []}, + {text: 'x', scopes: ['variable']}, + {text: ')', scopes: []}, + ] + ]) + }) + describe('when the buffer changes during a parse', () => { it('immediately parses again when the current parse completes', async () => { const grammar = new TreeSitterGrammar(atom.grammars, jsGrammarPath, { diff --git a/src/tree-sitter-grammar.js b/src/tree-sitter-grammar.js index 7f5412020..ee93ded4f 100644 --- a/src/tree-sitter-grammar.js +++ b/src/tree-sitter-grammar.js @@ -2,23 +2,6 @@ const path = require('path') const SyntaxScopeMap = require('./syntax-scope-map') const Module = require('module') -tap = x => (console.log(x), x) - -const toSyntaxClasses = scopes => - typeof scopes == 'string' - ? scopes - .split('.') - .map(s => `syntax--${s}`) - .join(' ') - : - Array.isArray(scopes) - ? scopes.map(toSyntaxClasses) - : - scopes.match - ? tap({match: new RegExp(scopes.match), scopes: toSyntaxClasses(scopes.scopes)}) - : - Object.assign({}, scopes, {scopes: toSyntaxClasses(scopes.scopes)}) - module.exports = class TreeSitterGrammar { constructor (registry, filePath, params) { @@ -86,3 +69,15 @@ class TreeSitterGrammar { if (this.registration) this.registration.dispose() } } + +const toSyntaxClasses = scopes => + typeof scopes === 'string' + ? scopes + .split('.') + .map(s => `syntax--${s}`) + .join(' ') + : Array.isArray(scopes) + ? scopes.map(toSyntaxClasses) + : scopes.match + ? {match: new RegExp(scopes.match), scopes: toSyntaxClasses(scopes.scopes)} + : Object.assign({}, scopes, {scopes: toSyntaxClasses(scopes.scopes)}) diff --git a/src/tree-sitter-language-mode.js b/src/tree-sitter-language-mode.js index de28fc216..62c1631d5 100644 --- a/src/tree-sitter-language-mode.js +++ b/src/tree-sitter-language-mode.js @@ -919,7 +919,7 @@ class LayerHighlightIterator { const applyLeafRules = (rules, cursor) => { if (!rules || typeof rules === 'string') return rules if (Array.isArray(rules)) { - for (let i = 0, {length} = rules; i != length; ++i) { + for (let i = 0, {length} = rules; i !== length; ++i) { const result = applyLeafRules(rules[i], cursor) if (result) return result }