From c550beccb45dd1259abe0724be503b817aad1bbf Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Tue, 10 Oct 2023 23:17:35 +0200 Subject: [PATCH] infra(typescript-eslint): strict-type-checked --- .eslintrc.js | 11 +++++++---- src/modules/finance/index.ts | 2 +- src/modules/helpers/index.ts | 2 +- src/modules/helpers/unique.ts | 2 +- src/modules/image/index.ts | 7 +++---- src/modules/word/filterWordListByLength.ts | 4 ++-- test/all_functional.spec.ts | 6 +++--- test/modules/helpers.spec.ts | 2 ++ test/scripts/apidoc/module.example.ts | 2 ++ test/scripts/apidoc/verify-jsdoc-tags.spec.ts | 8 ++++---- 10 files changed, 26 insertions(+), 20 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index e137be56299..72977fd30e0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,8 +15,7 @@ module.exports = defineConfig({ reportUnusedDisableDirectives: true, extends: [ 'eslint:recommended', - 'plugin:@typescript-eslint/recommended', - 'plugin:@typescript-eslint/recommended-type-checked', + 'plugin:@typescript-eslint/strict-type-checked', 'plugin:prettier/recommended', 'plugin:jsdoc/recommended-typescript-error', 'plugin:unicorn/recommended', @@ -109,11 +108,10 @@ module.exports = defineConfig({ 'error', { ignoreParameters: true }, ], - '@typescript-eslint/no-unsafe-argument': 'error', + '@typescript-eslint/no-unnecessary-condition': 'off', // requires `strictNullChecks` to be enabled '@typescript-eslint/no-unsafe-assignment': 'off', '@typescript-eslint/no-unsafe-call': 'off', '@typescript-eslint/no-unsafe-member-access': 'off', - '@typescript-eslint/no-unsafe-return': 'error', '@typescript-eslint/padding-line-between-statements': [ 'error', { blankLine: 'always', prev: 'block-like', next: '*' }, @@ -123,6 +121,11 @@ module.exports = defineConfig({ { allowNumber: true, allowBoolean: true }, ], '@typescript-eslint/unbound-method': 'off', + '@typescript-eslint/unified-signatures': 'off', // incompatible with our api docs generation + + // TODO @ST-DDT 2023-10-10: The following rules currently conflict with our code. + // Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently. + '@typescript-eslint/no-confusing-void-expression': 'off', 'jsdoc/no-types': 'error', 'jsdoc/require-jsdoc': 'off', diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 43cd16a1c5e..399ad8e590c 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -826,7 +826,7 @@ export class FinanceModule { const normalizedIssuer = issuer.toLowerCase(); if (normalizedIssuer in localeFormat) { format = this.faker.helpers.arrayElement(localeFormat[normalizedIssuer]); - } else if (/#/.test(issuer)) { + } else if (issuer.includes('#')) { // The user chose an optional scheme format = issuer; } else { diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 59a4041a065..7548c15b578 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -460,7 +460,7 @@ export class SimpleHelpersModule { } while (range != null) { - if (range[0].indexOf('-') === -1) { + if (!range[0].includes('-')) { // handle non-ranges if (isCaseInsensitive && isNaN(Number(range[0]))) { rangeCodes.push( diff --git a/src/modules/helpers/unique.ts b/src/modules/helpers/unique.ts index 860404455f5..fd1aec33a2e 100644 --- a/src/modules/helpers/unique.ts +++ b/src/modules/helpers/unique.ts @@ -135,7 +135,7 @@ export function exec< const result: ReturnType = method(...args) as ReturnType; // If the result has not been previously found, add it to the found array and return the value as it's unique. - if (compare(store, result) === -1 && exclude.indexOf(result) === -1) { + if (compare(store, result) === -1 && !exclude.includes(result)) { store[result] = result; options.currentIterations = 0; return result; diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index a93f181e131..b1c9c6922e3 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -232,17 +232,16 @@ export class ImageModule { length: { min: 5, max: 10 }, })}/${width}/${height}`; - const hasValidGrayscale = grayscale === true; const hasValidBlur = typeof blur === 'number' && blur >= 1 && blur <= 10; - if (hasValidGrayscale || hasValidBlur) { + if (grayscale || hasValidBlur) { url += '?'; - if (hasValidGrayscale) { + if (grayscale) { url += `grayscale`; } - if (hasValidGrayscale && hasValidBlur) { + if (grayscale && hasValidBlur) { url += '&'; } diff --git a/src/modules/word/filterWordListByLength.ts b/src/modules/word/filterWordListByLength.ts index 106e98589bc..60c6fae93c9 100644 --- a/src/modules/word/filterWordListByLength.ts +++ b/src/modules/word/filterWordListByLength.ts @@ -13,12 +13,12 @@ const STRATEGIES = { wordList: ReadonlyArray, length: { min: number; max: number } ): string[] => { - const wordsByLength = wordList.reduce( + const wordsByLength = wordList.reduce>( (data, word) => { (data[word.length] = data[word.length] ?? []).push(word); return data; }, - {} as Record + {} ); const lengths = Object.keys(wordsByLength).map(Number); diff --git a/test/all_functional.spec.ts b/test/all_functional.spec.ts index 0437a239b8b..4ca98856164 100644 --- a/test/all_functional.spec.ts +++ b/test/all_functional.spec.ts @@ -2,16 +2,16 @@ import { describe, expect, it } from 'vitest'; import type { allLocales, Faker, RandomModule } from '../src'; import { allFakers, fakerEN } from '../src'; -const IGNORED_MODULES = [ +const IGNORED_MODULES = new Set([ 'rawDefinitions', 'definitions', 'helpers', '_randomizer', '_defaultRefDate', -]; +]); function isTestableModule(mod: string) { - return IGNORED_MODULES.indexOf(mod) === -1; + return !IGNORED_MODULES.has(mod); } function isMethodOf(mod: string) { diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index 471313766ea..f92efe7d3be 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -95,6 +95,7 @@ describe('helpers', () => { enum MixedFoo { Foo = 0, Bar = 1, + // eslint-disable-next-line @typescript-eslint/no-mixed-enums FooName = 'Foo', BarName = 'Bar', } @@ -256,6 +257,7 @@ describe('helpers', () => { enum FooMixedEnum { Foo = 0, Bar = 1, + // eslint-disable-next-line @typescript-eslint/no-mixed-enums StrFoo = 'FOO', StrBar = 'BAR', } diff --git a/test/scripts/apidoc/module.example.ts b/test/scripts/apidoc/module.example.ts index b3f43a8602b..0e5d9d89613 100644 --- a/test/scripts/apidoc/module.example.ts +++ b/test/scripts/apidoc/module.example.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-extraneous-class -- required for tests */ + /** * A simple module without anything special. */ diff --git a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts index 5b8f5680b42..9a603d55e99 100644 --- a/test/scripts/apidoc/verify-jsdoc-tags.spec.ts +++ b/test/scripts/apidoc/verify-jsdoc-tags.spec.ts @@ -47,7 +47,7 @@ describe('verify JSDoc tags', () => { } const allowedReferences = new Set( - Object.values(modules).reduce((acc, [module, methods]) => { + Object.values(modules).reduce((acc, [module, methods]) => { const moduleFieldName = extractModuleFieldName(module); return [ ...acc, @@ -55,10 +55,10 @@ describe('verify JSDoc tags', () => { (methodName) => `faker.${moduleFieldName}.${methodName}` ), ]; - }, [] as string[]) + }, []) ); const allowedLinks = new Set( - Object.values(modules).reduce((acc, [module, methods]) => { + Object.values(modules).reduce((acc, [module, methods]) => { const moduleFieldName = extractModuleFieldName(module); return [ ...acc, @@ -68,7 +68,7 @@ describe('verify JSDoc tags', () => { `/api/${moduleFieldName}.html#${methodName.toLowerCase()}` ), ]; - }, [] as string[]) + }, []) ); function assertDescription(description: string, isHtml: boolean): void {