Skip to content

Commit 003fe9c

Browse files
committed
chore: add type-safe ESLint
1 parent 77513d9 commit 003fe9c

33 files changed

+297
-115
lines changed

eslint.config.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,34 @@ import { defineConfig, globalIgnores } from "eslint/config";
22
import js from "@eslint/js";
33
import globals from "globals";
44
import tseslint from "typescript-eslint";
5-
import eslintConfigPrettier from "eslint-config-prettier/flat";
5+
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
6+
import jestPlugin from "eslint-plugin-jest";
67

7-
const files = ["src/**/*.ts", "scripts/**/*.ts", "tests/**/*.test.ts", "eslint.config.js", "jest.config.js"];
8+
const testFiles = ["tests/**/*.test.ts", "tests/**/*.ts"];
9+
10+
const files = [...testFiles, "src/**/*.ts", "scripts/**/*.ts"];
811

912
export default defineConfig([
1013
{ files, plugins: { js }, extends: ["js/recommended"] },
1114
{ files, languageOptions: { globals: globals.node } },
15+
{
16+
files: testFiles,
17+
plugins: {
18+
jest: jestPlugin,
19+
},
20+
languageOptions: {
21+
globals: {
22+
...globals.node,
23+
...jestPlugin.environments.globals.globals,
24+
},
25+
},
26+
},
1227
tseslint.configs.recommendedTypeChecked,
1328
{
29+
files,
1430
languageOptions: {
1531
parserOptions: {
16-
projectService: true,
32+
project: "./tsconfig.lint.json",
1733
tsconfigRootDir: import.meta.dirname,
1834
},
1935
},
@@ -25,11 +41,14 @@ export default defineConfig([
2541
"@typescript-eslint/no-non-null-assertion": "error",
2642
},
2743
},
28-
// Ignore features specific to TypeScript resolved rules
29-
tseslint.config({
30-
// TODO: Configure tests and scripts to work with this.
31-
ignores: ["eslint.config.js", "jest.config.js", "tests/**/*.ts", "scripts/**/*.ts"],
32-
}),
33-
globalIgnores(["node_modules", "dist", "src/common/atlas/openapi.d.ts", "coverage"]),
34-
eslintConfigPrettier,
44+
globalIgnores([
45+
"node_modules",
46+
"dist",
47+
"src/common/atlas/openapi.d.ts",
48+
"coverage",
49+
"global.d.ts",
50+
"eslint.config.js",
51+
"jest.config.js",
52+
]),
53+
eslintPluginPrettierRecommended,
3554
]);

global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "jest-extended";

package-lock.json

Lines changed: 110 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"@types/simple-oauth2": "^5.0.7",
4242
"@types/yargs-parser": "^21.0.3",
4343
"eslint": "^9.24.0",
44-
"eslint-config-prettier": "^10.1.1",
44+
"eslint-config-prettier": "^10.1.2",
45+
"eslint-plugin-jest": "^28.11.0",
46+
"eslint-plugin-prettier": "^5.2.6",
4547
"globals": "^16.0.0",
4648
"jest": "^29.7.0",
4749
"jest-environment-node": "^29.7.0",

scripts/apply.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ function findObjectFromRef<T>(obj: T | OpenAPIV3_1.ReferenceObject, openapi: Ope
99
}
1010
const paramParts = ref.split("/");
1111
paramParts.shift(); // Remove the first part which is always '#'
12-
let foundObj: any = openapi; // eslint-disable-line @typescript-eslint/no-explicit-any
12+
13+
let foundObj: Record<string, unknown> = openapi;
1314
while (true) {
1415
const part = paramParts.shift();
1516
if (!part) {
1617
break;
1718
}
18-
foundObj = foundObj[part];
19+
20+
foundObj = foundObj[part] as Record<string, unknown>;
1921
}
2022
return foundObj as T;
2123
}
@@ -28,7 +30,7 @@ async function main() {
2830
process.exit(1);
2931
}
3032

31-
const specFile = (await fs.readFile(spec, "utf8")) as string;
33+
const specFile = await fs.readFile(spec as string, "utf8");
3234

3335
const operations: {
3436
path: string;
@@ -42,7 +44,7 @@ async function main() {
4244
const openapi = JSON.parse(specFile) as OpenAPIV3_1.Document;
4345
for (const path in openapi.paths) {
4446
for (const method in openapi.paths[path]) {
45-
const operation: OpenAPIV3_1.OperationObject = openapi.paths[path][method];
47+
const operation = openapi.paths[path][method] as OpenAPIV3_1.OperationObject;
4648

4749
if (!operation.operationId || !operation.tags?.length) {
4850
continue;
@@ -101,9 +103,9 @@ async function main() {
101103
})
102104
.join("\n");
103105

104-
const templateFile = (await fs.readFile(file, "utf8")) as string;
106+
const templateFile = await fs.readFile(file as string, "utf8");
105107
const templateLines = templateFile.split("\n");
106-
let outputLines: string[] = [];
108+
const outputLines: string[] = [];
107109
let addLines = true;
108110
for (const line of templateLines) {
109111
if (line.includes("DO NOT EDIT. This is auto-generated code.")) {
@@ -120,7 +122,7 @@ async function main() {
120122
}
121123
const output = outputLines.join("\n");
122124

123-
await fs.writeFile(file, output, "utf8");
125+
await fs.writeFile(file as string, output, "utf8");
124126
}
125127

126128
main().catch((error) => {

scripts/filter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ async function readStdin() {
88
reject(err);
99
});
1010
process.stdin.on("data", (chunk) => {
11+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
1112
data += chunk;
1213
});
1314
process.stdin.on("end", () => {
@@ -42,8 +43,8 @@ function filterOpenapi(openapi: OpenAPIV3_1.Document): OpenAPIV3_1.Document {
4243
for (const path in openapi.paths) {
4344
const filteredMethods = {} as OpenAPIV3_1.PathItemObject;
4445
for (const method in openapi.paths[path]) {
45-
if (allowedOperations.includes(openapi.paths[path][method].operationId)) {
46-
filteredMethods[method] = openapi.paths[path][method];
46+
if (allowedOperations.includes((openapi.paths[path][method] as { operationId: string }).operationId)) {
47+
filteredMethods[method] = openapi.paths[path][method] as OpenAPIV3_1.OperationObject;
4748
}
4849
}
4950
if (Object.keys(filteredMethods).length > 0) {

0 commit comments

Comments
 (0)