Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): added new rule prefer-readonly (#555)
* feat(eslint-plugin): added new rule prefer-readonly Adds the equivalent of TSLint's `prefer-readonly` rule. * Added docs, auto-fixing * Updated docs; love the new build time checks! * Fixed linting errors (ha) and corrected internal source * PR feedback: non recommended; :exit; some test coverage * I guess tslintRuleName isn't allowed now? * Added back recommended as false * Removed :exit; fixed README.md table
- Loading branch information
1 parent
a53fc71
commit 76b89a5
Showing
12 changed files
with
1,010 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# require never-modified private members be marked as `readonly` | ||
|
||
This rule enforces that private members are marked as `readonly` if they're never modified outside of the constructor. | ||
|
||
## Rule Details | ||
|
||
Member variables with the privacy `private` 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`. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
class Container { | ||
// These member variables could be marked as readonly | ||
private neverModifiedMember = true; | ||
private onlyModifiedInConstructor: number; | ||
|
||
public constructor( | ||
onlyModifiedInConstructor: number, | ||
// Private parameter properties can also be marked as reaodnly | ||
private neverModifiedParameter: string, | ||
) { | ||
this.onlyModifiedInConstructor = onlyModifiedInConstructor; | ||
} | ||
} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
class Container { | ||
// Public members might be modified externally | ||
public publicMember: boolean; | ||
|
||
// Protected members might be modified by child classes | ||
protected protectedMember: number; | ||
|
||
// This is modified later on by the class | ||
private modifiedLater = 'unchanged'; | ||
|
||
public mutate() { | ||
this.modifiedLater = 'mutated'; | ||
} | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
This rule, in its default state, does not require any argument. | ||
|
||
### onlyInlineLambdas | ||
|
||
You may pass `"onlyInlineLambdas": true` as a rule option within an object to restrict checking only to members immediately assigned a lambda value. | ||
|
||
```cjson | ||
{ | ||
"@typescript-eslint/prefer-readonly": ["error", { "onlyInlineLambdas": true }] | ||
} | ||
``` | ||
|
||
Example of **correct** code for the `{ "onlyInlineLambdas": true }` options: | ||
|
||
```ts | ||
class Container { | ||
private neverModifiedPrivate = 'unchanged'; | ||
} | ||
``` | ||
|
||
Example of **incorrect** code for the `{ "onlyInlineLambdas": true }` options: | ||
|
||
```ts | ||
class Container { | ||
private onClick = () => { | ||
/* ... */ | ||
}; | ||
} | ||
``` | ||
|
||
## Related to | ||
|
||
- TSLint: ['prefer-readonly'](https://palantir.github.io/tslint/rules/prefer-readonly) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.