Skip to content

Commit

Permalink
feat!: replace spread last sort-array-includes option with group kind
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Jul 22, 2024
1 parent fd7b121 commit 721e1ee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
16 changes: 9 additions & 7 deletions docs/content/rules/sort-array-includes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ Controls whether sorting should be case-sensitive or not.
- `false` — Consider case when sorting (e.g., “A” comes before “a”).


### spreadLast
### groupKind

<sub>default: `true`</sub>
<sub>default: `'literals-first'`</sub>

Allows you to group array elements by their kind, determining whether spread values should come before or after literal values.

Determines the position of spread elements within the array.
- `mixed` — Do not group array elements by their kind; spread values are sorted together with literal values.
- `literals-first` — Group all literal values before spread values.
- `spread-first` — Group all spread values before literal values.

- `true` — Always place spread elements at the end of the array.
- `false` — Do not enforce a specific position for spread elements.

## Usage

Expand All @@ -178,7 +180,7 @@ Determines the position of spread elements within the array.
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
spreadLast: true,
groupKind: 'literals-first',
},
],
},
Expand All @@ -202,7 +204,7 @@ Determines the position of spread elements within the array.
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
spreadLast: true,
groupKind: 'literals-first',
},
],
},
Expand Down
44 changes: 25 additions & 19 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type MESSAGE_ID = 'unexpectedArrayIncludesOrder'

type Options = [
Partial<{
groupKind: 'literals-first' | 'spreads-first' | 'mixed'
type: 'alphabetical' | 'line-length' | 'natural'
order: 'desc' | 'asc'
spreadLast: boolean
ignoreCase: boolean
}>,
]
Expand Down Expand Up @@ -55,10 +55,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
'Controls whether sorting should be case-sensitive or not.',
type: 'boolean',
},
spreadLast: {
description:
'Determines the position of spread elements within the array.',
type: 'boolean',
groupKind: {
description: 'Specifies top-level groups.',
enum: ['mixed', 'literals-first', 'spreads-first'],
type: 'string',
},
},
additionalProperties: false,
Expand All @@ -74,7 +74,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
spreadLast: true,
groupKind: 'literals-first',
},
],
create: context => ({
Expand All @@ -92,8 +92,8 @@ export default createEslintRule<Options, MESSAGE_ID>({

if (elements.length > 1) {
let options = complete(context.options.at(0), {
groupKind: 'literals-first',
type: 'alphabetical',
spreadLast: true,
ignoreCase: true,
order: 'asc',
} as const)
Expand All @@ -107,7 +107,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
) => {
if (element !== null) {
let group = 'unknown'
if (options.spreadLast) {
if (typeof options.groupKind === 'string') {
group =
element.type === 'SpreadElement' ? 'spread' : 'literal'
}
Expand All @@ -130,16 +130,21 @@ export default createEslintRule<Options, MESSAGE_ID>({
.flat()

pairwise(nodes, (left, right) => {
let groupKindOrder = options.spreadLast
? ['literal', 'spread']
: ['unknown']
let groupKindOrder = ['unknown']

if (typeof options.groupKind === 'string') {
groupKindOrder =
options.groupKind === 'literals-first'
? ['literal', 'spread']
: ['spread', 'literal']
}

let leftNum = getGroupNumber(groupKindOrder, left)
let rightNum = getGroupNumber(groupKindOrder, right)

if (
(options.spreadLast && leftNum > rightNum) ||
((!options.spreadLast || leftNum === rightNum) &&
(options.groupKind !== 'mixed' && leftNum > rightNum) ||
((options.groupKind === 'mixed' || leftNum === rightNum) &&
isPositive(compare(left, right, options)))
) {
context.report({
Expand All @@ -150,12 +155,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
node: right.node,
fix: fixer => {
let sortedNodes = options.spreadLast
? groupKindOrder
.map(group => nodes.filter(n => n.group === group))
.map(groupedNodes => sortNodes(groupedNodes, options))
.flat()
: sortNodes(nodes, options)
let sortedNodes =
options.groupKind !== 'mixed'
? groupKindOrder
.map(group => nodes.filter(n => n.group === group))
.map(groupedNodes => sortNodes(groupedNodes, options))
.flat()
: sortNodes(nodes, options)

return makeFixes(fixer, nodes, sortedNodes, sourceCode)
},
Expand Down
24 changes: 12 additions & 12 deletions test/sort-array-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
},
Expand All @@ -189,7 +189,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
errors: [
Expand Down Expand Up @@ -266,7 +266,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
},
Expand All @@ -292,7 +292,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
errors: [
Expand Down Expand Up @@ -464,7 +464,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
},
Expand All @@ -480,7 +480,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
errors: [
Expand Down Expand Up @@ -557,7 +557,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
},
Expand All @@ -583,7 +583,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
errors: [
Expand Down Expand Up @@ -735,7 +735,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
},
Expand All @@ -751,7 +751,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: true,
groupKind: 'literals-first',
},
],
errors: [
Expand Down Expand Up @@ -828,7 +828,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
},
Expand All @@ -854,7 +854,7 @@ describe(ruleName, () => {
options: [
{
...options,
spreadLast: false,
groupKind: 'mixed',
},
],
errors: [
Expand Down

0 comments on commit 721e1ee

Please sign in to comment.