diff --git a/src/rules/immutable-data.ts b/src/rules/immutable-data.ts index dbdf3ba1f..1010ffaba 100644 --- a/src/rules/immutable-data.ts +++ b/src/rules/immutable-data.ts @@ -110,13 +110,13 @@ const schema: JSONSchema4[] = [ /** * The default options for the rule. */ -const defaultOptions: RawOptions = [ +const defaultOptions = [ { ignoreClasses: false, ignoreImmediateMutation: true, ignoreNonConstDeclarations: false, }, -]; +] satisfies RawOptions; /** * The possible error messages. @@ -227,10 +227,8 @@ function checkAssignmentExpression( ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); const rootNode = findRootIdentifier(node.left) ?? node.left; - const optionsToUse = getCoreOptions( - rootNode, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(rootNode, context, options), ); if (optionsToUse === null) { @@ -306,10 +304,8 @@ function checkUnaryExpression( ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); const rootNode = findRootIdentifier(node.argument) ?? node.argument; - const optionsToUse = getCoreOptions( - rootNode, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(rootNode, context, options), ); if (optionsToUse === null) { @@ -384,10 +380,8 @@ function checkUpdateExpression( ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); const rootNode = findRootIdentifier(node.argument) ?? node.argument; - const optionsToUse = getCoreOptions( - rootNode, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(rootNode, context, options), ); if (optionsToUse === null) { @@ -533,6 +527,22 @@ function isInChainCallAndFollowsNew( return false; } +/** + * Add the default options to the given options. + */ +function getOptionsWithDefaults( + options: Readonly | null, +): Options | null { + if (options === null) { + return null; + } + + return { + ...defaultOptions[0], + ...options, + }; +} + /** * Check if the given node violates this rule. */ @@ -543,10 +553,8 @@ function checkCallExpression( ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); const rootNode = findRootIdentifier(node.callee) ?? node.callee; - const optionsToUse = getCoreOptions( - rootNode, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(rootNode, context, options), ); if (optionsToUse === null) { diff --git a/src/rules/prefer-immutable-types.ts b/src/rules/prefer-immutable-types.ts index 47c32bb78..c0d654b54 100644 --- a/src/rules/prefer-immutable-types.ts +++ b/src/rules/prefer-immutable-types.ts @@ -278,7 +278,7 @@ const schema: JSONSchema4[] = [ /** * The default options for the rule. */ -const defaultOptions: RawOptions = [ +const defaultOptions = [ { enforcement: Immutability.Immutable, ignoreInferredTypes: false, @@ -308,7 +308,7 @@ const defaultOptions: RawOptions = [ ], }, }, -]; +] satisfies RawOptions; /** * The possible error messages. @@ -496,10 +496,8 @@ function getParameterTypeViolations( const parameterProperty = isTSParameterProperty(param); const actualParam = parameterProperty ? param.parameter : param; - const optionsToUse = getCoreOptions( - param, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(param, context, options), ); if (optionsToUse === null) { @@ -637,11 +635,13 @@ function getReturnTypeViolations( options: Readonly, ): Descriptor[] { function getOptions(type: Type, typeNode: TypeNode | null) { - const optionsToUse = getCoreOptionsForType( - type, - typeNode, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptionsForType( + type, + typeNode, + context, + options, + ), ); if (optionsToUse === null) { @@ -824,6 +824,22 @@ function getReturnTypeViolations( ]; } +/** + * Add the default options to the given options. + */ +function getOptionsWithDefaults( + options: Readonly | null, +): Options | null { + if (options === null) { + return null; + } + + return { + ...defaultOptions[0], + ...options, + }; +} + /** * Check if the given function node violates this rule. */ @@ -854,10 +870,8 @@ function checkVariable( rawOptions: Readonly, ): RuleResult { const options = upgradeRawOverridableOptions(rawOptions[0]); - const optionsToUse = getCoreOptions( - node, - context, - options, + const optionsToUse = getOptionsWithDefaults( + getCoreOptions(node, context, options), ); if (optionsToUse === null) {