Skip to content

Commit 68fd666

Browse files
committed
Migrate codebase to typescript
1 parent f8a1680 commit 68fd666

File tree

108 files changed

+3367
-2001
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3367
-2001
lines changed

.c8rc.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"all": true,
3-
"lines": 100,
4-
"statements": 100,
5-
"functions": 100,
6-
"branches": 100,
3+
"lines": 98.4,
4+
"statements": 98.4,
5+
"functions": 99.6,
6+
"branches": 95,
77
"check-coverage": true,
88
"extension": [".js"],
99
"instrument": false,
10-
"src": ".",
10+
"src": "./target/build",
1111
"reporter": ["lcov", "text-summary"],
1212
"reportDir": "./target/coverage",
1313
"tempDirectory": "./target/c8-temporary-output",
14-
"exclude": ["target", "test", "benchmarks/", "eslint.config.js"]
14+
"exclude": ["**/*.test.js", "target/build/benchmarks/", "eslint.config.js"]
1515
}

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
node-version: ${{ matrix.node }}
1818
- name: Install dependencies
1919
run: npm ci
20+
- name: Compile
21+
run: npm run compile
2022
- name: Run linters
2123
run: npm run lint
2224
- name: Run tests

.mocharc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"diff": true,
3+
"forbid-pending": true,
4+
"extension": ["js"],
5+
"spec": [],
6+
"exclude": [],
7+
"jobs": 1,
8+
"parallel": false,
9+
"enable-source-maps": true,
10+
"reporter": "dot",
11+
"slow": 75,
12+
"timeout": 2000,
13+
"ui": "bdd"
14+
}

README.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ npm install --save-dev eslint-plugin-mocha
1717

1818
### Configuration via `eslint.config.js`
1919

