Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rule: no-decreasing-immutability #526

Open
danielnixon opened this issue Feb 3, 2023 · 0 comments
Open

New rule: no-decreasing-immutability #526

danielnixon opened this issue Feb 3, 2023 · 0 comments
Labels
Accepted This issue or PR has been accepted. Priority: Low Nice addition, maybe... someday... Type: Feature New features or options.

Comments

@danielnixon
Copy link

Suggestion

A new rule to prevent assignment of one value to another where the target has a lower immutability level (per https://github.com/RebeccaStevens/is-immutable-type/#definitions) than the source.

Such assignments can lead to surprising mutations in the source value.

For example, we might flag this as an issue:

export type ImmutableShallow<T extends {}> = {
  readonly [P in keyof T & {}]: T[P];
};

// An immutable array
const foo: ImmutableShallow<ReadonlyArray<string>> = ["a", "b", "c"];

// A deep readonly array
// This assignment from Immutable to DeepReadonly is what the suggested rule would flag
const bar: ReadonlyArray<string> = foo;

// This compiles because bar is only deep readonly
bar.find = () => "WHOOPS";

// logs "WHOOPS" - we accidentally mutated a seemingly immutable value
console.log(foo.find((a) => a.length > 0));

That example uses an assignment expression, but the rule would ideally target method passing, return values, etc.

And of course that example uses Immutable and DeepReadonly, but it would ideally catch any step downwards down that hierarchy.

Strictly speaking, assignments in the other direction are unsafe too (can lead to later surprising mutation in the immutable value that was assigned to). Anything that leads to two handles to the same value where those handles have different opinions about mutability will lead to the issue, but in practice its the downward direction assignments that cause the most trouble imo.

Some tentative stabs in that direction:

@danielnixon danielnixon added Status: Triage This issue needs to be triaged. Type: Idea Marks an idea, which might be accepted and implemented. labels Feb 3, 2023
@RebeccaStevens RebeccaStevens added Type: Feature New features or options. and removed Type: Idea Marks an idea, which might be accepted and implemented. Status: Triage This issue needs to be triaged. labels Mar 18, 2023
@RebeccaStevens RebeccaStevens added the Accepted This issue or PR has been accepted. label Jul 21, 2023
@RebeccaStevens RebeccaStevens added the Priority: Low Nice addition, maybe... someday... label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accepted This issue or PR has been accepted. Priority: Low Nice addition, maybe... someday... Type: Feature New features or options.
Projects
None yet
Development

No branches or pull requests

2 participants