8000 chore: use TLA in ESM scripts rather than async main().catch() (#11218) · aryaemami59/typescript-eslint@05014ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 05014ff

Browse files
chore: use TLA in ESM scripts rather than async main().catch() (typescript-eslint#11218)
use TLA in ESM scripts
1 parent 220c38c commit 05014ff

File tree

8 files changed

+624
-692
lines changed

8 files changed

+624
-692
lines changed

packages/eslint-plugin/tools/generate-breaking-changes.mts

Lines changed: 49 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,42 @@
1-
import type {
2-
ESLintPluginRuleModule,
3-
TypeScriptESLintRules,
4-
} from '@typescript-eslint/eslint-plugin/use-at-your-own-risk/rules';
1+
import type { TypeScriptESLintRules } from '@typescript-eslint/eslint-plugin/use-at-your-own-risk/rules';
52

63
import { fetch } from 'cross-fetch';
7-
// markdown-table is ESM, hence this file needs to be `.mts`
84
import { markdownTable } from 'markdown-table';
95

10-
async function main(): Promise<void> {
11-
const rulesImport = await import('../src/rules/index.js');
12-
/*
13-
weird TS resolution which adds an additional default layer in the type like:
14-
{ default: { default: Rules }}
15-
instead of just
16-
{ default: Rules }
17-
*/
18-
const rules = rulesImport.default as unknown as Record<
19-
string,
20-
ESLintPluginRuleModule
21-
>;
6+
import rulesImport from '../src/rules/index.js';
227

23-
// Annotate which rules are new since the last version
24-
async function getNewRulesAsOfMajorVersion(
25-
oldVersion: string,
26-
): Promise<Set<string>> {
27-
// 1. Get the current list of rules (already done)
28-
const newRuleNames = Object.keys(rules);
8+
// Annotate which rules are new since the last version
9+
async function getNewRulesAsOfMajorVersion(
10+
oldVersion: string,
11+
): Promise<Set<string>> {
12+
// 1. Get the current list of rules (already done)
13+
const newRuleNames = Object.keys(rulesImport);
2914

30-
// 2. Retrieve the old version of typescript-eslint from unpkg
31-
const oldUrl = `https://unpkg.com/@typescript-eslint/eslint-plugin@${oldVersion}/dist/configs/all.js`;
32-
const oldFileText = await (await fetch(oldUrl)).text();
33-
const oldObjectText = oldFileText.substring(
34-
oldFileText.indexOf('{'),
35-
oldFileText.lastIndexOf('}') + 1,
36-
);
37-
// Normally we wouldn't condone using the 'eval' API...
38-
// But this is an internal-only script and it's the easiest way to convert
39-
// the JS raw text into a runtime object. 🤷
40-
let oldRulesObject!: { rules: TypeScriptESLintRules };
41-
eval(`oldRulesObject = ${oldObjectText}`);
42-
const oldRuleNames = new Set(Object.keys(oldRulesObject.rules));
15+
// 2. Retrieve the old version of typescript-eslint from unpkg
16+
const oldUrl = `https://unpkg.com/@typescript-eslint/eslint-plugin@${oldVersion}/dist/configs/all.js`;
17+
const oldFileText = await (await fetch(oldUrl)).text();
18+
const oldObjectText = oldFileText.substring(
19+
oldFileText.indexOf('{'),
20+
oldFileText.lastIndexOf('}') + 1,
21+
);
22+
// Normally we wouldn't condone using the 'eval' API...
23+
// But this is an internal-only script and it's the easiest way to convert
24+
// the JS raw text into a runtime object. 🤷
25+
let oldRulesObject!: { rules: TypeScriptESLintRules };
26+
eval(`oldRulesObject = ${oldObjectText}`);
27+
const oldRuleNames = new Set(Object.keys(oldRulesObject.rules));
4328

44-
// 3. Get the keys that exist in (1) (new version) and not (2) (old version)
45-
return new Set(
46-
newRuleNames.filter(
47-
newRuleName => !oldRuleNames.has(`@typescript-eslint/${newRuleName}`),
48-
),
49-
);
50-
}
29+
// 3. Get the keys that exist in (1) (new version) and not (2) (old version)
30+
return new Set(
31+
newRuleNames.filter(
32+
newRuleName => !oldRuleNames.has(`@typescript-eslint/${newRuleName}`),
33+
),
34+
);
35+
}
5136

52-
const newRuleNames = await getNewRulesAsOfMajorVersion('5.0.0');
37+
const newRuleNames = await getNewRulesAsOfMajorVersion('5.0.0');
5338

54-
console.log(`## Table Key
39+
console.log(`## Table Key
5540
5641
<table>
5742
<thead>
@@ -132,28 +117,23 @@ async function main(): Promise<void> {
132117
> Hint: search for 🆕 to find newly added rules, and ➕ or ➖ to see config changes.
133118
`);
134119

135-
console.log(
136-
markdownTable([
137-
['Rule', 'Status', 'TC', 'Ext', "Rec'd", 'Strict', 'Style'],
138-
...Object.entries(rules).map(([ruleName, { meta }]) => {
139-
const { deprecated } = meta;
140-
const { extendsBaseRule, recommended, requiresTypeChecking } =
141-
meta.docs;
142-
143-
return [
144-
`[\`${ruleName}\`](https://typescript-eslint.io/rules/${ruleName})`,
145-
newRuleNames.has(ruleName) ? '🆕' : deprecated ? '💀' : '',
146-
requiresTypeChecking ? '💭' : '',
147-
extendsBaseRule ? '🧱' : '',
148-
recommended === 'recommended' ? '🟩' : '',
149-
recommended === 'strict' ? '🔵' : '',
150-
recommended === 'stylistic' ? '🔸' : '',
151-
];
152-
}),
153-
]),
154-
);
155-
}
120+
console.log(
121+
markdownTable([
122+
['Rule', 'Status', 'TC', 'Ext', "Rec'd", 'Strict', 'Style'],
123+
...Object.entries(rulesImport).map(([ruleName, { meta }]) => {
124+
const { deprecated } = meta;
125+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- all of our rules have meta.docs
126+
const { extendsBaseRule, recommended, requiresTypeChecking } = meta.docs!;
156127

157-
main().catch((error: unknown) => {
158-
console.error(error);
159-
});
128+
return [
129+
`[\`${ruleName}\`](https://typescript-eslint.io/rules/${ruleName})`,
130+
newRuleNames.has(ruleName) ? '🆕' : deprecated ? '💀' : '',
131+
requiresTypeChecking ? '💭' : '',
132+
extendsBaseRule ? '🧱' : '',
133+
recommended === 'recommended' ? '🟩' : '',
134+
recommended === 'strict' ? '🔵' : '',
135+
recommended === 'stylistic' ? '🔸' : '',
136+
];
137+
}),
138+
]),
139+
);

