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

feat(eslint-plugin): [prefer-readonly] private fields support #7686

Merged
merged 9 commits into from
Oct 19, 2023
10 changes: 9 additions & 1 deletion packages/eslint-plugin/docs/rules/prefer-readonly.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: "Require private members to be marked as `readonly` if they're neve
>
> See **https://typescript-eslint.io/rules/prefer-readonly** for documentation.

Member variables with the privacy `private` are never permitted to be modified outside of their declaring class.
Private member variables (whether using the `private` modifier or private `#` fields) are never permitted to be modified outside of their declaring class.
If that class never modifies their value, they may safely be marked as `readonly`.

This rule reports on private members are marked as `readonly` if they're never modified outside of the constructor.
Expand All @@ -22,6 +22,7 @@ class Container {
// These member variables could be marked as readonly
private neverModifiedMember = true;
private onlyModifiedInConstructor: number;
#neverModifiedPrivateField = 3;
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

public constructor(
onlyModifiedInConstructor: number,
Expand Down Expand Up @@ -49,6 +50,13 @@ class Container {
public mutate() {
this.modifiedLater = 'mutated';
}

// This is modified later on by the class
#modifiedLaterPrivateField = 'unchanged';

public mutatePrivateField() {
this.#modifiedLaterPrivateField = 'mutated';
}
}
```

Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/prefer-readonly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,10 @@ class ClassScope {

public addDeclaredVariable(node: ParameterOrPropertyDeclaration): void {
if (
!tsutils.isModifierFlagSet(node, ts.ModifierFlags.Private) ||
!(
tsutils.isModifierFlagSet(node, ts.ModifierFlags.Private) ||
node.name.kind === ts.SyntaxKind.PrivateIdentifier
) ||
tsutils.isModifierFlagSet(node, ts.ModifierFlags.Readonly) ||
ts.isComputedPropertyName(node.name)
) {
Expand Down