8000 Partially fix tests (in the `simplify` branch) by DmitrySharabin · Pull Request #3940 · PrismJS/prism · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Partially fix tests (in the simplify branch) #3940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 25, 2025
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
11 changes: 6 additions & 5 deletions tests/components-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { assert } from 'chai';
import { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { forEach, noop, toArray } from '../src/shared/util';
import { assert } from 'chai';
import { noop } from '../src/shared/util';
import { forEach, toArray } from '../src/util/iterables';
import { getComponent, getComponentIds, getLanguageIds } from './helper/prism-loader';
import type { LanguageProto } from '../src/types';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -41,7 +43,7 @@ describe('Components', () => {
for (const id of getComponentIds()) {
const proto = await getComponent(id).catch(noop);
add(id, 'a component id');
forEach(proto?.alias, a => add(a, `an alias of ${id}`));
forEach((proto as LanguageProto)?.alias, a => add(a, `an alias of ${id}`));
}
});
});
Expand Down Expand Up @@ -100,7 +102,7 @@ describe('components.json', () => {
describe('- should have valid alias titles', () => {
for (const lang of getLanguageIds()) {
it(`- ${lang} should have all alias titles registered as alias`, async () => {
const aliases = new Set(toArray((await getComponent(lang)).alias));
const aliases = new Set(toArray(((await getComponent(lang)) as LanguageProto)?.alias));
const aliasTitles =
(components.languages[lang] as ComponentEntry | undefined)?.aliasTitles ?? {};

Expand All @@ -123,7 +125,6 @@ describe('components.json', () => {
.map((key): { id: string; title: string } => {
return {
id: key,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
title: (components.languages[key] as ComponentEntry).title!,
};
});
Expand Down
56 changes: 15 additions & 41 deletions tests/core/registry.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,34 @@
import { assert } from 'chai';
import { Prism } from '../../src/core/prism';
import type { Grammar } from '../../src/types';

describe('Registry', () => {
it('should resolve aliases', () => {
const { components } = new Prism();
const { languageRegistry } = new Prism();

const grammar = {};
components.add({ id: 'a', alias: 'b', grammar });
const grammar = {} as Grammar;
languageRegistry.add({ id: 'a', alias: 'b', grammar });

assert.isTrue(components.has('a'));
assert.isTrue(components.has('b'));
assert.equal(languageRegistry.resolveRef('a').id, 'a');
assert.equal(languageRegistry.resolveRef('b').id, 'a');

assert.strictEqual(components.resolveAlias('a'), 'a');
assert.strictEqual(components.resolveAlias('b'), 'a');
assert.strictEqual(languageRegistry.aliases['b'], 'a');

assert.strictEqual(components.getLanguage('a'), grammar);
assert.strictEqual(components.getLanguage('b'), grammar);
assert.deepStrictEqual(languageRegistry.getLanguage('a')?.resolvedGrammar, grammar);
assert.deepStrictEqual(languageRegistry.getLanguage('b')?.resolvedGrammar, grammar);
});

it('should resolve aliases in optional dependencies', () => {
const { components } = new Prism();
const { languageRegistry } = new Prism();

const grammar = {};
components.add({ id: 'a', alias: 'b', grammar });
components.add({
const grammar = {} as Grammar;
languageRegistry.add({ id: 'a', alias: 'b', grammar });
languageRegistry.add({
id: 'c',
optional: 'b',
grammar ({ getOptionalLanguage }) {
return getOptionalLanguage('b') ?? {};
},
grammar: ({ getLanguage }) => getLanguage('b') ?? {},
});

assert.strictEqual(components.getLanguage('c'), grammar);
});

it('should throw on circular dependencies', () => {
assert.throws(() => {
const { components } = new Prism();

components.add({ id: 'a', optional: 'b', grammar: {} });
components.add({ id: 'b', optional: 'a', grammar: {} });
}, /Circular dependency a -> b -> a not allowed/);

assert.throws(() => {
const { components } = new Prism();

components.add(
{ id: 'a', optional: 'b', grammar: {} },
{ id: 'b', optional: 'a', grammar: {} }
);
}, /Circular dependency a -> b -> a not allowed/);

assert.throws(() => {
const { components } = new Prism();

components.add({ id: 'a', optional: 'a', grammar: {} });
}, /Circular dependency a -> a not allowed/);
assert.deepStrictEqual(languageRegistry.getLanguage('c')?.resolvedGrammar, grammar);
});
});
11 changes: 5 additions & 6 deletions tests/coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ describe('Pattern test coverage', () => {
const Prism = await PrismLoader.createInstance(languages);

const root = Object.fromEntries(
[...Prism.components['entries'].keys()].map(id => [
Object.keys(Prism.languageRegistry.cache).map(id => [
id,
Prism.components.getLanguage(id),
Prism.languageRegistry.getLanguage(id)?.resolvedGrammar,
])
);

BFS(root, (path, object) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { key, value } = path[path.length - 1];
const tokenPath = BFSPathToPrismTokenPath(path);

Expand Down Expand Up @@ -78,8 +77,8 @@ describe('Pattern test coverage', () => {
try {
await runTestCase(languageIdentifier, filePath, 'none', createInstance);
}
catch (error) {
// we don't case about whether the test succeeds,
catch (error) { // eslint-disable-line @typescript-eslint/no-unused-vars
// we don't care about whether the test succeeds,
// we just want to gather usage data
}
}
Expand Down Expand Up @@ -112,7 +111,7 @@ describe('Pattern test coverage', () => {
});

it(`- should exhaustively cover all keywords in keyword lists`, () => {
const problems = [];
const problems: string[] = [];

for (const data of getAllOf(language)) {
if (data.matches.length === 0) {
Expand Down
13 changes: 11 additions & 2 deletions tests/helper/prism-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export async function createInstance (languages?: string | string[]) {
const instance = new Prism();

const protos = await Promise.all(toArray(languages).map(getComponent));
instance.components.add(...protos);
protos.filter(Boolean).forEach(proto => {
instance.languageRegistry.add(proto as LanguageProto);
});

return instance;
}
Expand Down Expand Up @@ -142,7 +144,14 @@ export function createPrismDOM (): PrismDOM<{}> {
const load = async (languagesOrPlugins: string | string[]) => {
const protos = await Promise.all(toArray(languagesOrPlugins).map(getComponent));
withGlobals(() => {
instance.components.add(...protos);
protos.filter(Boolean).forEach(proto => {
if (proto.grammar) {
instance.languageRegistry.add(proto);
}
else {
instance.pluginRegistry.add(proto);
}
});
});
};

Expand Down
6 changes: 3 additions & 3 deletions tests/helper/test-case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import fs from 'fs';
import { createInstance } from './prism-loader';
import * as TokenStreamTransformer from './token-stream-transformer';
import { formatHtml, getLeadingSpaces } from './util';
import type { Prism } from '../../src/core';
import type { TokenStream } from '../../src/core/token';
import type { Prism } from '../../src/types';
import type { TokenStream } from '../../src/types';

const defaultCreateInstance = createInstance;

Expand Down Expand Up @@ -134,7 +134,7 @@ interface Runner<T> {
}
const jsonRunner: Runner<TokenStream> = {
run (Prism, code, language) {
const grammar = Prism.components.getLanguage(language);
const grammar = Prism.languageRegistry.getLanguage(language)?.resolvedGrammar;
return Prism.tokenize(code, grammar ?? {});
},
print (actual) {
Expand Down
8 changes: 4 additions & 4 deletions tests/identifier-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toArray } from '../src/util/iterables';
import { createInstance, getComponent, getLanguageIds } from './helper/prism-loader';
import { prettyprint } from './helper/token-stream-transformer';
import type { Prism, Token } from '../src/core';
import type { TokenStream } from '../src/core/token';
import type { TokenStream } from '../src/types';

// This is where you can exclude a language from the identifier test.
//
Expand Down Expand Up @@ -89,7 +89,7 @@ for (const lang of getLanguageIds()) {
describe(`Patterns of '${lang}' with optional dependencies`, () => {
const getPrism = async () => {
const component = await getComponent(lang);
const optional = toArray(component.optional);
const optional = toArray(component?.optional);
const Prism = await createInstance([lang, ...optional]);
return Prism;
};
Expand Down Expand Up @@ -122,8 +122,8 @@ function testLiterals (getPrism: Promise<Prism>, lang: string) {
identifierType: keyof IdentifierTestOptions
) {
const Prism = await getPrism;
for (const id of Prism.components['entries'].keys()) {
const grammar = Prism.components.getLanguage(id);
for (const id of Object.keys(Prism.languageRegistry.cache)) {
const grammar = Prism.languageRegistry.getLanguage(id)?.resolvedGrammar;
if (!grammar) {
continue;
}
Expand Down
20 changes: 8 additions & 12 deletions tests/pattern-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
visited.add(grammar);

BFS(grammar, path => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const { key, value } = path[path.length - 1];
const tokenPath = BFSPathToPrismTokenPath(path, rootStr);
visited.add(value);
Expand All @@ -125,9 +124,7 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
);
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const parent = path.length > 1 ? path[path.length - 2].value : undefined;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const lookbehind =
key === 'pattern' && !!parent && !!(parent as GrammarToken).lookbehind;
const lookbehindGroup = lookbehind
Expand All @@ -138,7 +135,6 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
ast,
tokenPath,
name: key,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
parent,
path,
lookbehind,
Expand All @@ -159,8 +155,8 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
}

// static analysis
for (const id of Prism.components['entries'].keys()) {
const grammar = Prism.components.getLanguage(id);
for (const id of Object.keys(Prism.languageRegistry.cache)) {
const grammar = Prism.languageRegistry.getLanguage(id)?.resolvedGrammar;
if (grammar) {
traverse(grammar, id);
}
Expand All @@ -169,7 +165,7 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
// dynamic analysis
for (const lang of getRelevantLanguages()) {
const snippets = testSnippets.get(lang);
const grammar = Prism.components.getLanguage(lang);
const grammar = Prism.languageRegistry.getLanguage(lang)?.resolvedGrammar;

// eslint-disable-next-line @typescript-eslint/unbound-method
const oldTokenize = Prism.tokenize;
Expand Down Expand Up @@ -282,7 +278,7 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
forEachCapturingGroup(ast.pattern, ({ group, number }) => {
const isLookbehindGroup = group === lookbehindGroup;
if (group.references.length === 0 && !isLookbehindGroup) {
const fixes = [];
const fixes: string[] = [];
fixes.push(
`Make this group a non-capturing group ('(?:...)' instead of '(...)'). (It's usually this option.)`
);
Expand Down Expand Up @@ -406,7 +402,7 @@ function testPatterns (getPrism: () => Promise<Prism | undefined>, mainLanguage:
* Returns the first capturing group in the given pattern.
*/
function getFirstCapturingGroup (pattern: Pattern): CapturingGroup | undefined {
let cap = undefined;
let cap: CapturingGroup | undefined = undefined;

try {
visitRegExpAST(pattern, {
Expand All @@ -416,7 +412,7 @@ function getFirstCapturingGroup (pattern: Pattern): CapturingGroup | undefined {
},
});
}
catch (error) {
catch (error) { // eslint-disable-line @typescript-eslint/no-unused-vars
// ignore errors
}

Expand Down Expand Up @@ -784,9 +780,9 @@ interface Highlight {
function highlight (highlights: Highlight[], offset = 0) {
highlights.sort((a, b) => a.start - b.start);

const lines = [];
const lines: string[] = [];
while (highlights.length > 0) {
const newHighlights = [];
const newHighlights: Highlight[] = [];
let l = '';
for (const highlight of highlights) {
const start = highlight.start + offset;
Expand Down
0