Skip to content

Commit

Permalink
feat: add rule to make sure that object type that is spread has exact…
Browse files Browse the repository at this point in the history
… type (#391)
  • Loading branch information
Kiwka authored and gajus committed Apr 8, 2019
1 parent 0f8a9b5 commit 52b0c00
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions .README/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ When `true`, only checks files with a [`@flow` annotation](http://flowtype.org/d
{"gitdown": "include", "file": "./rules/space-after-type-colon.md"}
{"gitdown": "include", "file": "./rules/space-before-generic-bracket.md"}
{"gitdown": "include", "file": "./rules/space-before-type-colon.md"}
{"gitdown": "include", "file": "./rules/spread-exact-type.md"}
{"gitdown": "include", "file": "./rules/type-id-match.md"}
{"gitdown": "include", "file": "./rules/type-import-style.md"}
{"gitdown": "include", "file": "./rules/union-intersection-spacing.md"}
Expand Down
5 changes: 5 additions & 0 deletions .README/rules/spread-exact-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### `spread-exact-type`

Enforce object types, that are spread to be exact type explicitly.

<!-- assertions spreadExactType -->
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import typeImportStyle from './rules/typeImportStyle';
import unionIntersectionSpacing from './rules/unionIntersectionSpacing';
import useFlowType from './rules/useFlowType';
import validSyntax from './rules/validSyntax';
import spreadExactType from './rules/spreadExactType';
import {checkFlowFileAnnotation} from './utilities';

const rules = {
Expand Down Expand Up @@ -66,6 +67,7 @@ const rules = {
'space-after-type-colon': spaceAfterTypeColon,
'space-before-generic-bracket': spaceBeforeGenericBracket,
'space-before-type-colon': spaceBeforeTypeColon,
'spread-exact-type': spreadExactType,
'type-id-match': typeIdMatch,
'type-import-style': typeImportStyle,
'union-intersection-spacing': unionIntersectionSpacing,
Expand Down Expand Up @@ -109,6 +111,7 @@ export default {
'space-after-type-colon': 0,
'space-before-generic-bracket': 0,
'space-before-type-colon': 0,
'spread-exact-type': 0,
'type-id-match': 0,
'type-import-style': 0,
'union-intersection-spacing': 0,
Expand Down
33 changes: 33 additions & 0 deletions src/rules/spreadExactType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const schema = [
{
enum: ['always', 'never'],
type: 'string'
}
];

const create = (context) => {
return {
ObjectTypeAnnotation (node) {
const {properties} = node;

properties.forEach((property) => {
const {type} = property;
if (type === 'ObjectTypeSpreadProperty') {
const {argument: {type: argumentType, id: argumentId}} = property;
if (
argumentType !== 'GenericTypeAnnotation' || argumentId.name !== '$Exact') {
context.report({
message: 'Use $Exact to make type spreading safe.',
node
});
}
}
});
}
};
};

export default {
create,
schema
};
20 changes: 20 additions & 0 deletions tests/rules/assertions/spreadExactType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export default {
invalid: [
{
code: 'type bar = {...{test: string}}',
errors: [{message: 'Use $Exact to make type spreading safe.'}]
},
{
code: 'type foo = {test: number}; type bar = {...foo}',
errors: [{message: 'Use $Exact to make type spreading safe.'}]
}
],
valid: [
{
code: 'type bar = {...$Exact<{test: string}>}'
},
{
code: 'type foo = {test: number}; type bar = {...$Exact<foo>}'
}
]
};
1 change: 1 addition & 0 deletions tests/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const reportingRules = [
'space-after-type-colon',
'space-before-generic-bracket',
'space-before-type-colon',
'spread-exact-type',
'type-id-match',
'type-import-style',
'union-intersection-spacing',
Expand Down

0 comments on commit 52b0c00

Please sign in to comment.