-
-
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-exact-type rule (fixes #304) * docs: update README
- Loading branch information
1 parent
c898dec
commit 2008687
Showing
8 changed files
with
339 additions
and
1 deletion.
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,32 @@ | ||
### `require-exact-type` | ||
|
||
This rule enforces [exact object types](https://flow.org/en/docs/types/objects/#toc-exact-object-types). | ||
|
||
#### Options | ||
|
||
The rule has one string option: | ||
|
||
* `"always"` (default): Report all object type definitions that aren't exact. | ||
* `"never"`: Report all object type definitions that are exact. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/require-exact-type": [ | ||
2, | ||
"always" | ||
] | ||
} | ||
} | ||
|
||
{ | ||
"rules": { | ||
"flowtype/require-exact-type": [ | ||
2, | ||
"never" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
<!-- assertions requireExactType --> |
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,39 @@ | ||
const schema = [ | ||
{ | ||
enum: ['always', 'never'], | ||
type: 'string' | ||
} | ||
]; | ||
|
||
const create = (context) => { | ||
const always = (context.options[0] || 'always') === 'always'; | ||
|
||
return { | ||
TypeAlias (node) { | ||
const {id: {name}, right: {type, exact}} = node; | ||
|
||
if (type === 'ObjectTypeAnnotation') { | ||
if (always && !exact) { | ||
context.report({ | ||
data: {name}, | ||
message: 'Type identifier \'{{name}}\' must be exact.', | ||
node | ||
}); | ||
} | ||
|
||
if (!always && exact) { | ||
context.report({ | ||
data: {name}, | ||
message: 'Type identifier \'{{name}}\' must not be exact.', | ||
node | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
schema | ||
}; |
Oops, something went wrong.