-
-
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 new rule no-duplicate-type-union-intersection-members (#503)
- Loading branch information
Geraint White
authored
Sep 20, 2021
1 parent
0b5b3a8
commit 1c1c009
Showing
6 changed files
with
205 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
43 changes: 43 additions & 0 deletions
43
.README/rules/no-duplicate-type-union-intersection-members.md
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,43 @@ | ||
### `no-duplicate-type-union-intersection-members` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Checks for duplicate members of a type union/intersection. | ||
|
||
#### Options | ||
|
||
You can disable checking intersection types using `checkIntersections`. | ||
|
||
* `true` (default) - check for duplicate members of intersection members. | ||
* `false` - do not check for duplicate members of intersection members. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/no-duplicate-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"checkIntersections": true | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
You can disable checking union types using `checkUnions`. | ||
|
||
* `true` (default) - check for duplicate members of union members. | ||
* `false` - do not check for duplicate members of union members. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/no-duplicate-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"checkUnions": true | ||
} | ||
] | ||
} | ||
} | ||
``` |
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,113 @@ | ||
const create = (context) => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const { | ||
checkIntersections = true, | ||
checkUnions = true, | ||
} = context.options[1] || {}; | ||
|
||
const checkForDuplicates = (node) => { | ||
const uniqueMembers = []; | ||
const duplicates = []; | ||
|
||
const source = node.types.map((type) => { | ||
return { | ||
node: type, | ||
text: sourceCode.getText(type), | ||
}; | ||
}); | ||
|
||
const hasComments = node.types.some((type) => { | ||
const count = | ||
sourceCode.getCommentsBefore(type).length + | ||
sourceCode.getCommentsAfter(type).length; | ||
|
||
return count > 0; | ||
}); | ||
|
||
const fix = (fixer) => { | ||
const result = uniqueMembers | ||
.map((t) => { | ||
return t.text; | ||
}) | ||
.join( | ||
node.type === 'UnionTypeAnnotation' ? ' | ' : ' & ', | ||
); | ||
|
||
return fixer.replaceText(node, result); | ||
}; | ||
|
||
for (const member of source) { | ||
const match = uniqueMembers.find((uniqueMember) => { | ||
return uniqueMember.text === member.text; | ||
}); | ||
|
||
if (match) { | ||
duplicates.push(member); | ||
} else { | ||
uniqueMembers.push(member); | ||
} | ||
} | ||
|
||
for (const duplicate of duplicates) { | ||
context.report({ | ||
data: { | ||
name: duplicate.text, | ||
type: node.type === 'UnionTypeAnnotation' ? 'union' : 'intersection', | ||
}, | ||
messageId: 'duplicate', | ||
node, | ||
|
||
// don't autofix if any of the types have leading/trailing comments | ||
// the logic for preserving them correctly is a pain - we may implement this later | ||
...hasComments ? | ||
{ | ||
suggest: [ | ||
{ | ||
fix, | ||
messageId: 'suggestFix', | ||
}, | ||
], | ||
} : | ||
{fix}, | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
IntersectionTypeAnnotation (node) { | ||
if (checkIntersections === true) { | ||
checkForDuplicates(node); | ||
} | ||
}, | ||
UnionTypeAnnotation (node) { | ||
if (checkUnions === true) { | ||
checkForDuplicates(node); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta: { | ||
fixable: 'code', | ||
messages: { | ||
duplicate: 'Duplicate {{type}} member found "{{name}}".', | ||
suggestFix: 'Remove duplicate members of type (removes all comments).', | ||
}, | ||
schema: [ | ||
{ | ||
properties: { | ||
checkIntersections: { | ||
type: 'boolean', | ||
}, | ||
checkUnions: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
}; |
44 changes: 44 additions & 0 deletions
44
tests/rules/assertions/noDuplicateTypeUnionIntersectionMembers.js
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 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'type A = 1 | 2 | 3 | 1;', | ||
errors: [{message: 'Duplicate union member found "1".'}], | ||
output: 'type A = 1 | 2 | 3;', | ||
}, | ||
{ | ||
code: 'type B = \'foo\' | \'bar\' | \'foo\';', | ||
errors: [{message: 'Duplicate union member found "\'foo\'".'}], | ||
output: 'type B = \'foo\' | \'bar\';', | ||
}, | ||
{ | ||
code: 'type C = A | B | A | B;', | ||
errors: [ | ||
{message: 'Duplicate union member found "A".'}, | ||
{message: 'Duplicate union member found "B".'}, | ||
], | ||
output: 'type C = A | B;', | ||
}, | ||
{ | ||
code: 'type C = A & B & A & B;', | ||
errors: [ | ||
{message: 'Duplicate intersection member found "A".'}, | ||
{message: 'Duplicate intersection member found "B".'}, | ||
], | ||
output: 'type C = A & B;', | ||
}, | ||
], | ||
valid: [ | ||
{ | ||
code: 'type A = 1 | 2 | 3;', | ||
}, | ||
{ | ||
code: 'type B = \'foo\' | \'bar\';', | ||
}, | ||
{ | ||
code: 'type C = A | B;', | ||
}, | ||
{ | ||
code: 'type C = A & B;', | ||
}, | ||
], | ||
}; |
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