From ef18825f9ee581b70959726822c6a90788cf62ae Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:16:21 -0800 Subject: [PATCH 1/3] Prohibit non-null assertions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From rule doc page¹: It's generally better to structure program logic so that TypeScript understands when values may be nullable. Also added a comment to eventually move to use plugin:@typescript-eslint/strict which includes this rule. ¹ --- .eslintrc.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 51926d896..66510929b 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -24,6 +24,9 @@ rules: eqeqeq: error "@typescript-eslint/no-empty-function": ["error", { "allow": ["arrowFunctions"] }] "@typescript-eslint/no-explicit-any": off # Allow explicit any to make incremental TypeScript adoption easier. + # TODO: use strict ruleset which includes no-non-null-assertion. + # Not possible while strictNullChecks is disabled in tsconfig. + "@typescript-eslint/no-non-null-assertion": "error" no-unused-vars: off "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_", "destructuredArrayIgnorePattern": "^_" }] no-use-before-define: off From 4592e2a983ef039fba742c4c5b8c807d9446df71 Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:46:46 -0800 Subject: [PATCH 2/3] Prohibit type assertions This should not be used unless there is no other way for the TypeScript compiler to infer the type properly. In such cases, a comment with per-line exception should be used. I will address existing violations in subsequent commits. --- .eslintrc.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 66510929b..93a1c56b3 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -22,6 +22,7 @@ globals: rules: # Code quality rules eqeqeq: error + "@typescript-eslint/consistent-type-assertions": ["error", { assertionStyle: 'never' }] "@typescript-eslint/no-empty-function": ["error", { "allow": ["arrowFunctions"] }] "@typescript-eslint/no-explicit-any": off # Allow explicit any to make incremental TypeScript adoption easier. # TODO: use strict ruleset which includes no-non-null-assertion. From 6215591c49fc0c7b63065276bdc530b8bba5dd6a Mon Sep 17 00:00:00 2001 From: Victor Lin <13424970+victorlin@users.noreply.github.com> Date: Wed, 20 Nov 2024 14:47:30 -0800 Subject: [PATCH 3/3] Remove unnecessary type assertions The types can already be inferred with existing usage. --- src/util/entropyCreateStateFromJsons.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/entropyCreateStateFromJsons.ts b/src/util/entropyCreateStateFromJsons.ts index bd042373e..0745c532a 100644 --- a/src/util/entropyCreateStateFromJsons.ts +++ b/src/util/entropyCreateStateFromJsons.ts @@ -103,7 +103,7 @@ export const genomeMap = (annotations: JsonAnnotations): GenomeAnnotation => { .map(([annotationKey, annotation]) => { const geneName = annotation.gene || annotationKey; if (!(geneName in annotationsPerGene)) annotationsPerGene[geneName] = {}; - const gene = annotationsPerGene[geneName] as JsonAnnotations; // TODO - why do I need to cast? + const gene = annotationsPerGene[geneName]; gene[annotationKey] = annotation; }) @@ -167,7 +167,7 @@ function validColor(color:(string|undefined)) { function* nextColorGenerator() { let i=0; while (true) { - yield genotypeColors[i++] as string; + yield genotypeColors[i++]; if (i===genotypeColors.length) i=0; } }