From eefaaadc6d0a515448e1284e64dc86e474cbd43e Mon Sep 17 00:00:00 2001 From: bbourgeois Date: Thu, 31 Oct 2024 18:19:06 +0100 Subject: [PATCH] chore: configure ESLint to new configuration including tests --- .eslintrc.js | 26 -------- eslint.config.mjs | 42 +++++++++++++ package.json | 13 ++-- src/nutripatrol.ts | 5 +- tsconfig.build.json | 7 +++ tsconfig.json | 4 +- yarn.lock | 143 +++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 205 insertions(+), 35 deletions(-) delete mode 100644 .eslintrc.js create mode 100644 eslint.config.mjs create mode 100644 tsconfig.build.json diff --git a/.eslintrc.js b/.eslintrc.js deleted file mode 100644 index 02f419a3..00000000 --- a/.eslintrc.js +++ /dev/null @@ -1,26 +0,0 @@ -module.exports = { - env: { - browser: true, - es2021: true, - node: true, - }, - extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], - overrides: [ - { - env: { - node: true, - }, - files: [".eslintrc.{js,cjs}"], - parserOptions: { - sourceType: "script", - }, - }, - ], - parser: "@typescript-eslint/parser", - parserOptions: { - ecmaVersion: "latest", - sourceType: "module", - }, - plugins: ["@typescript-eslint"], - rules: {}, -}; diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 00000000..a1b8d4e3 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,42 @@ +import globals from "globals"; +import eslint from "@eslint/js"; +import tseslint from "typescript-eslint"; +import tsParser from "@typescript-eslint/parser"; + +export default tseslint.config( + { + files: ["**/*.ts"], + languageOptions: { + parser: tsParser, + parserOptions: { + project: "./tsconfig.json", + }, + }, + plugins: { + "@typescript-eslint": tseslint, + }, + rules: { + ...eslint.configs.recommended.rules, // Include recommended JS rules + ...tseslint.configs.recommended.rules, // Include recommended TS rules + }, + }, + { + languageOptions: { + globals: { ...globals.browser, ...globals.node, ...globals.jest }, + }, + }, + { + // Global ignore patterns + ignores: [ + "dist", + "docs", + ".yarn", + "node_modules", + "**/node_modules", + "src/schemas", + "coverage", + "*.config.mjs", + "jest.config.ts", + ], + } +); diff --git a/package.json b/package.json index 71ebf387..28e08541 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "types": "dist/main.d.ts", "scripts": { "prepack": "yarn run build", - "build": "tsc", + "build": "tsc -p tsconfig.build.json", "build:docs": "typedoc src/main.ts", "commitmsg": "commitlint -quiet=0 --extends=@commitlint/config-conventional -e", "precommit": "standard `git diff --name-only --staged --relative | grep '.js$'`", @@ -20,7 +20,8 @@ "api:folksonomy": "openapi-typescript https://api.folksonomy.openfoodfacts.org/openapi.json --output src/schemas/folksonomy.ts", "api:nutripatrol": "openapi-typescript https://nutripatrol.openfoodfacts.org/api/openapi.json --output src/schemas/nutripatrol.ts", "check": "yarn run lint && yarn run test", - "lint": "prettier --check . && eslint .", + "lint": "eslint .", + "lint:fix": "eslint --fix .", "format": "prettier --write .", "fix": "standard --fix", "test": "jest", @@ -56,6 +57,8 @@ "devDependencies": { "@commitlint/cli": "^19.3.0", "@commitlint/config-conventional": "^19.2.2", + "@eslint/eslintrc": "^3.1.0", + "@eslint/js": "^9.13.0", "@jest/globals": "^29.7.0", "@types/jest": "^29.5.14", "@types/node": "^22.8.5", @@ -63,13 +66,15 @@ "@typescript-eslint/parser": "^7.8.0", "eslint": "^9.1.1", "formdata-node": "^6.0.3", + "globals": "^15.11.0", "jest": "^29.7.0", "openapi-typescript": "^6.7.4", "prettier": "^3.1.1", "ts-jest": "^29.2.5", "ts-node": "^10.9.2", "typedoc": "^0.26.10", - "typescript": "^5.6.3" + "typescript": "^5.6.3", + "typescript-eslint": "^8.12.2" }, "packageManager": "yarn@4.0.2" -} +} \ No newline at end of file diff --git a/src/nutripatrol.ts b/src/nutripatrol.ts index da43f38a..18cf8bad 100644 --- a/src/nutripatrol.ts +++ b/src/nutripatrol.ts @@ -58,6 +58,7 @@ export class NutriPatrol { try { const fct = methods[method] as any; const res = await fct(path as any, options as any); + let errorDetails: NutriPatrolError | undefined; if (!res.response.ok) { switch (res.response.status) { @@ -70,7 +71,7 @@ export class NutriPatrol { }, } as NutriPatrolError; default: - const errorDetails = await res.response.json(); + errorDetails = await res.response.json(); return { error: { statusCode: res.response.status, @@ -93,7 +94,7 @@ export class NutriPatrol { } return data; - } catch (error) { + } catch { return { error: { statusCode: 500, diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 00000000..92be1858 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "rootDir": "./src" + }, + "include": ["src"] +} diff --git a/tsconfig.json b/tsconfig.json index e0bfba1f..3e7d3113 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,7 @@ }, "forceConsistentCasingInFileNames": true, "target": "ES2015", - "rootDir": "./src", + "rootDir": ".", "module": "CommonJS", "lib": ["ES2022", "DOM"], "declaration": true, @@ -13,5 +13,5 @@ "strict": true, "esModuleInterop": true }, - "exclude": ["node_modules", "dist", "tests", "jest.config.ts"] + "exclude": ["node_modules", "dist", "jest.config.ts", "coverage", "docs"] } diff --git a/yarn.lock b/yarn.lock index f5d4bf81..49dcd56b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -678,7 +678,7 @@ __metadata: languageName: node linkType: hard -"@eslint/js@npm:9.13.0": +"@eslint/js@npm:9.13.0, @eslint/js@npm:^9.13.0": version: 9.13.0 resolution: "@eslint/js@npm:9.13.0" checksum: aa7a4c45044a6cf6e14666ecc0b56ad41c80f022bd4718620b4a7e3d892111312f4e4ac4787fd11b3bf5abdb6ff9a95fdae7e73ef790528f150d86e9be1754a2 @@ -1379,6 +1379,29 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/eslint-plugin@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/eslint-plugin@npm:8.12.2" + dependencies: + "@eslint-community/regexpp": "npm:^4.10.0" + "@typescript-eslint/scope-manager": "npm:8.12.2" + "@typescript-eslint/type-utils": "npm:8.12.2" + "@typescript-eslint/utils": "npm:8.12.2" + "@typescript-eslint/visitor-keys": "npm:8.12.2" + graphemer: "npm:^1.4.0" + ignore: "npm:^5.3.1" + natural-compare: "npm:^1.4.0" + ts-api-utils: "npm:^1.3.0" + peerDependencies: + "@typescript-eslint/parser": ^8.0.0 || ^8.0.0-alpha.0 + eslint: ^8.57.0 || ^9.0.0 + peerDependenciesMeta: + typescript: + optional: true + checksum: 3ba36240bdb1f278050bbde377d858e67511bf1de31a1ab9d99c5b560204e7d448c33d1487cd5eba9bfb823abff2eccf4532f42065cadad6772b68f91006ddc2 + languageName: node + linkType: hard + "@typescript-eslint/eslint-plugin@npm:^7.0.0": version: 7.16.1 resolution: "@typescript-eslint/eslint-plugin@npm:7.16.1" @@ -1402,6 +1425,24 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/parser@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/parser@npm:8.12.2" + dependencies: + "@typescript-eslint/scope-manager": "npm:8.12.2" + "@typescript-eslint/types": "npm:8.12.2" + "@typescript-eslint/typescript-estree": "npm:8.12.2" + "@typescript-eslint/visitor-keys": "npm:8.12.2" + debug: "npm:^4.3.4" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + peerDependenciesMeta: + typescript: + optional: true + checksum: 257cfbe13bd1a3a2c83e0b3fd2d441a51181ffd57f2e98112217668383fc28de995bfca6d7c4d2c04c6347ee6a376fe1d87eb6ce60d7383cda1044626d794185 + languageName: node + linkType: hard + "@typescript-eslint/parser@npm:^7.8.0": version: 7.8.0 resolution: "@typescript-eslint/parser@npm:7.8.0" @@ -1440,6 +1481,16 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/scope-manager@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/scope-manager@npm:8.12.2" + dependencies: + "@typescript-eslint/types": "npm:8.12.2" + "@typescript-eslint/visitor-keys": "npm:8.12.2" + checksum: a2cd6ad4b31f4d0ca6f94c4df8a94bdee762abd556686817ab4143d80a27506f43fbf96769b44e698d573784a464bfd78e0cbc17ac61c36a868e02311c754ce1 + languageName: node + linkType: hard + "@typescript-eslint/type-utils@npm:7.16.1": version: 7.16.1 resolution: "@typescript-eslint/type-utils@npm:7.16.1" @@ -1457,6 +1508,21 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/type-utils@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/type-utils@npm:8.12.2" + dependencies: + "@typescript-eslint/typescript-estree": "npm:8.12.2" + "@typescript-eslint/utils": "npm:8.12.2" + debug: "npm:^4.3.4" + ts-api-utils: "npm:^1.3.0" + peerDependenciesMeta: + typescript: + optional: true + checksum: 3563ff938a3a9afa341ef2e38974147a1655c46328f0dcd1c46c3999428efd7f4b9c4b57018122a898be7b23c532b88b0b2baa14d9f6338da8efc8488bb24f96 + languageName: node + linkType: hard + "@typescript-eslint/types@npm:7.16.1": version: 7.16.1 resolution: "@typescript-eslint/types@npm:7.16.1" @@ -1471,6 +1537,13 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/types@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/types@npm:8.12.2" + checksum: 57981e5fa45b03a0398ffb82418fdb716f476aa0b9c17d96edeb7fd3e3f4a720466868af7c2a02ddca65c27e70bfaff50c523b2a570582c4645a2702e17dc94a + languageName: node + linkType: hard + "@typescript-eslint/typescript-estree@npm:7.16.1": version: 7.16.1 resolution: "@typescript-eslint/typescript-estree@npm:7.16.1" @@ -1509,6 +1582,25 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/typescript-estree@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/typescript-estree@npm:8.12.2" + dependencies: + "@typescript-eslint/types": "npm:8.12.2" + "@typescript-eslint/visitor-keys": "npm:8.12.2" + debug: "npm:^4.3.4" + fast-glob: "npm:^3.3.2" + is-glob: "npm:^4.0.3" + minimatch: "npm:^9.0.4" + semver: "npm:^7.6.0" + ts-api-utils: "npm:^1.3.0" + peerDependenciesMeta: + typescript: + optional: true + checksum: 9995929ec4b66afa53d52c16f5cecd7c9aa45994f943c41e9ec91fe178593e83d9049ff056fe2638c3cf7da01476861eff0dc3cb76c314cc130458d3f828930d + languageName: node + linkType: hard + "@typescript-eslint/utils@npm:7.16.1": version: 7.16.1 resolution: "@typescript-eslint/utils@npm:7.16.1" @@ -1523,6 +1615,20 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/utils@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/utils@npm:8.12.2" + dependencies: + "@eslint-community/eslint-utils": "npm:^4.4.0" + "@typescript-eslint/scope-manager": "npm:8.12.2" + "@typescript-eslint/types": "npm:8.12.2" + "@typescript-eslint/typescript-estree": "npm:8.12.2" + peerDependencies: + eslint: ^8.57.0 || ^9.0.0 + checksum: 4588866ca43314692a0e685d8936c470dca4e6d119a4a1adefbc2fd54682ff081bc21d60bf4e8077d3668aa680bada851b88566264d09c92a840fe2e4feb331b + languageName: node + linkType: hard + "@typescript-eslint/visitor-keys@npm:7.16.1": version: 7.16.1 resolution: "@typescript-eslint/visitor-keys@npm:7.16.1" @@ -1543,6 +1649,16 @@ __metadata: languageName: node linkType: hard +"@typescript-eslint/visitor-keys@npm:8.12.2": + version: 8.12.2 + resolution: "@typescript-eslint/visitor-keys@npm:8.12.2" + dependencies: + "@typescript-eslint/types": "npm:8.12.2" + eslint-visitor-keys: "npm:^3.4.3" + checksum: 42795ad1c71520a367e2b53c3511b6cf922dcee05d61f6b0ec56b71c0b89a58889e0c3282b1bb13befc69df07204d0e4e053436d0c2b808460ce310b58a2a92e + languageName: node + linkType: hard + "@ungap/structured-clone@npm:^1.0.0": version: 1.2.0 resolution: "@ungap/structured-clone@npm:1.2.0" @@ -3001,6 +3117,13 @@ __metadata: languageName: node linkType: hard +"globals@npm:^15.11.0": + version: 15.11.0 + resolution: "globals@npm:15.11.0" + checksum: 14009ef1906ac929d930ed1c896a47159e7d11b4d201901ca5f3827766519191a3f5fb45124de43c4511fee04018704e7ed5a097fb37d23abf39523d1d41c85f + languageName: node + linkType: hard + "globby@npm:^11.1.0": version: 11.1.0 resolution: "globby@npm:11.1.0" @@ -4598,6 +4721,8 @@ __metadata: dependencies: "@commitlint/cli": "npm:^19.3.0" "@commitlint/config-conventional": "npm:^19.2.2" + "@eslint/eslintrc": "npm:^3.1.0" + "@eslint/js": "npm:^9.13.0" "@jest/globals": "npm:^29.7.0" "@types/jest": "npm:^29.5.14" "@types/node": "npm:^22.8.5" @@ -4605,6 +4730,7 @@ __metadata: "@typescript-eslint/parser": "npm:^7.8.0" eslint: "npm:^9.1.1" formdata-node: "npm:^6.0.3" + globals: "npm:^15.11.0" jest: "npm:^29.7.0" openapi-fetch: "npm:^0.13.0" openapi-typescript: "npm:^6.7.4" @@ -4613,6 +4739,7 @@ __metadata: ts-node: "npm:^10.9.2" typedoc: "npm:^0.26.10" typescript: "npm:^5.6.3" + typescript-eslint: "npm:^8.12.2" languageName: unknown linkType: soft @@ -5507,6 +5634,20 @@ __metadata: languageName: node linkType: hard +"typescript-eslint@npm:^8.12.2": + version: 8.12.2 + resolution: "typescript-eslint@npm:8.12.2" + dependencies: + "@typescript-eslint/eslint-plugin": "npm:8.12.2" + "@typescript-eslint/parser": "npm:8.12.2" + "@typescript-eslint/utils": "npm:8.12.2" + peerDependenciesMeta: + typescript: + optional: true + checksum: 2f479c5d2369aa6e40b1957ae75178c584e62e90d2f0e7434274ec23bb8fee69c6c8832eea39628b60a1511f1db80802aef6687636e80126b01af4748f3be816 + languageName: node + linkType: hard + "typescript@npm:^5.6.3": version: 5.6.3 resolution: "typescript@npm:5.6.3"