Skip to content

Commit

Permalink
fix(functional-parameters): default options not being applied to opti…
Browse files Browse the repository at this point in the history
…on overrides (#888)
  • Loading branch information
RebeccaStevens committed Oct 20, 2024
1 parent e36178c commit 06f2b80
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
23 changes: 18 additions & 5 deletions docs/rules/functional-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
],
};
```

Expand Down
1 change: 1 addition & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const overrides = {
],
options: {
enforceParameterCount: {
count: "atLeastOne",
ignoreLambdaExpression: true,
ignoreIIFE: true,
ignoreGettersAndSetters: true,
Expand Down
40 changes: 32 additions & 8 deletions src/rules/functional-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const schema: JSONSchema4[] = [
/**
* The default options for the rule.
*/
const defaultOptions: RawOptions = [
const defaultOptions = [
{
allowRestParameter: false,
allowArgumentsKeyword: false,
Expand All @@ -138,7 +138,7 @@ const defaultOptions: RawOptions = [
ignoreGettersAndSetters: true,
},
},
];
] satisfies RawOptions;

/**
* The possible error messages.
Expand Down Expand Up @@ -234,6 +234,31 @@ function getParamCountViolations(
return [];
}

/**
* Add the default options to the given options.
*/
function getOptionsWithDefaults(
options: Readonly<Options> | 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.
*/
Expand All @@ -243,10 +268,8 @@ function checkFunction(
rawOptions: Readonly<RawOptions>,
): RuleResult<keyof typeof errorMessages, RawOptions> {
const options = upgradeRawOverridableOptions(rawOptions[0]);
const optionsToUse = getCoreOptions<CoreOptions, Options>(
node,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(node, context, options),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -291,10 +314,11 @@ function checkIdentifier(

const functionNode = getEnclosingFunction(node);
const options = upgradeRawOverridableOptions(rawOptions[0]);
const optionsToUse =
const optionsToUse = getOptionsWithDefaults(
functionNode === null
? options
: getCoreOptions<CoreOptions, Options>(functionNode, context, options);
: getCoreOptions<CoreOptions, Options>(functionNode, context, options),
);

if (optionsToUse === null) {
return {
Expand Down

0 comments on commit 06f2b80

Please sign in to comment.