From c2a84b65925d2150a090e3af96c9def178f004e3 Mon Sep 17 00:00:00 2001 From: Joachim Van Herwegen Date: Mon, 30 Oct 2023 17:09:04 +0100 Subject: [PATCH] refactor: Prevent default parameters for objects --- eslint.config.js | 4 +--- src/server/BaseServerFactory.ts | 4 ++-- src/util/locking/RedisLocker.ts | 7 ++++--- src/util/templates/TemplateUtil.ts | 6 +++--- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index a0691be66..40354a538 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -238,6 +238,7 @@ const configs = antfu( 'unicorn/no-lonely-if': 'error', 'unicorn/no-negated-condition': 'error', 'unicorn/no-nested-ternary': 'error', + 'unicorn/no-object-as-default-parameter': 'error', 'unicorn/no-process-exit': 'error', 'unicorn/no-thenable': 'error', 'unicorn/no-useless-fallback-in-spread': 'error', @@ -269,9 +270,6 @@ const configs = antfu( 'unicorn/require-array-join-separator': 'error', 'unicorn/require-number-to-fixed-digits-argument': 'error', - // Might want to enable these - 'unicorn/no-object-as-default-parameter': 'off', - 'unused-imports/no-unused-vars': [ 'error', { args: 'after-used', vars: 'all', ignoreRestSiblings: true }, diff --git a/src/server/BaseServerFactory.ts b/src/server/BaseServerFactory.ts index da8c9c776..421522fb6 100644 --- a/src/server/BaseServerFactory.ts +++ b/src/server/BaseServerFactory.ts @@ -38,9 +38,9 @@ export class BaseServerFactory implements HttpServerFactory { private readonly configurator: ServerConfigurator; private readonly options: BaseServerFactoryOptions; - public constructor(configurator: ServerConfigurator, options: BaseServerFactoryOptions = { https: false }) { + public constructor(configurator: ServerConfigurator, options?: BaseServerFactoryOptions) { this.configurator = configurator; - this.options = { ...options }; + this.options = { https: false, ...options }; } /** diff --git a/src/util/locking/RedisLocker.ts b/src/util/locking/RedisLocker.ts index e650e1856..78176318e 100644 --- a/src/util/locking/RedisLocker.ts +++ b/src/util/locking/RedisLocker.ts @@ -19,7 +19,7 @@ const PREFIX_LOCK = '__L__'; export interface RedisSettings { /* Override default namespacePrefixes (used to prefix keys in Redis) */ - namespacePrefix: string; + namespacePrefix?: string; /* Username used for AUTH on the Redis server */ username?: string; /* Password used for AUTH on the Redis server */ @@ -74,12 +74,13 @@ export class RedisLocker implements ReadWriteLocker, ResourceLocker, Initializab public constructor( redisClient = '127.0.0.1:6379', attemptSettings: AttemptSettings = {}, - redisSettings: RedisSettings = { namespacePrefix: '' }, + redisSettings?: RedisSettings, ) { + redisSettings = { namespacePrefix: '', ...redisSettings }; const { namespacePrefix, ...options } = redisSettings; this.redis = this.createRedisClient(redisClient, options); this.attemptSettings = { ...attemptDefaults, ...attemptSettings }; - this.namespacePrefix = namespacePrefix; + this.namespacePrefix = namespacePrefix!; // Register lua scripts for (const [ name, script ] of Object.entries(REDIS_LUA_SCRIPTS)) { diff --git a/src/util/templates/TemplateUtil.ts b/src/util/templates/TemplateUtil.ts index f68b2c5f1..45bcc3675 100644 --- a/src/util/templates/TemplateUtil.ts +++ b/src/util/templates/TemplateUtil.ts @@ -23,10 +23,10 @@ export function getTemplateFilePath(template?: Template): string | undefined { /** * Reads the template and returns it as a string. */ -export async function readTemplate(template: Template = { templateString: '' }): Promise { +export async function readTemplate(template?: Template): Promise { // The template has already been given as a string - if (typeof template === 'object' && 'templateString' in template) { - return template.templateString; + if (typeof template === 'undefined' || (typeof template === 'object' && 'templateString' in template)) { + return template?.templateString ?? ''; } // The template needs to be read from disk return fsPromises.readFile(getTemplateFilePath(template)!, 'utf8');