Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-readonly] private fields support (#7686)
Browse files Browse the repository at this point in the history
* Add private field support to prefer-readonly

* Fix change in prefer-readonly docs

* Restore missing test

* Update docs to mention private fields

* Update packages/eslint-plugin/docs/rules/prefer-readonly.md

---------

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
Jamesernator and JoshuaKGoldberg committed Oct 19, 2023
1 parent 76ab373 commit 0e875bf
Show file tree
Hide file tree
Showing 3 changed files with 911 additions and 11 deletions.
10 changes: 9 additions & 1 deletion packages/eslint-plugin/docs/rules/prefer-readonly.md
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;

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
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

0 comments on commit 0e875bf

Please sign in to comment.