Skip to content

Commit 7695c23

Browse files
committed
convert tests to typescript
1 parent beb2985 commit 7695c23

33 files changed

+327
-225
lines changed

.eslintrc.json

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,41 @@
1212
"node": true,
1313
"es6": true
1414
},
15-
"parser": "@typescript-eslint/parser",
1615
"parserOptions": {
17-
"sourceType": "script",
16+
"sourceType": "module",
1817
"ecmaVersion": 2020
1918
},
2019
"overrides": [
20+
{
21+
"files": ["*.ts"],
22+
"parser": "@typescript-eslint/parser",
23+
"extends": [
24+
"plugin:@typescript-eslint/eslint-recommended"
25+
],
26+
"parserOptions": {
27+
"//": "NB project is relative to the directory you run eslint from, see https://github.com/typescript-eslint/typescript-eslint/tree/main/packages/parser#parseroptionsproject",
28+
"project": "./tsconfig.json"
29+
},
30+
"rules": {
31+
"@typescript-eslint/consistent-type-imports": "error",
32+
"@typescript-eslint/no-use-before-define": [
33+
"error",
34+
{
35+
"functions": false,
36+
"typedefs": false
37+
}
38+
],
39+
"@typescript-eslint/no-unnecessary-type-assertion": "error",
40+
"@typescript-eslint/no-unused-vars": "error",
41+
"@typescript-eslint/no-explicit-any": "error",
42+
"no-unused-vars": "off"
43+
}
44+
},
2145
{
2246
"files": ["js/**/*"],
47+
"parserOptions": {
48+
"sourceType": "script"
49+
},
2350
"env": {
2451
"node": false,
2552
"browser": true,
@@ -45,13 +72,12 @@
4572
},
4673
{
4774
"files": ["test/**/*"],
48-
"env": {
49-
"mocha": true
75+
"parserOptions": {
76+
"project": "./test/tsconfig.json"
5077
}
5178
}
5279
],
5380
"rules": {
54-
"@typescript-eslint/consistent-type-imports": "error",
5581
"prettier/prettier": "error",
5682
"arrow-body-style": "error",
5783
"prefer-arrow-callback": "error",

.github/workflows/check.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ jobs:
2525
- name: Build
2626
run: npm run build
2727

28+
- name: Typecheck
29+
run: npm run typecheck
30+
2831
- name: Test
2932
run: npm test
3033

package-lock.json

Lines changed: 29 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"test-published-files": "npm pack && tar zxvf \"ecmarkup-$npm_package_version.tgz\" && cp -r test package/test && cd package && npm test && cd ..",
1919
"posttest-published-files": "rm -rf \"ecmarkup-$npm_package_version.tgz\" package",
2020
"lint": "eslint --ext .js,.ts js src test",
21+
"typecheck": "tsc --noEmit && tsc -p test",
2122
"format": "prettier --write ."
2223
},
2324
"bin": {
@@ -70,7 +71,7 @@
7071
"@types/command-line-usage": "^5.0.2",
7172
"@types/js-yaml": "^3.12.1",
7273
"@types/jsdom": "^16.2.13",
73-
"@types/node": "^13.1.8",
74+
"@types/node": "^24.10.13",
7475
"@types/parse5": "^6.0.2",
7576
"@typescript-eslint/eslint-plugin": "^8.6.0",
7677
"@typescript-eslint/parser": "^8.6.0",

src/.eslintrc.json

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Biblio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export type BiblioEntry =
414414
| StepBiblioEntry;
415415

416416
// see https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
417-
type Unkey<T, S extends string> = T extends any ? Omit<T, S> : never;
417+
type Unkey<T, S extends string> = T extends unknown ? Omit<T, S> : never;
418418

419419
type NonExportedKeys = 'location' | 'referencingIds' | 'namespace';
420420
export type PartialBiblioEntry = Unkey<BiblioEntry, NonExportedKeys>;

src/Eqn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export default class Eqn extends Builder {
5151
let contents;
5252
try {
5353
contents = emd.fragment(node.innerHTML);
54-
} catch (e: any) {
55-
utils.warnEmdFailure(spec.warn, node, e);
54+
} catch (e) {
55+
utils.warnEmdFailure(spec.warn, node, e as SyntaxError & { line?: number; column?: number });
5656
node.innerHTML = utils.wrapEmdFailure(node.innerHTML);
5757
return;
5858
}

src/Spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,10 +1276,10 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}
12761276
const solarizedStyle = this.doc.createElement('style');
12771277
solarizedStyle.textContent = `
12781278
@import url("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${
1279-
(hljs as any).versionString
1279+
(hljs as unknown as { versionString: string }).versionString
12801280
}/styles/base16/solarized-light.min.css");
12811281
@import url("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/${
1282-
(hljs as any).versionString
1282+
(hljs as unknown as { versionString: string }).versionString
12831283
}/styles/a11y-dark.min.css") (prefers-color-scheme: dark);
12841284
`;
12851285
this.doc.head.appendChild(solarizedStyle);
@@ -1340,18 +1340,20 @@ ${this.opts.multipage ? `<li><span>Navigate to/from multipage</span><code>m</cod
13401340
return;
13411341
}
13421342

1343-
let data: any;
1343+
let data: unknown;
13441344
try {
13451345
data = yaml.safeLoad(block.textContent!);
1346-
} catch (e: any) {
1347-
if (typeof e?.mark.line === 'number' && typeof e?.mark.column === 'number') {
1346+
} catch (e) {
1347+
const yamlError = e as { mark?: { line?: number; column?: number }; reason?: string };
1348+
const mark = yamlError?.mark;
1349+
if (typeof mark?.line === 'number' && typeof mark?.column === 'number') {
13481350
this.warn({
13491351
type: 'contents',
13501352
ruleId: 'invalid-metadata',
1351-
message: `metadata block failed to parse: ${e.reason}`,
1353+
message: `metadata block failed to parse: ${yamlError.reason}`,
13521354
node: block,
1353-
nodeRelativeLine: e.mark.line + 1,
1354-
nodeRelativeColumn: e.mark.column + 1,
1355+
nodeRelativeLine: mark.line + 1,
1356+
nodeRelativeColumn: mark.column + 1,
13551357
});
13561358
} else {
13571359
this.warn({

src/arg-parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ export function parse<T extends readonly OptionDefinition[]>(
3030
try {
3131
// @ts-ignore the types are wrong about mutability
3232
args = commandLineArgs(options, { argv });
33-
} catch (e: any) {
34-
if (e?.name === 'UNKNOWN_OPTION') {
35-
fail(`Unknown option ${e.optionName}`);
33+
} catch (e) {
34+
if (e != null && typeof e === 'object' && 'name' in e && e.name === 'UNKNOWN_OPTION') {
35+
fail(`Unknown option ${(e as unknown as { optionName: string }).optionName}`);
3636
}
3737
throw e;
3838
}

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ const build = debounce(async function build() {
186186
utils.logVerbose(warned ? 'Completed with errors.' : 'Done.');
187187
}
188188

189-
const pending: Promise<any>[] = [];
189+
const pending: Promise<void>[] = [];
190190
if (args['write-biblio']) {
191191
if (args.verbose) {
192192
utils.logVerbose('Writing biblio file to ' + args['write-biblio']);
@@ -281,9 +281,9 @@ const build = debounce(async function build() {
281281
}
282282
}
283283
}
284-
} catch (e: any) {
284+
} catch (e) {
285285
if (args.watch) {
286-
process.stderr.write(e.stack);
286+
process.stderr.write(e instanceof Error ? e.stack ?? e.message : String(e));
287287
} else {
288288
throw e;
289289
}

0 commit comments

Comments
 (0)