Skip to content

Commit c04af80

Browse files
fix: ensure default options are applied
1 parent 4da1024 commit c04af80

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/rules/immutable-data.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ const schema: JSONSchema4[] = [
110110
/**
111111
* The default options for the rule.
112112
*/
113-
const defaultOptions: RawOptions = [
113+
const defaultOptions = [
114114
{
115115
ignoreClasses: false,
116116
ignoreImmediateMutation: true,
117117
ignoreNonConstDeclarations: false,
118118
},
119-
];
119+
] satisfies RawOptions;
120120

121121
/**
122122
* The possible error messages.
@@ -227,10 +227,8 @@ function checkAssignmentExpression(
227227
): RuleResult<keyof typeof errorMessages, RawOptions> {
228228
const options = upgradeRawOverridableOptions(rawOptions[0]);
229229
const rootNode = findRootIdentifier(node.left) ?? node.left;
230-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
231-
rootNode,
232-
context,
233-
options,
230+
const optionsToUse = getOptionsWithDefaults(
231+
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
234232
);
235233

236234
if (optionsToUse === null) {
@@ -306,10 +304,8 @@ function checkUnaryExpression(
306304
): RuleResult<keyof typeof errorMessages, RawOptions> {
307305
const options = upgradeRawOverridableOptions(rawOptions[0]);
308306
const rootNode = findRootIdentifier(node.argument) ?? node.argument;
309-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
310-
rootNode,
311-
context,
312-
options,
307+
const optionsToUse = getOptionsWithDefaults(
308+
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
313309
);
314310

315311
if (optionsToUse === null) {
@@ -384,10 +380,8 @@ function checkUpdateExpression(
384380
): RuleResult<keyof typeof errorMessages, RawOptions> {
385381
const options = upgradeRawOverridableOptions(rawOptions[0]);
386382
const rootNode = findRootIdentifier(node.argument) ?? node.argument;
387-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
388-
rootNode,
389-
context,
390-
options,
383+
const optionsToUse = getOptionsWithDefaults(
384+
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
391385
);
392386

393387
if (optionsToUse === null) {
@@ -533,6 +527,20 @@ function isInChainCallAndFollowsNew(
533527
return false;
534528
}
535529

530+
/**
531+
* Add the default options to the given options.
532+
*/
533+
function getOptionsWithDefaults(options: Readonly<Options> | null): Options {
534+
if (options === null) {
535+
return defaultOptions[0];
536+
}
537+
538+
return {
539+
...defaultOptions[0],
540+
...options,
541+
};
542+
}
543+
536544
/**
537545
* Check if the given node violates this rule.
538546
*/
@@ -543,10 +551,8 @@ function checkCallExpression(
543551
): RuleResult<keyof typeof errorMessages, RawOptions> {
544552
const options = upgradeRawOverridableOptions(rawOptions[0]);
545553
const rootNode = findRootIdentifier(node.callee) ?? node.callee;
546-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
547-
rootNode,
548-
context,
549-
options,
554+
const optionsToUse = getOptionsWithDefaults(
555+
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
550556
);
551557

552558
if (optionsToUse === null) {

src/rules/prefer-immutable-types.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ const schema: JSONSchema4[] = [
278278
/**
279279
* The default options for the rule.
280280
*/
281-
const defaultOptions: RawOptions = [
281+
const defaultOptions = [
282282
{
283283
enforcement: Immutability.Immutable,
284284
ignoreInferredTypes: false,
@@ -308,7 +308,7 @@ const defaultOptions: RawOptions = [
308308
],
309309
},
310310
},
311-
];
311+
] satisfies RawOptions;
312312

313313
/**
314314
* The possible error messages.
@@ -496,10 +496,8 @@ function getParameterTypeViolations(
496496
const parameterProperty = isTSParameterProperty(param);
497497
const actualParam = parameterProperty ? param.parameter : param;
498498

499-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
500-
param,
501-
context,
502-
options,
499+
const optionsToUse = getOptionsWithDefaults(
500+
getCoreOptions<CoreOptions, Options>(param, context, options),
503501
);
504502

505503
if (optionsToUse === null) {
@@ -637,11 +635,13 @@ function getReturnTypeViolations(
637635
options: Readonly<Options>,
638636
): Descriptor[] {
639637
function getOptions(type: Type, typeNode: TypeNode | null) {
640-
const optionsToUse = getCoreOptionsForType<CoreOptions, Options>(
641-
type,
642-
typeNode,
643-
context,
644-
options,
638+
const optionsToUse = getOptionsWithDefaults(
639+
getCoreOptionsForType<CoreOptions, Options>(
640+
type,
641+
typeNode,
642+
context,
643+
options,
644+
),
645645
);
646646

647647
if (optionsToUse === null) {
@@ -824,6 +824,20 @@ function getReturnTypeViolations(
824824
];
825825
}
826826

827+
/**
828+
* Add the default options to the given options.
829+
*/
830+
function getOptionsWithDefaults(options: Readonly<Options> | null): Options {
831+
if (options === null) {
832+
return defaultOptions[0];
833+
}
834+
835+
return {
836+
...defaultOptions[0],
837+
...options,
838+
};
839+
}
840+
827841
/**
828842
* Check if the given function node violates this rule.
829843
*/
@@ -854,10 +868,8 @@ function checkVariable(
854868
rawOptions: Readonly<RawOptions>,
855869
): RuleResult<keyof typeof errorMessages, RawOptions> {
856870
const options = upgradeRawOverridableOptions(rawOptions[0]);
857-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
858-
node,
859-
context,
860-
options,
871+
const optionsToUse = getOptionsWithDefaults(
872+
getCoreOptions<CoreOptions, Options>(node, context, options),
861873
);
862874

863875
if (optionsToUse === null) {

0 commit comments

Comments
 (0)