From 4b05ac144c34ca10647dc8598e13e300f35115e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Mon, 4 Apr 2022 10:57:13 +0100 Subject: [PATCH] refactor: Enable `strict-boolean-expressions` Fixes an inefficiency in `encodeHTMLTrieRe`, and prevents similar issues from happening again. Also re-enables several previously disabled rules. --- .eslintrc.json | 9 +++++---- src/decode_codepoint.ts | 2 +- src/encode-trie.ts | 4 ++-- src/encode.ts | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 3293f472..d0b9fba9 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -44,11 +44,12 @@ } }, "rules": { - "@typescript-eslint/prefer-for-of": 0, - "@typescript-eslint/member-ordering": 0, - "@typescript-eslint/explicit-function-return-type": 0, - "@typescript-eslint/no-unused-vars": 0, "@typescript-eslint/no-non-null-assertion": 0, + "@typescript-eslint/no-unused-vars": [ + 2, + { "argsIgnorePattern": "^_" } + ], + "@typescript-eslint/strict-boolean-expressions": 2, "@typescript-eslint/no-use-before-define": [ 2, { "functions": false } diff --git a/src/decode_codepoint.ts b/src/decode_codepoint.ts index e3ff63b5..ffa6cd78 100644 --- a/src/decode_codepoint.ts +++ b/src/decode_codepoint.ts @@ -33,7 +33,7 @@ const decodeMap = new Map([ const fromCodePoint = // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, node/no-unsupported-features/es-builtins - String.fromCodePoint || + String.fromCodePoint ?? function (codePoint: number): string { let output = ""; diff --git a/src/encode-trie.ts b/src/encode-trie.ts index edcae777..5001dbb1 100644 --- a/src/encode-trie.ts +++ b/src/encode-trie.ts @@ -41,10 +41,10 @@ export function encodeHTMLTrieRe(regExp: RegExp, str: string): string { typeof next.n === "number" ? next.n === str.charCodeAt(i + 1) ? next.o - : null + : undefined : next.n.get(str.charCodeAt(i + 1)); - if (value) { + if (value !== undefined) { ret += str.substring(lastIdx, i) + value; lastIdx = regExp.lastIndex += 1; continue; diff --git a/src/encode.ts b/src/encode.ts index c872a712..27060def 100644 --- a/src/encode.ts +++ b/src/encode.ts @@ -28,7 +28,7 @@ export function encodeXML(str: string): string { const char = str.charCodeAt(i); const next = xmlCodeMap.get(char); - if (next) { + if (next !== undefined) { ret += str.substring(lastIdx, i) + next; lastIdx = i + 1; } else {