packages/types/tools/copy-ast-spec.mts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,11 @@ async function copyFile(
7272
console.log('Copied', fileName);
7373
}
7474

75-
async function main(): Promise<void> {
76-
if (process.env.SKIP_AST_SPEC_REBUILD) {
77-
// ensure the package is built
78-
await execAsync('yarn', ['build'], { cwd: AST_SPEC_PATH });
79-
}
80-
81-
await copyFile('dist', 'ast-spec.ts', code =>
82-
code.replaceAll('export declare enum', 'export enum'),
83-
);
75+
if (process.env.SKIP_AST_SPEC_REBUILD) {
76+
// ensure the package is built
77+
await execAsync('yarn', ['build'], { cwd: AST_SPEC_PATH });
8478
}
8579

86-
main().catch((error: unknown) => {
87-
console.error(error);
88-
process.exitCode = 1;
89-
});
80+
await copyFile('dist', 'ast-spec.ts', code =>
81+
code.replaceAll('export declare enum', 'export enum'),
82+
);

packages/website-eslint/build.mts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-process-exit, no-console */
1+
/* eslint-disable no-console */
22

33
import * as esbuild from 'esbuild';
44
import * as fs from 'node:fs/promises';
@@ -170,12 +170,5 @@ async function buildPackage(name: string, file: string): Promise<void> {
170170
}
171171

172172
console.time('building eslint for web');
173-
174-
buildPackage('index', './src/index.js')
175-
.then(() => {
176-
console.timeEnd('building eslint for web');
177-
})
178-
.catch((e: unknown) => {
179-
console.error(String(e));
180-
process.exit(1);
181-
});
173+
await buildPackage('index', './src/index.js');
174+
console.timeEnd('building eslint for web');

packages/website/tools/generate-website-dts.mts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,22 @@ function processFiles(text: string): string {
7979
return result;
8080
}
8181

82-
async function main(): Promise<void> {
83-
const vendor = path.join(__dirname, '..', 'src', 'vendor');
82+
const vendor = path.join(__dirname, '..', 'src', 'vendor');
8483

85-
console.log('Cleaning...');
86-
await rimraf(vendor);
87-
await makeDirectory(vendor);
84+
console.log('Cleaning...');
85+
await rimraf(vendor);
86+
await makeDirectory(vendor);
8887

89-
// TS-VFS
90-
await getFileAndStoreLocally(
91-
'/js/sandbox/vendor/typescript-vfs.d.ts',
92-
path.join(vendor, 'typescript-vfs.d.ts'),
93-
processFiles,
94-
);
95-
96-
// Sandbox
97-
await getFileAndStoreLocally(
98-
'/js/sandbox/index.d.ts',
99-
path.join(vendor, 'sandbox.d.ts'),
100-
processFiles,
101-
);
102-
}
88+
// TS-VFS
89+
await getFileAndStoreLocally(
90+
'/js/sandbox/vendor/typescript-vfs.d.ts',
91+
path.join(vendor, 'typescript-vfs.d.ts'),
92+
processFiles,
93+
);
10394

104-
main().catch((error: unknown) => {
105-
console.error(error);
106-
process.exitCode = 1;
107-
});
95+
// Sandbox
96+
await getFileAndStoreLocally(
97+
'/js/sandbox/index.d.ts',
98+
path.join(vendor, 'sandbox.d.ts'),
99+
processFiles,
100+
);

0 commit comments

Comments
 (0)
0