From 6f9ddb02b8d9b8028522d16234b40a78c9a59c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Belin?= Date: Mon, 29 Jan 2024 16:37:34 +0100 Subject: [PATCH] Update the settings of the static analyzer --- etc/eslint.cjs | 13 +++++++++---- example/check_comment.js | 3 ++- example/submit_ham.js | 3 ++- example/submit_spam.js | 3 ++- example/verify_key.js | 3 ++- package-lock.json | 8 ++++---- package.json | 4 ++-- src/client.ts | 2 +- src/comment.ts | 2 +- src/usage.ts | 4 ++-- 10 files changed, 27 insertions(+), 18 deletions(-) diff --git a/etc/eslint.cjs b/etc/eslint.cjs index d4aca928..f81ea9f8 100644 --- a/etc/eslint.cjs +++ b/etc/eslint.cjs @@ -6,15 +6,19 @@ module.exports = { }, extends: [ "eslint:recommended", - "plugin:@typescript-eslint/recommended" + "plugin:@typescript-eslint/recommended-type-checked" ], overrides: [ { - files: ["*.cjs", "*.js", "*.mjs"], + files: ["*.{cjs,js,mjs}"], rules: { "@typescript-eslint/explicit-function-return-type": "off", "@typescript-eslint/explicit-module-boundary-types": "off" } + }, + { + files: ["test/**/*.js"], + rules: {"@typescript-eslint/no-floating-promises": "off"} } ], parser: "@typescript-eslint/parser", @@ -135,7 +139,7 @@ module.exports = { "no-useless-rename": "error", "no-useless-return": "error", "no-var": "error", - "no-void": "error", + "no-void": ["error", {allowAsStatement: true}], "no-warning-comments": "warn", "object-shorthand": "error", "one-var": ["error", "never"], @@ -190,7 +194,7 @@ module.exports = { "@typescript-eslint/naming-convention": "off", "@typescript-eslint/no-array-delete": "error", "@typescript-eslint/no-confusing-non-null-assertion": "error", - "@typescript-eslint/no-confusing-void-expression": ["error", {ignoreArrowShorthand: true}], + "@typescript-eslint/no-confusing-void-expression": "off", "@typescript-eslint/no-dupe-class-members": "error", "@typescript-eslint/no-dynamic-delete": "error", "@typescript-eslint/no-empty-function": "error", @@ -243,6 +247,7 @@ module.exports = { "@typescript-eslint/prefer-ts-expect-error": "error", "@typescript-eslint/promise-function-async": "off", "@typescript-eslint/require-array-sort-compare": "error", + "@typescript-eslint/restrict-template-expressions": "off", "@typescript-eslint/return-await": "error", "@typescript-eslint/sort-type-constituents": "error", "@typescript-eslint/strict-boolean-expressions": "off", diff --git a/example/check_comment.js b/example/check_comment.js index 59e7544d..b2310ce3 100644 --- a/example/check_comment.js +++ b/example/check_comment.js @@ -29,5 +29,6 @@ try { console.log(result == CheckResult.ham ? "The comment is ham." : "The comment is spam."); } catch (error) { - console.log(`An error occurred: ${error}`); + const message = error instanceof Error ? error.message : String(error); + console.log(`An error occurred: ${message}`); } diff --git a/example/submit_ham.js b/example/submit_ham.js index ae66ea93..3062cbcb 100644 --- a/example/submit_ham.js +++ b/example/submit_ham.js @@ -18,5 +18,6 @@ try { console.log("The comment was successfully submitted as ham."); } catch (error) { - console.log(`An error occurred: ${error}`); + const message = error instanceof Error ? error.message : String(error); + console.log(`An error occurred: ${message}`); } diff --git a/example/submit_spam.js b/example/submit_spam.js index cf103f63..5e6abb61 100644 --- a/example/submit_spam.js +++ b/example/submit_spam.js @@ -18,5 +18,6 @@ try { console.log("The comment was successfully submitted as spam."); } catch (error) { - console.log(`An error occurred: ${error}`); + const message = error instanceof Error ? error.message : String(error); + console.log(`An error occurred: ${message}`); } diff --git a/example/verify_key.js b/example/verify_key.js index 581ce227..64825c01 100644 --- a/example/verify_key.js +++ b/example/verify_key.js @@ -10,5 +10,6 @@ try { console.log(isValid ? "The API key is valid." : "The API key is invalid."); } catch (error) { - console.log(`An error occurred: ${error}`); + const message = error instanceof Error ? error.message : String(error); + console.log(`An error occurred: ${message}`); } diff --git a/package-lock.json b/package-lock.json index 7b37ed86..ecbc2913 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "16.1.0", "license": "MIT", "devDependencies": { - "@types/node": "^20.11.9", + "@types/node": "^20.11.10", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", "del": "^7.1.0", @@ -206,9 +206,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.11.9", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.9.tgz", - "integrity": "sha512-CQXNuMoS/VcoAMISe5pm4JnEd1Br5jildbQEToEMQvutmv+EaQr90ry9raiudgpyDuqFiV9e4rnjSfLNq12M5w==", + "version": "20.11.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.10.tgz", + "integrity": "sha512-rZEfe/hJSGYmdfX9tvcPMYeYPW2sNl50nsw4jZmRcaG0HIAb0WYEpsB05GOb53vjqpyE9GUhlDQ4jLSoB5q9kg==", "dev": true, "dependencies": { "undici-types": "~5.26.4" diff --git a/package.json b/package.json index b34826dc..a6054aaa 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "url": "https://belin.io" }, "devDependencies": { - "@types/node": "^20.11.9", + "@types/node": "^20.11.10", "@typescript-eslint/eslint-plugin": "^6.19.1", "@typescript-eslint/parser": "^6.19.1", "del": "^7.1.0", @@ -50,8 +50,8 @@ "scripts": { "build": "node tool/build.js && tsc --project src/tsconfig.json", "clean": "node tool/clean.js", - "doc": "typedoc --options etc/typedoc.js && node tool/doc.js", "dist": "npm run clean && npm run build", + "doc": "typedoc --options etc/typedoc.js && node tool/doc.js", "lint": "tsc --project tsconfig.json && eslint --config=etc/eslint.cjs example src test tool", "postpublish": "node tool/publish.js", "prepack": "npm run dist", diff --git a/src/client.ts b/src/client.ts index e0c726bd..3f6eee72 100644 --- a/src/client.ts +++ b/src/client.ts @@ -105,7 +105,7 @@ export class Client { * @param fields The fields describing the query body. * @returns The server response. */ - async #fetch(endpoint: string, fields: Record): Promise { + async #fetch(endpoint: string, fields: Record): Promise { const body = new URLSearchParams(this.blog.toJSON()); body.set("api_key", this.apiKey); if (this.isTest) body.set("is_test", "1"); diff --git a/src/comment.ts b/src/comment.ts index d363e324..3d8d73ef 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -90,7 +90,7 @@ export class Comment { * Converts this object to a map in JSON format. * @returns The map in JSON format corresponding to this object. */ - toJSON(): Record { + toJSON(): Record { const map = this.author ? this.author.toJSON() : {}; if (this.content) map.comment_content = this.content; if (this.context.length) map.comment_context = this.context; diff --git a/src/usage.ts b/src/usage.ts index b3c1b236..abc33bcd 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -41,10 +41,10 @@ export class Usage { */ static fromJson(json: Record): Usage { return new this({ - limit: Number.isInteger(json.limit) ? json.limit : -1, + limit: typeof json.limit == "number" && Number.isInteger(json.limit) ? json.limit : -1, percentage: typeof json.percentage == "number" ? json.percentage : 0, throttled: typeof json.throttled == "boolean" ? json.throttled : false, - usage: Number.isInteger(json.usage) ? json.usage : 0 + usage: typeof json.usage == "number" && Number.isInteger(json.usage) ? json.usage : 0 }); } }