-
-
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: add new rule sort-type-union-intersection-members (#501)
- Loading branch information
Geraint White
authored
Sep 14, 2021
1 parent
4265b27
commit fa4207d
Showing
6 changed files
with
436 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,101 @@ | ||
### `sort-type-union-intersection-members` | ||
|
||
_The `--fix` option on the command line automatically fixes problems reported by this rule._ | ||
|
||
Enforces that members of a type union/intersection are sorted alphabetically. | ||
|
||
#### Options | ||
|
||
You can specify the sort order using `order`. | ||
|
||
* `"asc"` (default) - enforce ascending sort order. | ||
* `"desc"` - enforce descending sort order. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/sort-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"order": "asc" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
You can disable checking intersection types using `checkIntersections`. | ||
|
||
* `true` (default) - enforce sort order of intersection members. | ||
* `false` - do not enforce sort order of intersection members. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/sort-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"checkIntersections": true | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
You can disable checking union types using `checkUnions`. | ||
|
||
* `true` (default) - enforce sort order of union members. | ||
* `false` - do not enforce sort order of union members. | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/sort-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"checkUnions": true | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
You can specify the ordering of groups using `groupOrder`. | ||
|
||
Each member of the type is placed into a group, and then the rule sorts alphabetically within each group. | ||
The ordering of groups is determined by this option. | ||
|
||
* `keyword` - Keyword types (`any`, `string`, etc) | ||
* `named` - Named types (`A`, `A['prop']`, `B[]`, `Array<C>`) | ||
* `literal` - Literal types (`1`, `'b'`, `true`, etc) | ||
* `function` - Function types (`() => void`) | ||
* `object` - Object types (`{ a: string }`, `{ [key: string]: number }`) | ||
* `tuple` - Tuple types (`[A, B, C]`) | ||
* `intersection` - Intersection types (`A & B`) | ||
* `union` - Union types (`A | B`) | ||
* `nullish` - `null` and `undefined` | ||
|
||
```js | ||
{ | ||
"rules": { | ||
"flowtype/sort-type-union-intersection-members": [ | ||
2, | ||
{ | ||
"groupOrder": [ | ||
'keyword', | ||
'named', | ||
'literal', | ||
'function', | ||
'object', | ||
'tuple', | ||
'intersection', | ||
'union', | ||
'nullish', | ||
] | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
<!-- assertions sortTypeUnionIntersectionMembers --> |
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,237 @@ | ||
const groups = { | ||
function: 'function', | ||
intersection: 'intersection', | ||
keyword: 'keyword', | ||
literal: 'literal', | ||
named: 'named', | ||
nullish: 'nullish', | ||
object: 'object', | ||
tuple: 'tuple', | ||
union: 'union', | ||
unknown: 'unknown', | ||
}; | ||
|
||
// eslint-disable-next-line complexity | ||
const getGroup = (node) => { | ||
switch (node.type) { | ||
case 'FunctionTypeAnnotation': | ||
return groups.function; | ||
|
||
case 'IntersectionTypeAnnotation': | ||
return groups.intersection; | ||
|
||
case 'AnyTypeAnnotation': | ||
case 'BooleanTypeAnnotation': | ||
case 'NumberTypeAnnotation': | ||
case 'StringTypeAnnotation': | ||
case 'SymbolTypeAnnotation': | ||
case 'ThisTypeAnnotation': | ||
return groups.keyword; | ||
|
||
case 'NullLiteralTypeAnnotation': | ||
case 'NullableTypeAnnotation': | ||
case 'VoidTypeAnnotation': | ||
return groups.nullish; | ||
|
||
case 'BooleanLiteralTypeAnnotation': | ||
case 'NumberLiteralTypeAnnotation': | ||
case 'StringLiteralTypeAnnotation': | ||
return groups.literal; | ||
|
||
case 'ArrayTypeAnnotation': | ||
case 'IndexedAccessType': | ||
case 'GenericTypeAnnotation': | ||
case 'OptionalIndexedAccessType': | ||
return groups.named; | ||
|
||
case 'ObjectTypeAnnotation': | ||
return groups.object; | ||
|
||
case 'TupleTypeAnnotation': | ||
return groups.tuple; | ||
|
||
case 'UnionTypeAnnotation': | ||
return groups.union; | ||
} | ||
|
||
return groups.unknown; | ||
}; | ||
|
||
const fallbackSort = (a, b) => { | ||
if (a < b) { | ||
return -1; | ||
} | ||
if (a > b) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}; | ||
|
||
const sorters = { | ||
asc: (collator, a, b) => { | ||
return collator.compare(a, b) || fallbackSort(a, b); | ||
}, | ||
desc: (collator, a, b) => { | ||
return collator.compare(b, a) || fallbackSort(b, a); | ||
}, | ||
}; | ||
|
||
const create = (context) => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
const { | ||
checkIntersections = true, | ||
checkUnions = true, | ||
groupOrder = [ | ||
groups.keyword, | ||
groups.named, | ||
groups.literal, | ||
groups.function, | ||
groups.object, | ||
groups.tuple, | ||
groups.intersection, | ||
groups.union, | ||
groups.nullish, | ||
], | ||
order = 'asc', | ||
} = context.options[1] || {}; | ||
|
||
const sort = sorters[order]; | ||
|
||
const collator = new Intl.Collator('en', { | ||
numeric: true, | ||
sensitivity: 'base', | ||
}); | ||
|
||
const checkSorting = (node) => { | ||
const sourceOrder = node.types.map((type) => { | ||
const group = groupOrder?.indexOf(getGroup(type)) ?? -1; | ||
|
||
return { | ||
group: group === -1 ? Number.MAX_SAFE_INTEGER : group, | ||
node: type, | ||
text: sourceCode.getText(type), | ||
}; | ||
}); | ||
|
||
const expectedOrder = [...sourceOrder].sort((a, b) => { | ||
if (a.group !== b.group) { | ||
return a.group - b.group; | ||
} | ||
|
||
return sort(collator, a.text, b.text); | ||
}); | ||
|
||
const hasComments = node.types.some((type) => { | ||
const count = | ||
sourceCode.getCommentsBefore(type).length + | ||
sourceCode.getCommentsAfter(type).length; | ||
|
||
return count > 0; | ||
}); | ||
|
||
let prev = null; | ||
|
||
for (let i = 0; i < expectedOrder.length; i += 1) { | ||
const type = node.type === 'UnionTypeAnnotation' ? 'union' : 'intersection'; | ||
const current = sourceOrder[i].text; | ||
const last = prev; | ||
|
||
// keep track of the last token | ||
prev = current || last; | ||
|
||
if (!last || !current) { | ||
continue; | ||
} | ||
|
||
if (expectedOrder[i].node !== sourceOrder[i].node) { | ||
const data = { | ||
current, | ||
last, | ||
order, | ||
type, | ||
}; | ||
|
||
const fix = (fixer) => { | ||
const sorted = expectedOrder | ||
.map((t) => { | ||
return t.text; | ||
}) | ||
.join( | ||
node.type === 'UnionTypeAnnotation' ? ' | ' : ' & ', | ||
); | ||
|
||
return fixer.replaceText(node, sorted); | ||
}; | ||
|
||
context.report({ | ||
data, | ||
messageId: 'notSorted', | ||
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) { | ||
checkSorting(node); | ||
} | ||
}, | ||
UnionTypeAnnotation (node) { | ||
if (checkUnions === true) { | ||
checkSorting(node); | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
export default { | ||
create, | ||
meta: { | ||
fixable: 'code', | ||
messages: { | ||
notSorted: 'Expected {{type}} members to be in {{order}}ending order. "{{current}}" should be before "{{last}}".', | ||
suggestFix: 'Sort members of type (removes all comments).', | ||
}, | ||
schema: [ | ||
{ | ||
properties: { | ||
checkIntersections: { | ||
type: 'boolean', | ||
}, | ||
checkUnions: { | ||
type: 'boolean', | ||
}, | ||
groupOrder: { | ||
items: { | ||
enum: Object.keys(groups), | ||
type: 'string', | ||
}, | ||
type: 'array', | ||
}, | ||
order: { | ||
enum: ['asc', 'desc'], | ||
type: 'string', | ||
}, | ||
}, | ||
type: 'object', | ||
}, | ||
], | ||
}, | ||
}; |
Oops, something went wrong.