Skip to content

Commit 6af1a48

Browse files
[tests] Allow type check (#4015)
- Restore `tsconfig.json` for tests - Adjust types and fix TS errors - Add corresponding NPM scripts
1 parent ca2f366 commit 6af1a48

File tree

13 files changed

+143
-28
lines changed

13 files changed

+143
-28
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
"test:plugins": "mocha tests/plugins/**/*.js",
5050
"test:runner": "mocha tests/testrunner-tests.js",
5151
"test": "npm-run-all test:*",
52-
"typecheck": "tsc"
52+
"typecheck:core": "tsc",
53+
"typecheck:tests": "tsc -p tests/tsconfig.json",
54+
"typecheck": "npm-run-all typecheck:*"
5355
},
5456
"repository": {
5557
"type": "git",

src/shared/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const noop = () => {
4242

4343
/**
4444
* @param {any} value
45-
* @returns {boolean}
45+
* @returns {value is T & {}}
4646
*/
4747
export function isNonNull (value) {
4848
return value != null;

tests/components-test.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('Components', () => {
1414

1515
for (const id of getComponentIds()) {
1616
const proto = await getComponent(id).catch(noop);
17-
const require = new Set(toArray(proto?.require).map(p => p.id));
17+
const require = new Set(toArray(/** @type any */ (proto)?.require).map(p => p.id));
1818

19-
forEach(proto?.optional, opt => {
19+
forEach(/** @type any */ (proto?.optional), opt => {
2020
if (require.has(opt)) {
2121
assert.fail(
2222
`The optional dependency ${opt} is redundant because ${id} already requires it. Remove the optional dependency.`
@@ -47,7 +47,8 @@ describe('Components', () => {
4747
for (const id of getComponentIds()) {
4848
const proto = await getComponent(id).catch(noop);
4949
add(id, 'a component id');
50-
forEach(proto?.alias, a => add(a, `an alias of ${id}`));
50+
forEach(/** @type {string | string[] | null | undefined} */ (proto?.alias), a =>
51+
add(a, `an alias of ${id}`));
5152
}
5253
});
5354
});
@@ -74,15 +75,22 @@ describe('components.json', () => {
7475
}
7576

7677
for (const id in entries) {
77-
consumeFn(entries[id], id, entries);
78+
consumeFn(entries[id], id, /** @type {Record<string, ComponentEntry>} */ (entries));
7879
}
7980
}
8081

8182
describe('- should have valid alias titles', () => {
8283
for (const lang of getLanguageIds()) {
8384
it(`- ${lang} should have all alias titles registered as alias`, async () => {
84-
const aliases = new Set(toArray((await getComponent(lang)).alias));
85-
const aliasTitles = components.languages[lang]?.aliasTitles ?? {};
85+
const aliases = new Set(
86+
toArray(
87+
/** @type {string | string[] | null | undefined} */ (
88+
(await getComponent(lang)).alias
89+
)
90+
)
91+
);
92+
const aliasTitles =
93+
/** @type {ComponentEntry} */ (components.languages[lang])?.aliasTitles ?? {};
8694

8795
Object.keys(aliasTitles).forEach(id => {
8896
if (!aliases.has(id)) {
@@ -103,7 +111,7 @@ describe('components.json', () => {
103111
.map(key => {
104112
return {
105113
id: key,
106-
title: components.languages[key].title,
114+
title: /** @type {ComponentEntry} */ (components.languages[key]).title,
107115
};
108116
});
109117

@@ -119,12 +127,16 @@ describe('components.json', () => {
119127
}
120128

121129
const sorted = [...languages].sort((a, b) => {
122-
const comp = transformTitle(a.title).localeCompare(transformTitle(b.title));
130+
const comp = transformTitle(/** @type {string} */ (a.title)).localeCompare(
131+
transformTitle(/** @type {string} */ (b.title))
132+
);
123133
if (comp !== 0) {
124134
return comp;
125135
}
126136
// a and b have the same intermediate form (e.g. "C" => "C", "C++" => "C", "C#" => "C").
127-
return a.title.toLowerCase().localeCompare(b.title.toLowerCase());
137+
return /** @type {string} */ (a.title)
138+
.toLowerCase()
139+
.localeCompare(/** @type {string} */ (b.title).toLowerCase());
128140
});
129141

130142
assert.sameOrderedMembers(languages, sorted);

tests/helper/args.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import yargs from 'yargs';
22
import { hideBin } from 'yargs/helpers';
33

4-
/**
5-
* @type {Args}
6-
*/
7-
const args = yargs(hideBin(process.argv)).argv;
4+
const args = /** @type {Args} */ (yargs(hideBin(process.argv)).argv);
85

96
export const language = args.language;
107
export const update = !!args.update;

tests/helper/prism-dom-util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function createTestSuite (options) {
8181
await dom.loadLanguages(options.languages);
8282
}
8383
if (options.plugins) {
84-
await dom.loadPlugins(options.plugins);
84+
await dom.loadPlugins(/** @type {string | string[]} */ (options.plugins));
8585
}
8686

8787
dom.withGlobals(() => {
@@ -101,7 +101,7 @@ export function createTestSuite (options) {
101101
*/
102102

103103
/**
104-
* @template T
104+
* @template {string} T
105105
* @typedef {import('../types.d.ts').TestSuiteDom<T>} TestSuiteDom
106106
*/
107107

tests/helper/prism-loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export function createPrismDOM () {
143143
finally {
144144
undo?.();
145145
// Clean up navigator property
146-
delete g.navigator;
146+
delete (/** @type {Partial<typeof global>} */ (g).navigator);
147147
}
148148
};
149149

tests/helper/token-stream-transformer.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,15 +498,15 @@ function prettyGlueTogetherAll (prettyStream, slice) {
498498
/**
499499
*
500500
* @param {PrettyTokenStreamItem} item
501-
* @returns {boolean}
501+
* @returns {item is SimplifiedToken}
502502
*/
503503
function isToken (item) {
504504
return typeof item === 'string' || Array.isArray(item);
505505
}
506506
/**
507507
*
508508
* @param {PrettyTokenStreamItem} item
509-
* @returns {boolean}
509+
* @returns {item is [string, SimplifiedToken[]]}
510510
*/
511511
function isNested (item) {
512512
return Array.isArray(item) && Array.isArray(item[1]);
@@ -515,7 +515,7 @@ function isNested (item) {
515515
/**
516516
*
517517
* @param {PrettyTokenStreamItem} item
518-
* @returns {boolean}
518+
* @returns {item is [string, [string]]}
519519
*/
520520
function isTriviallyNested (item) {
521521
return isNested(item) && item[1].length === 1 && typeof item[1][0] === 'string';

tests/identifier-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ for (const lang of getLanguageIds()) {
7878
describe(`Patterns of '${lang}' with optional dependencies`, () => {
7979
const getPrism = async () => {
8080
const component = await getComponent(lang);
81-
const optional = toArray(component.optional);
81+
const optional = toArray(
82+
/** @type {string | string[] | null | undefined} */ (component.optional)
83+
);
8284
const Prism = await createInstance([lang, ...optional]);
8385
return Prism;
8486
};

tests/pattern-tests.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ for (const lang of getLanguageIds()) {
5454
describe(`Patterns of '${lang}' with optional dependencies`, () => {
5555
const create = lazy(async () => {
5656
const component = await getComponent(lang);
57-
const optional = toArray(component.optional);
57+
const optional = toArray(
58+
/** @type {string | string[] | null | undefined} */ (component.optional)
59+
);
5860
if (optional.length === 0) {
5961
return undefined;
6062
}
@@ -517,7 +519,7 @@ function getResultCache (cacheName) {
517519
}
518520

519521
/**
520-
* @template T
522+
* @template {Node} T
521523
* @param {string} cacheName
522524
* @param {T} cacheKey
523525
* @param {(node: T) => void} compute
@@ -752,7 +754,10 @@ function checkPolynomialBacktracking (path, pattern, ast) {
752754
ast = parseRegex(pattern);
753755
}
754756

755-
const result = scslre.analyse(ast, { maxReports: 1, reportTypes: { 'Move': false } });
757+
const result = scslre.analyse(/** @type {scslre.ParsedLiteral} */ (ast), {
758+
maxReports: 1,
759+
reportTypes: { 'Move': false },
760+
});
756761
if (result.reports.length > 0) {
757762
const report = result.reports[0];
758763

@@ -927,3 +932,16 @@ async function replaceRegExpProto (execSupplier, fn) {
927932
* @property {CapturingGroup} group
928933
* @property {number} number - Note: Starts at 1.
929934
*/
935+
936+
/**
937+
* @typedef {object} ForEachPatternCallbackValue
938+
* @property {RegExp} pattern
939+
* @property {LiteralAST} ast
940+
* @property {string} tokenPath
941+
* @property {string} name
942+
* @property {any} parent
943+
* @property {boolean} lookbehind
944+
* @property {CapturingGroup | undefined} lookbehindGroup
945+
* @property {PathItem[]} path
946+
* @property {(message: string) => void} reportError
947+
*/

tests/plugins/copy-to-clipboard/basic-functionality.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DummyClipboard {
2020
* @param {Navigator} navigator
2121
*/
2222
assign (navigator) {
23-
navigator.clipboard = this;
23+
/** @type {any} */ (navigator).clipboard = this;
2424
}
2525
}
2626

0 commit comments

Comments
 (0)