Skip to content

Commit

Permalink
fix: ensure default options are applied (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Oct 20, 2024
1 parent 06f2b80 commit 5b57cd2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
44 changes: 26 additions & 18 deletions src/rules/immutable-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -227,10 +227,8 @@ function checkAssignmentExpression(
): RuleResult<keyof typeof errorMessages, RawOptions> {
const options = upgradeRawOverridableOptions(rawOptions[0]);
const rootNode = findRootIdentifier(node.left) ?? node.left;
const optionsToUse = getCoreOptions<CoreOptions, Options>(
rootNode,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -306,10 +304,8 @@ function checkUnaryExpression(
): RuleResult<keyof typeof errorMessages, RawOptions> {
const options = upgradeRawOverridableOptions(rawOptions[0]);
const rootNode = findRootIdentifier(node.argument) ?? node.argument;
const optionsToUse = getCoreOptions<CoreOptions, Options>(
rootNode,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -384,10 +380,8 @@ function checkUpdateExpression(
): RuleResult<keyof typeof errorMessages, RawOptions> {
const options = upgradeRawOverridableOptions(rawOptions[0]);
const rootNode = findRootIdentifier(node.argument) ?? node.argument;
const optionsToUse = getCoreOptions<CoreOptions, Options>(
rootNode,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -533,6 +527,22 @@ function isInChainCallAndFollowsNew(
return false;
}

/**
* Add the default options to the given options.
*/
function getOptionsWithDefaults(
options: Readonly<Options> | null,
): Options | null {
if (options === null) {
return null;
}

return {
...defaultOptions[0],
...options,
};
}

/**
* Check if the given node violates this rule.
*/
Expand All @@ -543,10 +553,8 @@ function checkCallExpression(
): RuleResult<keyof typeof errorMessages, RawOptions> {
const options = upgradeRawOverridableOptions(rawOptions[0]);
const rootNode = findRootIdentifier(node.callee) ?? node.callee;
const optionsToUse = getCoreOptions<CoreOptions, Options>(
rootNode,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(rootNode, context, options),
);

if (optionsToUse === null) {
Expand Down
44 changes: 29 additions & 15 deletions src/rules/prefer-immutable-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ const schema: JSONSchema4[] = [
/**
* The default options for the rule.
*/
const defaultOptions: RawOptions = [
const defaultOptions = [
{
enforcement: Immutability.Immutable,
ignoreInferredTypes: false,
Expand Down Expand Up @@ -308,7 +308,7 @@ const defaultOptions: RawOptions = [
],
},
},
];
] satisfies RawOptions;

/**
* The possible error messages.
Expand Down Expand Up @@ -496,10 +496,8 @@ function getParameterTypeViolations(
const parameterProperty = isTSParameterProperty(param);
const actualParam = parameterProperty ? param.parameter : param;

const optionsToUse = getCoreOptions<CoreOptions, Options>(
param,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptions<CoreOptions, Options>(param, context, options),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -637,11 +635,13 @@ function getReturnTypeViolations(
options: Readonly<Options>,
): Descriptor[] {
function getOptions(type: Type, typeNode: TypeNode | null) {
const optionsToUse = getCoreOptionsForType<CoreOptions, Options>(
type,
typeNode,
context,
options,
const optionsToUse = getOptionsWithDefaults(
getCoreOptionsForType<CoreOptions, Options>(
type,
typeNode,
context,
options,
),
);

if (optionsToUse === null) {
Expand Down Expand Up @@ -824,6 +824,22 @@ function getReturnTypeViolations(
];
}

/**
* Add the default options to the given options.
*/
function getOptionsWithDefaults(
options: Readonly<Options> | null,
): Options | null {
if (options === null) {
return null;
}

return {
...defaultOptions[0],
...options,
};
}

/**
* Check if the given function node violates this rule.
*/
Expand Down Expand Up @@ -854,10 +870,8 @@ function checkVariable(
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

0 comments on commit 5b57cd2

Please sign in to comment.