diff --git a/scripts/build.ts b/scripts/build.ts index e0a2b5939c..c570702492 100644 --- a/scripts/build.ts +++ b/scripts/build.ts @@ -140,7 +140,7 @@ const dataToInsert = { const data = await Promise.all( [...languageIds, ...pluginIds].map(async id => { const proto = await loadComponent(id); - return { id, alias: toArray((proto as LanguageProto).alias) }; + return { id, alias: toArray((proto as LanguageProto)?.alias) }; }) ); return Object.fromEntries(data.flatMap(({ id, alias }) => alias.map(a => [a, id]))); @@ -167,7 +167,7 @@ const dataToInsert = { if (!title) { throw new Error(`No title for ${id}`); } - return [id, ...toArray(proto.alias)].map(name => ({ + return [id, ...toArray(proto?.alias)].map(name => ({ name, title: rawTitles.get(id) ?? title, })); diff --git a/src/core/tokenize/match.ts b/src/core/tokenize/match.ts index 186d53ad12..1de4ee9766 100644 --- a/src/core/tokenize/match.ts +++ b/src/core/tokenize/match.ts @@ -2,7 +2,7 @@ import { Token } from '../classes/token'; import singleton from '../prism'; import { tokenize } from './tokenize'; import { resolve, tokenizeByNamedGroups } from './util'; -import type { GrammarToken, GrammarTokens, RegExpLike, TokenStream } from '../../types'; +import type { Grammar, GrammarToken, GrammarTokens, RegExpLike, TokenStream } from '../../types'; import type { LinkedList, LinkedListHeadNode, @@ -22,7 +22,7 @@ export function _matchGrammar ( ): void { const prism = this ?? singleton; - grammar = resolve.call(prism, grammar); + grammar = resolve.call(prism, grammar) as Grammar; for (const token in grammar) { const tokenValue = grammar[token]; @@ -50,7 +50,7 @@ export function _matchGrammar ( // Without the global flag, lastIndex won't work flagsToAdd += 'g'; } - if (pattern.source.indexOf('(?<') && pattern.hasIndices === false) { + if (pattern.source.includes('(?<') && pattern.hasIndices === false) { // Has named groups, we need to be able to capture their indices flagsToAdd += 'd'; } @@ -81,7 +81,7 @@ export function _matchGrammar ( } let removeCount = 1; // this is the to parameter of removeBetween - let match; + let match: RegExpExecArray | null = null; if (greedy) { match = matchPattern(pattern, pos, text, lookbehind); @@ -155,7 +155,7 @@ export function _matchGrammar ( tokenList.removeRange(removeFrom, removeCount); let byGroups = match.groups ? tokenizeByNamedGroups(match) : null; - if (byGroups?.length > 1) { + if (byGroups && byGroups.length > 1) { content = byGroups.map(arg => { let content = typeof arg === 'string' ? arg : arg.content; let type = typeof arg === 'string' ? undefined : arg.type; @@ -180,7 +180,7 @@ export function _matchGrammar ( }); } else if (insideGrammar) { - content = tokenize.call(prism, content, insideGrammar); + content = tokenize.call(prism, content, insideGrammar as Grammar); } const wrapped = new Token(token, content, alias, matchStr); diff --git a/src/core/tokenize/tokenize.ts b/src/core/tokenize/tokenize.ts index 8cb9bd7f85..c3f5acf3c1 100644 --- a/src/core/tokenize/tokenize.ts +++ b/src/core/tokenize/tokenize.ts @@ -2,7 +2,6 @@ import { LinkedList } from '../linked-list'; import singleton from '../prism'; import { _matchGrammar } from './match'; import type { Grammar } from '../../types'; -import type LanguageRegistry from '../classes/language-registry'; import type { Token, TokenStream } from '../classes/token'; import type { Prism } from '../prism'; diff --git a/src/core/tokenize/util.ts b/src/core/tokenize/util.ts index aadd24fdb3..e41d67699c 100644 --- a/src/core/tokenize/util.ts +++ b/src/core/tokenize/util.ts @@ -19,14 +19,14 @@ export function resolve ( } if (typeof ret === 'object' && ret.$rest) { - let restGrammar = resolve(ret.$rest, prism) ?? {}; + let restGrammar = resolve.call(prism, ret.$rest) ?? {}; if (typeof restGrammar === 'object') { ret = { ...ret, ...restGrammar }; } } - return ret; + return ret as Grammar | undefined | Function; } export function tokenizeByNamedGroups (match: RegExpExecArray): ({ type; content } | string)[] { diff --git a/src/util/async.ts b/src/util/async.ts index bd7e06d495..44f1cc0751 100644 --- a/src/util/async.ts +++ b/src/util/async.ts @@ -49,16 +49,19 @@ export class Deferred extends Promise { } } -export function defer (): Promise & { +type DeferredPromise = Promise & { resolve: (value: T) => void; reject: (reason?: any) => void; -} { - let res, rej; +}; - let promise = new Promise((resolve, reject) => { +export function defer (): DeferredPromise { + let res!: (value: T) => void; + let rej!: (reason?: any) => void; + + let promise = new Promise((resolve, reject) => { res = resolve; rej = reject; - }); + }) as DeferredPromise; promise.resolve = res; promise.reject = rej; diff --git a/tests/pattern-tests.ts b/tests/pattern-tests.ts index 8a19358d81..38c77fadf9 100644 --- a/tests/pattern-tests.ts +++ b/tests/pattern-tests.ts @@ -1,21 +1,22 @@ import { assert } from 'chai'; import { - JS, - NFA, - Transformers, - Words, combineTransformers, getIntersectionWordSets, isDisjointWith, + JS, + NFA, transform, + Transformers, + Words, } from 'refa'; import * as RAA from 'regexp-ast-analysis'; import { visitRegExpAST } from 'regexpp'; import * as scslre from 'scslre'; -import { lazy, toArray } from '../src/shared/util'; +import { lazy } from '../src/shared/util'; +import { toArray } from '../src/util/iterables'; import * as args from './helper/args'; import { createInstance, getComponent, getLanguageIds } from './helper/prism-loader'; -import { TestCaseFile, parseLanguageNames } from './helper/test-case'; +import { parseLanguageNames, TestCaseFile } from './helper/test-case'; import { loadAllTests } from './helper/test-discovery'; import { BFS, BFSPathToPrismTokenPath, isRegExp, parseRegex } from './helper/util'; import type { Prism } from '../src/core'; @@ -90,7 +91,7 @@ function testPatterns (getPrism: () => Promise, mainLanguage: ast: LiteralAST; tokenPath: string; name: string; - parent: any; + parent: any; lookbehind: boolean; lookbehindGroup: CapturingGroup | undefined; path: PathItem[]; @@ -704,7 +705,10 @@ function checkPolynomialBacktracking (path: string, pattern: RegExp, ast?: Liter ast = parseRegex(pattern); } - const result = scslre.analyse(ast as Readonly, { maxReports: 1, reportTypes: { 'Move': false } }); + const result = scslre.analyse(ast as Readonly, { + maxReports: 1, + reportTypes: { 'Move': false }, + }); if (result.reports.length > 0) { const report = result.reports[0];