Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/**
* The default options for the rule.
*/
const defaultOptions: RawOptions = [
const defaultOptions = [
{
enforcement: Immutability.Immutable,
ignoreInferredTypes: false,
Expand Down Expand Up @@ -308,7 +308,7 @@
],
},
},
];
] satisfies RawOptions;

/**
* The possible error messages.
Expand Down Expand Up @@ -496,10 +496,8 @@
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 @@ -616,7 +614,7 @@
},
fix,
suggest:
suggestionFixers?.map(({ fix, message }) => ({

Check warning on line 617 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js

'fix' is already declared in the upper scope on line 598 column 15

Check warning on line 617 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

'fix' is already declared in the upper scope on line 598 column 15
messageId: "userDefined",
data: {
message,
Expand All @@ -637,11 +635,13 @@
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 @@ -741,7 +741,7 @@
},
fix,
suggest:
suggestionFixers?.map(({ fix, message }) => ({

Check warning on line 744 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js

'fix' is already declared in the upper scope on line 727 column 15

Check warning on line 744 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

'fix' is already declared in the upper scope on line 727 column 15
messageId: "userDefined",
data: {
message,
Expand Down Expand Up @@ -813,7 +813,7 @@
},
fix,
suggest:
suggestionFixers?.map(({ fix, message }) => ({

Check warning on line 816 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js

'fix' is already declared in the upper scope on line 796 column 11

Check warning on line 816 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

'fix' is already declared in the upper scope on line 796 column 11
messageId: "userDefined",
data: {
message,
Expand All @@ -824,6 +824,22 @@
];
}

/**
* 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 @@
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 @@ -1014,7 +1028,7 @@
data,
fix,
suggest:
suggestionFixers?.map(({ fix, message }) => ({

Check warning on line 1031 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js

'fix' is already declared in the upper scope on line 1019 column 38

Check warning on line 1031 in src/rules/prefer-immutable-types.ts

View workflow job for this annotation

GitHub Actions / lint_js / lint_js

'fix' is already declared in the upper scope on line 1019 column 38
messageId: "userDefined",
data: {
message,
Expand Down
Loading