-
-
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.
feat: add rule to make sure that object type that is spread has exact…
… type (#391)
- Loading branch information
Showing
6 changed files
with
63 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,5 @@ | ||
### `spread-exact-type` | ||
|
||
Enforce object types, that are spread to be exact type explicitly. | ||
|
||
<!-- assertions spreadExactType --> |
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 = [ | ||
{ | ||
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 | ||
}; |
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,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>}' | ||
} | ||
] | ||
}; |
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