Skip to content

Commit b6d374a

Browse files
fix: ensure default options are applied
1 parent 687ebf7 commit b6d374a

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

src/rules/immutable-data.ts

Lines changed: 26 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,22 @@ function isInChainCallAndFollowsNew(
533527
return false;
534528
}
535529

530+
/**
531+
* Add the default options to the given options.
532+
*/
533+
function getOptionsWithDefaults(
534+
options: Readonly<Options> | null,
535+
): Options | null {
536+
if (options === null) {
537+
return null;
538+
}
539+
540+
return {
541+
...defaultOptions[0],
542+
...options,
543+
};
544+
}
545+
536546
/**
537547
* Check if the given node violates this rule.
538548
*/
@@ -543,10 +553,8 @@ function checkCallExpression(
543553
): RuleResult<keyof typeof errorMessages, RawOptions> {
544554
const options = upgradeRawOverridableOptions(rawOptions[0]);
545555
const rootNode = findRootIdentifier(node.callee) ?? node.callee;
546-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
547-
rootNode,
548-
context,
549-
options,
556+
const optionsToUse = getOptionsWithDefaults(
557+
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
550558
);
551559

552560
if (optionsToUse === null) {

src/rules/prefer-immutable-types.ts

Lines changed: 29 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,22 @@ function getReturnTypeViolations(
824824
];
825825
}
826826

827+
/**
828+
* Add the default options to the given options.
829+
*/
830+
function getOptionsWithDefaults(
831+
options: Readonly<Options> | null,
832+
): Options | null {
833+
if (options === null) {
834+
return null;
835+
}
836+
837+
return {
838+
...defaultOptions[0],
839+
...options,
840+
};
841+
}
842+
827843
/**
828844
* Check if the given function node violates this rule.
829845
*/
@@ -854,10 +870,8 @@ function checkVariable(
854870
rawOptions: Readonly<RawOptions>,
855871
): RuleResult<keyof typeof errorMessages, RawOptions> {
856872
const options = upgradeRawOverridableOptions(rawOptions[0]);
857-
const optionsToUse = getCoreOptions<CoreOptions, Options>(
858-
node,
859-
context,
860-
options,
873+
const optionsToUse = getOptionsWithDefaults(
874+
getCoreOptions<CoreOptions, Options>(node, context, options),
861875
);
862876

863877
if (optionsToUse === null) {

0 commit comments

Comments
 (0)