Skip to content

Commit da83698

Browse files
authored
Generate types rather than relying on type-fest (#209)
1 parent d9bef83 commit da83698

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 20
14+
- 18
15+
- 16
1316
- 14
1417
- 12
1518
- 10

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
yarn.lock
33
.cache
4+
/index.d.ts

index.d.ts

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

index.test-d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {expectType, expectError} from 'tsd';
1+
import {expectType, expectError, expectAssignable} from 'tsd';
22
import {ReadonlyDeep} from 'type-fest';
3-
import globals = require('.');
3+
import globals from '.';
44

5-
expectType<ReadonlyDeep<Record<string, Record<string, boolean>>>>(globals);
6-
expectType<boolean>(globals.builtin.Array);
5+
expectAssignable<ReadonlyDeep<Record<string, Record<string, boolean>>>>(globals);
6+
expectType<false>(globals.builtin.Array);
77
expectError((globals.builtin.Array = true));

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"node": ">=8"
1616
},
1717
"scripts": {
18-
"test": "xo && ava",
19-
"update-builtin-globals": "node scripts/get-builtin-globals.mjs"
18+
"test": "xo && ava && tsd",
19+
"prepare": "npm run --silent update-types",
20+
"update-builtin-globals": "node scripts/get-builtin-globals.mjs",
21+
"update-types": "node scripts/generate-types.mjs > index.d.ts"
2022
},
2123
"files": [
2224
"index.js",
@@ -33,13 +35,11 @@
3335
"eslint",
3436
"environments"
3537
],
36-
"dependencies": {
37-
"type-fest": "^0.20.2"
38-
},
3938
"devDependencies": {
4039
"ava": "^2.4.0",
4140
"cheerio": "^1.0.0-rc.12",
42-
"tsd": "^0.14.0",
41+
"tsd": "^0.30.4",
42+
"type-fest": "^4.10.2",
4343
"xo": "^0.36.1"
4444
},
4545
"xo": {

scripts/generate-types.mjs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'node:fs/promises';
2+
3+
const DATA_FILE = new URL('../globals.json', import.meta.url);
4+
5+
const globals = JSON.parse(await fs.readFile(DATA_FILE));
6+
7+
const getGroupTypeName = (group) => `Globals${group[0].toUpperCase() + group.slice(1).replaceAll('-', '')}`;
8+
9+
const groups = {};
10+
const output = [
11+
'// This file is autogenerated by scripts/generate-types.mjs',
12+
'// Do NOT modify this file manually\n'
13+
];
14+
15+
for (const group in globals) {
16+
const groupType = getGroupTypeName(group);
17+
groups[group] = groupType;
18+
19+
output.push(`type ${getGroupTypeName(group)} = {`);
20+
21+
for (const [rule, status] of Object.entries(globals[group])) {
22+
output.push(` readonly '${rule}': ${status};`);
23+
}
24+
25+
output.push(`}\n`);
26+
}
27+
28+
output.push(`type Globals = {`);
29+
30+
for (const [group, groupType] of Object.entries(groups)) {
31+
output.push(` readonly '${group}': ${groupType};`);
32+
}
33+
34+
output.push(`}\n`);
35+
output.push(`declare const globals: Globals;\n`);
36+
output.push(`export = globals;`);
37+
38+
console.log(output.join('\n'));

0 commit comments

Comments
 (0)