Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])));
Expand All @@ -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,
}));
Expand Down
12 changes: 6 additions & 6 deletions src/core/tokenize/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/core/tokenize/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
4 changes: 2 additions & 2 deletions src/core/tokenize/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TS can't infer that ret can't be of type string in the end (even though we have a branch that checks that ret is a string and where we update its value, so it's no longer a string).

}

export function tokenizeByNamedGroups (match: RegExpExecArray): ({ type; content } | string)[] {
Expand Down
13 changes: 8 additions & 5 deletions src/util/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ export class Deferred<T> extends Promise<T> {
}
}

export function defer<T> (): Promise<T> & {
type DeferredPromise<T> = Promise<T> & {
resolve: (value: T) => void;
reject: (reason?: any) => void;
} {
let res, rej;
};

let promise = new Promise((resolve, reject) => {
export function defer<T> (): DeferredPromise<T> {
let res!: (value: T) => void;
let rej!: (reason?: any) => void;

let promise = new Promise<T>((resolve, reject) => {
res = resolve;
rej = reject;
});
}) as DeferredPromise<T>;

promise.resolve = res;
promise.reject = rej;
Expand Down
20 changes: 12 additions & 8 deletions tests/pattern-tests.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -90,7 +91,7 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
ast: LiteralAST;
tokenPath: string;
name: string;
parent: any;
parent: any;
lookbehind: boolean;
lookbehindGroup: CapturingGroup | undefined;
path: PathItem[];
Expand Down Expand Up @@ -704,7 +705,10 @@ function checkPolynomialBacktracking (path: string, pattern: RegExp, ast?: Liter
ast = parseRegex(pattern);
}

const result = scslre.analyse(ast as Readonly<scslre.ParsedLiteral>, { maxReports: 1, reportTypes: { 'Move': false } });
const result = scslre.analyse(ast as Readonly<scslre.ParsedLiteral>, {
maxReports: 1,
reportTypes: { 'Move': false },
});
if (result.reports.length > 0) {
const report = result.reports[0];

Expand Down