Skip to content

chore: add type-safe ESLint #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@ import { defineConfig, globalIgnores } from "eslint/config";
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import eslintConfigPrettier from "eslint-config-prettier/flat";
import eslintPluginPrettierRecommended from "eslint-plugin-prettier/recommended";
import jestPlugin from "eslint-plugin-jest";

const files = ["src/**/*.ts", "scripts/**/*.ts", "tests/**/*.test.ts", "eslint.config.js", "jest.config.js"];
const testFiles = ["tests/**/*.test.ts", "tests/**/*.ts"];

const files = [...testFiles, "src/**/*.ts", "scripts/**/*.ts"];

export default defineConfig([
{ files, plugins: { js }, extends: ["js/recommended"] },
{ files, languageOptions: { globals: globals.node } },
{
files: testFiles,
plugins: {
jest: jestPlugin,
},
languageOptions: {
globals: {
...globals.node,
...jestPlugin.environments.globals.globals,
},
},
},
tseslint.configs.recommendedTypeChecked,
{
files,
languageOptions: {
parserOptions: {
projectService: true,
project: "./tsconfig.lint.json",
tsconfigRootDir: import.meta.dirname,
},
},
Expand All @@ -25,11 +41,14 @@ export default defineConfig([
"@typescript-eslint/no-non-null-assertion": "error",
},
},
// Ignore features specific to TypeScript resolved rules
tseslint.config({
// TODO: Configure tests and scripts to work with this.
ignores: ["eslint.config.js", "jest.config.js", "tests/**/*.ts", "scripts/**/*.ts"],
}),
globalIgnores(["node_modules", "dist", "src/common/atlas/openapi.d.ts", "coverage"]),
eslintConfigPrettier,
globalIgnores([
"node_modules",
"dist",
"src/common/atlas/openapi.d.ts",
"coverage",
"global.d.ts",
"eslint.config.js",
"jest.config.js",
]),
eslintPluginPrettierRecommended,
]);
1 change: 1 addition & 0 deletions global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "jest-extended";
111 changes: 110 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
"@types/simple-oauth2": "^5.0.7",
"@types/yargs-parser": "^21.0.3",
"eslint": "^9.24.0",
"eslint-config-prettier": "^10.1.1",
"eslint-config-prettier": "^10.1.2",
"eslint-plugin-jest": "^28.11.0",
"eslint-plugin-prettier": "^5.2.6",
"globals": "^16.0.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
Expand Down
16 changes: 9 additions & 7 deletions scripts/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ function findObjectFromRef<T>(obj: T | OpenAPIV3_1.ReferenceObject, openapi: Ope
}
const paramParts = ref.split("/");
paramParts.shift(); // Remove the first part which is always '#'
let foundObj: any = openapi; // eslint-disable-line @typescript-eslint/no-explicit-any

let foundObj: Record<string, unknown> = openapi;
while (true) {
const part = paramParts.shift();
if (!part) {
break;
}
foundObj = foundObj[part];

foundObj = foundObj[part] as Record<string, unknown>;
}
return foundObj as T;
}
Expand All @@ -28,7 +30,7 @@ async function main() {
process.exit(1);
}

const specFile = (await fs.readFile(spec, "utf8")) as string;
const specFile = await fs.readFile(spec as string, "utf8");

const operations: {
path: string;
Expand All @@ -42,7 +44,7 @@ async function main() {
const openapi = JSON.parse(specFile) as OpenAPIV3_1.Document;
for (const path in openapi.paths) {
for (const method in openapi.paths[path]) {
const operation: OpenAPIV3_1.OperationObject = openapi.paths[path][method];
const operation = openapi.paths[path][method] as OpenAPIV3_1.OperationObject;

if (!operation.operationId || !operation.tags?.length) {
continue;
Expand Down Expand Up @@ -101,9 +103,9 @@ async function main() {
})
.join("\n");

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

await fs.writeFile(file, output, "utf8");
await fs.writeFile(file as string, output, "utf8");
}

main().catch((error) => {
Expand Down
5 changes: 3 additions & 2 deletions scripts/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ async function readStdin() {
reject(err);
});
process.stdin.on("data", (chunk) => {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
data += chunk;
});
process.stdin.on("end", () => {
Expand Down Expand Up @@ -42,8 +43,8 @@ function filterOpenapi(openapi: OpenAPIV3_1.Document): OpenAPIV3_1.Document {
for (const path in openapi.paths) {
const filteredMethods = {} as OpenAPIV3_1.PathItemObject;
for (const method in openapi.paths[path]) {
if (allowedOperations.includes(openapi.paths[path][method].operationId)) {
filteredMethods[method] = openapi.paths[path][method];
if (allowedOperations.includes((openapi.paths[path][method] as { operationId: string }).operationId)) {
filteredMethods[method] = openapi.paths[path][method] as OpenAPIV3_1.OperationObject;
}
}
if (Object.keys(filteredMethods).length > 0) {
Expand Down
Loading
Loading