20-
To use this plugin with [the new eslint configuration format (flat config)](https://eslint.org/docs/latest/use/configure/configuration-files-new):
20+
To use this plugin with [eslint flat configuration format](https://eslint.org/docs/latest/use/configure/configuration-files-new):
2121

2222
```js
2323
import mochaPlugin from "eslint-plugin-mocha";
2424

2525
export default [
26-
mochaPlugin.configs.flat.recommended, // or `mochaPlugin.configs.flat.all` to enable all
26+
mochaPlugin.configs.recommended, // or `mochaPlugin.configs.all` to enable all
2727
// ... Your configurations here
2828
];
2929
```
@@ -89,26 +89,6 @@ This plugin supports the following settings, which are used by multiple rules:
8989

9090
- `interface`: This allows to select either `TDD`, `BDD` (default) or `exports`. When using `exports` mocha variables are resolved from named `import` statements instead of global variables.
9191

92-
## Configs
93-
94-
### `recommended`
95-
96-
This plugin exports a recommended config that enforces good practices.
97-
98-
Enable it with the extends option:
99-
100-
```json
101-
{
102-
"extends": ["plugin:mocha/recommended"]
103-
}
104-
```
105-
106-
### `all`
107-
108-
There's also a configuration that enables all of our rules.
109-
110-
See [Configuring Eslint](http://eslint.org/docs/user-guide/configuring) on [eslint.org](http://eslint.org) for more info.
111-
11292
## Rules
11393

11494
<!-- begin auto-generated rules list -->

benchmarks/measure.js

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

benchmarks/measure.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import os from 'node:os';
2+
import { performance as performanceHooks } from 'node:perf_hooks';
3+
4+
const [{ speed: cpuSpeed = 0 } = {}] = os.cpus();
5+
6+
export { cpuSpeed };
7+
8+
export async function importFresh(modulePath: string): Promise<void> {
9+
const cacheBuster = `${performanceHooks.now()}_${Math.random()}`;
10+
const cacheBustingModulePath = `${modulePath}?buster=${cacheBuster}`;
11+
12+
await import(cacheBustingModulePath);
13+
}
14+
15+
function isPositiveNumber(value: number): boolean {
16+
return value > 0;
17+
}
18+
19+
type Result = {
20+
readonly duration: number;
21+
readonly memory: number;
22+
};
23+
24+
type MedianResult = {
25+
readonly medianDuration: number;
26+
readonly medianMemory: number;
27+
};
28+
29+
function median(list: readonly number[]): number {
30+
const listParts = 2;
31+
const medianIndex = Math.floor(list.length / listParts);
32+
const lowerValue = list[medianIndex - 1] ?? 0;
33+
const upperValue = list[medianIndex] ?? 0;
34+
35+
if (list.length % listParts === 0) {
36+
return (lowerValue + upperValue) / listParts;
37+
}
38+
39+
return upperValue;
40+
}
41+
42+
export function runSyncBenchmark(fn: () => void, count: number): Readonly<MedianResult> {
43+
const results: Result[] = [];
44+
45+
Array.from({ length: count }).forEach(() => {
46+
const startTime = performanceHooks.now();
47+
const startMemory = process.memoryUsage.rss();
48+
fn();
49+
const endTime = performanceHooks.now();
50+
const endMemory = process.memoryUsage.rss();
51+
const duration = endTime - startTime;
52+
const memory = endMemory - startMemory;
53+
54+
results.push({ duration, memory });
55+
});
56+
57+
const medianDuration = median(results.map((result) => {
58+
return result.duration;
59+
}));
60+
const medianMemory = median(
61+
results
62+
.map((result) => {
63+
return result.memory;
64+
})
65+
.filter(isPositiveNumber)
66+
);
67+
68+
return { medianDuration, medianMemory };
69+
}
70+
71+
async function measureSingleAsyncTask(fn: () => Promise<void>): Promise<Result> {
72+
const startTime = performanceHooks.now();
73+
const startMemory = process.memoryUsage().rss;
74+
await fn();
75+
const endTime = performanceHooks.now();
76+
const endMemory = process.memoryUsage().rss;
77+
const duration = endTime - startTime;
78+
const memory = endMemory - startMemory;
79+
80+
return { duration, memory };
81+
}
82+
83+
export async function runAsyncBenchmark(fn: () => Promise<void>, count: number): Promise<MedianResult> {
84+
const results = [];
85+
86+
for (let iteration = 0; iteration < count; iteration += 1) {
87+
const result = await measureSingleAsyncTask(fn);
88+
results.push(result);
89+
}
90+
91+
const medianDuration = median(results.map((result) => {
92+
return result.duration;
93+
}));
94+
const medianMemory = median(
95+
results
96+
.map((result) => {
97+
return result.memory;
98+
})
99+
.filter(isPositiveNumber)
100+
);
101+
102+
return { medianDuration, medianMemory };
103+
}

benchmarks/runtime.bench.js renamed to benchmarks/runtime.bench.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
import { Linter } from 'eslint';
22
import assert from 'node:assert';
3-
import { times } from 'rambda';
4-
import mochaPlugin from '../index.js';
3+
import mochaPlugin from '../source/plugin.js';
54
import { cpuSpeed, runSyncBenchmark } from './measure.js';
65

7-
const allRules = mochaPlugin.configs.all.rules;
6+
const { configs: { all } = {} } = mochaPlugin;
87

9-
function lintManyFilesWithAllRecommendedRules(options) {
8+
type Options = {
9+
readonly numberOfFiles: number;
10+
};
11+
12+
function lintManyFilesWithAllRecommendedRules(options: Readonly<Options>): void {
1013
const { numberOfFiles } = options;
1114
const linter = new Linter();
1215

13-
const config = {
14-
plugins: { mocha: mochaPlugin },
15-
rules: allRules,
16+
const config: Linter.Config[] = [all as Linter.Config, {
1617
languageOptions: {
1718
sourceType: 'script',
1819
ecmaVersion: 2018
1920
}
20-
};
21+
}];
2122

22-
times((index) => {
23+
Array.from({ length: numberOfFiles }).forEach((_value, index) => {
2324
const codeToLint = `
2425
'use strict';
2526
const assert = require('assert');
@@ -73,7 +74,7 @@ function lintManyFilesWithAllRecommendedRules(options) {
7374
`;
7475

7576
linter.verify(codeToLint, config);
76-
}, numberOfFiles);
77+
});
7778
}
7879

7980
const iterations = 50;
@@ -95,7 +96,7 @@ describe('runtime', function () {
9596
});
9697

9798
it('should not consume more memory as the defined budget to lint many files with the recommended config', function () {
98-
const budget = 2_250_000;
99+
const budget = 4_500_000;
99100

100101
const { medianMemory } = runSyncBenchmark(() => {
101102
lintManyFilesWithAllRecommendedRules({ numberOfFiles: 350 });

benchmarks/startup.bench.js renamed to benchmarks/startup.bench.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('startup / require time', function () {
99
const budget = cpuAgnosticBudget / cpuSpeed;
1010

1111
const { medianDuration } = await runAsyncBenchmark(async () => {
12-
await importFresh('../index.js');
12+
await importFresh('../source/plugin.js');
1313
}, iterations);
1414

1515
assert.strictEqual(
@@ -20,10 +20,10 @@ describe('startup / require time', function () {
2020
});
2121

2222
it('should not consume more memory as the defined budget', async function () {
23-
const budget = 225_000;
23+
const budget = 350_000;
2424

2525
const { medianMemory } = await runAsyncBenchmark(async () => {
26-
await importFresh('../index.js');
26+
await importFresh('../source/plugin.js');
2727
}, iterations);
2828

2929
assert.strictEqual(

benchmarks/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../tsconfig.base.json",
3+
"compilerOptions": {
4+
"tsBuildInfoFile": "../target/buildcache/benchmarks.tsbuildinfo",
5+
"rootDir": "..",
6+
"types": ["node", "mocha"]
7+
},
8+
"include": ["./**/*.ts"],
9+
"references": [{ "path": "../source/tsconfig.sources.json" }]
10+
}

cspell.config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"xcustom",
88
"Toru",
99
"Nagashima",
10-
"rambda",
1110
"quuux"
1211
]
1312
}

0 commit comments

Comments
 (0)