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

Add a rule that requires to use $ReadOnlyArray instead of Array #236

Closed
gajus opened this issue Jun 5, 2017 · 3 comments
Closed

Add a rule that requires to use $ReadOnlyArray instead of Array #236

gajus opened this issue Jun 5, 2017 · 3 comments

Comments

@gajus
Copy link
Owner

gajus commented Jun 5, 2017

Rule must warn when Array is used instead of $ReadOnlyArray.

@pnevyk
Copy link
Contributor

pnevyk commented Oct 1, 2017

@gajus I am working on this. And I have basically three questions:

  • Should this rule apply also on string[] notation? I would guess so. It should be probably configurable.
  • Does this rule need any specific configuration (apart from one mentioned above)? For example to specify exceptions for this rule somehow, or any other.
  • Do you have a nice text about reason for this use? I think it should be noted in the docs. I found this general article about immutability and some Flow issues where $ReadOnlyArray solves problems with Array (1, 2).

@gajus
Copy link
Owner Author

gajus commented Oct 1, 2017

Should this rule apply also on string[] notation?

Yes. Regardless of whether the array contains primitive data or not, it is mutable. There is no short-hand syntax for non-mutable array.

Does this rule need any specific configuration (apart from one mentioned above)? For example to specify exceptions for this rule somehow, or any other.

I cannot think of any exceptions.

It should always require to use $ReadOnlyArray. When user intends to use Array, he should use // eslint-disable-next-line require-read-only-array.

Do you have a nice text about reason for this use? I think it should be noted in the docs. I found this general article about immutability and some Flow issues where $ReadOnlyArray solves problems with Array (1, 2).

All generic pro-immutable data arguments apply, e.g.

  • immutable objects are simpler to construct, test, and use
  • they help to avoid temporal coupling
  • their usage is side-effect free (no defensive copies)
  • identity mutability problem is avoided
  • they always have failure atomicity
  • they are much easier to cache

– https://softwareengineering.stackexchange.com/questions/151733/if-immutable-objects-are-good-why-do-people-keep-creating-mutable-objects

pnevyk added a commit to pnevyk/eslint-plugin-flowtype that referenced this issue Oct 1, 2017
This rule enforces use of `$ReadOnlyArray` instead of `Array` or array
shorthand notation.
gajus pushed a commit that referenced this issue Oct 6, 2017
* feat: Add `require-read-only-array` rule (#236)

This rule enforces use of `$ReadOnlyArray` instead of `Array` or array
shorthand notation.

* feat: Rename the rule to `no-mutable-array`
@pnevyk
Copy link
Contributor

pnevyk commented Feb 18, 2018

@gajus This is already merged. You can close this issue.

@gajus gajus closed this as completed Mar 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants