-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate a new rule 'use-read-only-spread' (#472)
This commit adds a new rule 'use-read-only-spread' originally developed and used here: https://github.com/adeira/universe/blob/91ba39d4f59ac0376121b67e443f474a6ade4feb/src/eslint-plugin-adeira/src/rules/flow-use-readonly-spread.js It prevents users from accidentally creating an object which is no longer read-only because of Flow spread operator.
- Loading branch information
Showing
7 changed files
with
263 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,39 @@ | ||
### `use-read-only-spread` | ||
|
||
Warns against accidentally creating an object which is no longer read-only because of how spread operator works in Flow. Imagine the following code: | ||
|
||
```flow js | ||
type INode = {| | ||
+type: string, | ||
|}; | ||
type Identifier = {| | ||
...INode, | ||
+name: string, | ||
|}; | ||
``` | ||
|
||
You might expect the identifier name to be read-only, however, that's not true ([flow.org/try](https://flow.org/try/#0C4TwDgpgBAkgcgewCbQLxQN4B8BQUoDUokAXFAM7ABOAlgHYDmANDlgL4DcOOx0MKdYDQBmNCFSjpseKADp58ZBBb4CdAIYBbCGUq1GLdlxwBjBHUpQAHmX4RBIsRKlQN2sgHIPTKL08eoTm4rWV5JKA8AZQALBABXABskVwRgKAAjaAB3WmB1dISIAEIPLhC3NAiY+KSUtMyoHJo8guLSnCA)): | ||
|
||
```flow js | ||
const x: Identifier = { name: '', type: '' }; | ||
x.type = 'Should not be writable!'; // No Flow error | ||
x.name = 'Should not be writable!'; // No Flow error | ||
``` | ||
|
||
This rule suggests to use `$ReadOnly<…>` to prevent accidental loss of readonly-ness: | ||
|
||
```flow js | ||
type Identifier = $ReadOnly<{| | ||
...INode, | ||
+name: string, | ||
|}>; | ||
const x: Identifier = { name: '', type: '' }; | ||
x.type = 'Should not be writable!'; // $FlowExpectedError[cannot-write] | ||
x.name = 'Should not be writable!'; // $FlowExpectedError[cannot-write] | ||
``` | ||
|
||
<!-- assertions useReadOnlySpread --> |
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,44 @@ | ||
const meta = { | ||
messages: { | ||
readonlySpread: | ||
'Flow type with spread property and all readonly properties should be ' + | ||
'wrapped in \'$ReadOnly<…>\' to prevent accidental loss of readonly-ness.', | ||
}, | ||
}; | ||
|
||
const create = (context) => { | ||
return { | ||
TypeAlias (node) { | ||
if (node.right.type === 'GenericTypeAnnotation' && node.right.id.name === '$ReadOnly') { | ||
// it's already $ReadOnly<…>, nothing to do | ||
} else if (node.right.type === 'ObjectTypeAnnotation') { | ||
// let's iterate all props and if everything is readonly then throw | ||
let shouldThrow = false; | ||
let hasSpread = false; | ||
for (const property of node.right.properties) { | ||
if (property.type === 'ObjectTypeProperty') { | ||
if (property.variance && property.variance.kind === 'plus') { | ||
shouldThrow = true; | ||
} else { | ||
shouldThrow = false; | ||
break; | ||
} | ||
} else if (property.type === 'ObjectTypeSpreadProperty') { | ||
hasSpread = true; | ||
} | ||
} | ||
if (hasSpread === true && shouldThrow === true) { | ||
context.report({ | ||
messageId: 'readonlySpread', | ||
node: node.right, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta, | ||
}; |
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,76 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = {| | ||
...INode, | ||
+aaa: string, | ||
|};`, | ||
errors: [{ | ||
message: 'Flow type with spread property and all readonly properties should be wrapped in \'$ReadOnly<…>\' to prevent accidental loss of readonly-ness.', | ||
}], | ||
}, | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = {| | ||
...INode, | ||
+aaa: string, | ||
+bbb: string, | ||
|};`, | ||
errors: [{ | ||
message: 'Flow type with spread property and all readonly properties should be wrapped in \'$ReadOnly<…>\' to prevent accidental loss of readonly-ness.', | ||
}], | ||
}, | ||
], | ||
|
||
valid: [ | ||
// Object with spread operator: | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = {| | ||
...INode, | ||
name: string, | ||
|};`, | ||
}, | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = {| | ||
...INode, | ||
name: string, // writable on purpose | ||
+surname: string, | ||
|};`, | ||
}, | ||
|
||
// Object without spread operator: | ||
{ | ||
code: `type Identifier = {| | ||
+name: string, | ||
|};`, | ||
}, | ||
|
||
// Read-only object with spread: | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = $ReadOnly<{| | ||
...INode, | ||
+name: string, | ||
|}>;`, | ||
}, | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = $ReadOnly<{| | ||
...INode, | ||
name: string, // writable on purpose | ||
|}>;`, | ||
}, | ||
|
||
// Write-only object with spread: | ||
{ | ||
code: `type INode = {||}; | ||
type Identifier = $ReadOnly<{| | ||
...INode, | ||
-name: 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