From 06f2b809ef7e29717662f9be006ec80cd3d07c91 Mon Sep 17 00:00:00 2001 From: RebeccaStevens Date: Sun, 20 Oct 2024 14:42:03 +0000 Subject: [PATCH] fix(functional-parameters): default options not being applied to option overrides (#888) --- docs/rules/functional-parameters.md | 23 +++++++++++++---- src/configs/recommended.ts | 1 + src/rules/functional-parameters.ts | 40 +++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/docs/rules/functional-parameters.md b/docs/rules/functional-parameters.md index f993f007e..2c055a4de 100644 --- a/docs/rules/functional-parameters.md +++ b/docs/rules/functional-parameters.md @@ -132,11 +132,24 @@ const defaults = { ```ts const recommendedOptions = { - enforceParameterCount: { - ignoreLambdaExpression: true, - ignoreIIFE: true, - ignoreGettersAndSetters: true, - }, + enforceParameterCount: false, + overrides: [ + { + specifiers: [ + { + from: "file", + }, + ], + options: { + enforceParameterCount: { + count: "atLeastOne", + ignoreLambdaExpression: true, + ignoreIIFE: true, + ignoreGettersAndSetters: true, + }, + }, + }, + ], }; ``` diff --git a/src/configs/recommended.ts b/src/configs/recommended.ts index 4bdad7761..bf4c078e3 100644 --- a/src/configs/recommended.ts +++ b/src/configs/recommended.ts @@ -41,6 +41,7 @@ const overrides = { ], options: { enforceParameterCount: { + count: "atLeastOne", ignoreLambdaExpression: true, ignoreIIFE: true, ignoreGettersAndSetters: true, diff --git a/src/rules/functional-parameters.ts b/src/rules/functional-parameters.ts index 4d65a75b0..073f5cb3e 100644 --- a/src/rules/functional-parameters.ts +++ b/src/rules/functional-parameters.ts @@ -127,7 +127,7 @@ const schema: JSONSchema4[] = [ /** * The default options for the rule. */ -const defaultOptions: RawOptions = [ +const defaultOptions = [ { allowRestParameter: false, allowArgumentsKeyword: false, @@ -138,7 +138,7 @@ const defaultOptions: RawOptions = [ ignoreGettersAndSetters: true, }, }, -]; +] satisfies RawOptions; /** * The possible error messages. @@ -234,6 +234,31 @@ function getParamCountViolations( return []; } +/** + * Add the default options to the given options. + */ +function getOptionsWithDefaults( + options: Readonly | null, +): Options | null { + if (options === null) { + return null; + } + + const topLevel = { + ...defaultOptions[0], + ...options, + }; + return typeof topLevel.enforceParameterCount === "object" + ? { + ...topLevel, + enforceParameterCount: { + ...defaultOptions[0].enforceParameterCount, + ...topLevel.enforceParameterCount, + }, + } + : topLevel; +} + /** * Check if the given function node has a reset parameter this rule. */ @@ -243,10 +268,8 @@ function checkFunction( rawOptions: Readonly, ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); - const optionsToUse = getCoreOptions( - node, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(node, context, options), ); if (optionsToUse === null) { @@ -291,10 +314,11 @@ function checkIdentifier( const functionNode = getEnclosingFunction(node); const options = upgradeRawOverridableOptions(rawOptions[0]); - const optionsToUse = + const optionsToUse = getOptionsWithDefaults( functionNode === null ? options - : getCoreOptions(functionNode, context, options); + : getCoreOptions(functionNode, context, options), + ); if (optionsToUse === null) { return {