-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* 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`
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
### `no-mutable-array` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Requires use of [`$ReadOnlyArray`](https://github.com/facebook/flow/blob/v0.46.0/lib/core.js#L185) instead of just `Array` or array [shorthand notation](https://flow.org/en/docs/types/arrays/#toc-array-type-shorthand-syntax). `$ReadOnlyArray` is immutable array collection type and the superclass of Array and tuple types in Flow. Use of `$ReadOnlyArray` instead of `Array` can solve some "problems" in typing with Flow (e.g., [1](https://github.com/facebook/flow/issues/3425), [2](https://github.com/facebook/flow/issues/4251)). | ||
|
||
General reasons for using immutable data structures: | ||
|
||
* They 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 | ||
|
||
<!-- assertions noMutableArray --> |
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,33 @@ | ||
const schema = []; | ||
|
||
const create = (context) => { | ||
return { | ||
ArrayTypeAnnotation (node) { | ||
context.report({ | ||
fix (fixer) { | ||
const rawElementType = context.getSourceCode().getText(node.elementType); | ||
|
||
return fixer.replaceText(node, '$ReadOnlyArray<' + rawElementType + '>'); | ||
}, | ||
message: 'Use "$ReadOnlyArray" instead of array shorthand notation', | ||
node | ||
}); | ||
}, | ||
GenericTypeAnnotation (node) { | ||
if (node.id.name === 'Array') { | ||
context.report({ | ||
fix (fixer) { | ||
return fixer.replaceText(node.id, '$ReadOnlyArray'); | ||
}, | ||
message: 'Use "$ReadOnlyArray" instead of "Array"', | ||
node | ||
}); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; |
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,19 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'type X = Array<string>', | ||
errors: [{message: 'Use "$ReadOnlyArray" instead of "Array"'}], | ||
output: 'type X = $ReadOnlyArray<string>' | ||
}, | ||
{ | ||
code: 'type X = string[]', | ||
errors: [{message: 'Use "$ReadOnlyArray" instead of array shorthand notation'}], | ||
output: 'type X = $ReadOnlyArray<string>' | ||
} | ||
], | ||
valid: [ | ||
{ | ||
code: 'type X = $ReadOnlyArray<string>' | ||
} | ||
] | ||
}; |
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