From c30c4ec908206c4da42b688a85ff042c72150a6c Mon Sep 17 00:00:00 2001 From: Evan Sosenko Date: Fri, 22 Sep 2023 16:17:33 -0700 Subject: [PATCH] Add eslint to generate-code --- generate-routes.ts | 23 +++++++++++++++++++++-- package-lock.json | 20 ++++++++++++++++++-- package.json | 1 + 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/generate-routes.ts b/generate-routes.ts index 95ed2dd5..6f2436e2 100644 --- a/generate-routes.ts +++ b/generate-routes.ts @@ -2,6 +2,7 @@ import { writeFile } from 'node:fs/promises' import { resolve } from 'node:path' import { camelCase, pascalCase, snakeCase } from 'change-case' +import { ESLint } from 'eslint' import { format, resolveConfig } from 'prettier' interface Route { @@ -140,12 +141,30 @@ const exampleRoute: Route = { const write = async (data: string, ...path: string[]): Promise => { const filepath = resolve(...path) + const eslint = new ESLint({ fix: true }) + const prettierConfig = await resolveConfig(filepath) if (prettierConfig == null) { throw new Error('Failed to resolve Prettier config') } - const output = await format(data, { ...prettierConfig, filepath }) - await writeFile(filepath, output) + + const [linted] = await eslint.lintText(data) + + if (linted == null) { + throw new Error('ESLint returned empty results') + } + + if (linted.fatalErrorCount > 0) { + // TODO: ESLint is failing to parse, seems it may not be loading the right config. + // throw new Error( + // `ESLint returned fatal errors: ${JSON.stringify(linted.messages)}`, + // ) + } + + const output = linted.output ?? linted.source ?? '' + + const prettyData = await format(output, { ...prettierConfig, filepath }) + await writeFile(filepath, prettyData) } const routeRootPath = resolve('src', 'lib', 'seam', 'connect', 'routes') diff --git a/package-lock.json b/package-lock.json index 6e6f4340..a4be6bf0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ }, "devDependencies": { "@seamapi/types": "^1.14.0", + "@types/eslint": "^8.44.2", "@types/node": "^18.11.18", "ava": "^5.0.1", "c8": "^8.0.0", @@ -804,6 +805,22 @@ "zod": "^3.21.4" } }, + "node_modules/@types/eslint": { + "version": "8.44.2", + "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.44.2.tgz", + "integrity": "sha512-sdPRb9K6iL5XZOmBubg8yiFp5yS/JdUDQsq5e6h95km91MCYMuvp7mh1fjPEYUhvHepKpZOjnEaMBR4PxjWDzg==", + "dev": true, + "dependencies": { + "@types/estree": "*", + "@types/json-schema": "*" + } + }, + "node_modules/@types/estree": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.1.tgz", + "integrity": "sha512-LG4opVs2ANWZ1TJoKc937iMmNstM/d0ae1vNbnBvBhqCSezgVUOzcLCqbI5elV8Vy6WKwKjaqR+zO9VKirBBCA==", + "dev": true + }, "node_modules/@types/istanbul-lib-coverage": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz", @@ -814,8 +831,7 @@ "version": "7.0.13", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.13.tgz", "integrity": "sha512-RbSSoHliUbnXj3ny0CNFOoxrIDV6SUGyStHsvDqosw6CkdPV8TtWGlfecuK4ToyMEAql6pzNxgCFKanovUzlgQ==", - "dev": true, - "peer": true + "dev": true }, "node_modules/@types/json5": { "version": "0.0.29", diff --git a/package.json b/package.json index da8c7828..9de30067 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ }, "devDependencies": { "@seamapi/types": "^1.14.0", + "@types/eslint": "^8.44.2", "@types/node": "^18.11.18", "ava": "^5.0.1", "c8": "^8.0.0",