eslint-plugin-perfectionist 3.6.0 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/dist/index.d.ts +47 -4
  2. package/dist/rules/sort-array-includes.js +124 -60
  3. package/dist/rules/sort-array-includes.mjs +112 -58
  4. package/dist/rules/sort-astro-attributes.js +26 -45
  5. package/dist/rules/sort-astro-attributes.mjs +18 -34
  6. package/dist/rules/sort-classes-utils.js +4 -8
  7. package/dist/rules/sort-classes-utils.mjs +4 -8
  8. package/dist/rules/sort-classes.js +70 -41
  9. package/dist/rules/sort-classes.mjs +68 -44
  10. package/dist/rules/sort-enums.js +81 -39
  11. package/dist/rules/sort-enums.mjs +72 -37
  12. package/dist/rules/sort-exports.js +95 -7
  13. package/dist/rules/sort-exports.mjs +91 -12
  14. package/dist/rules/sort-imports.js +57 -29
  15. package/dist/rules/sort-imports.mjs +54 -32
  16. package/dist/rules/sort-interfaces.js +93 -107
  17. package/dist/rules/sort-interfaces.mjs +76 -96
  18. package/dist/rules/sort-intersection-types.js +163 -119
  19. package/dist/rules/sort-intersection-types.mjs +153 -113
  20. package/dist/rules/sort-jsx-props.js +28 -47
  21. package/dist/rules/sort-jsx-props.mjs +20 -36
  22. package/dist/rules/sort-maps.js +84 -22
  23. package/dist/rules/sort-maps.mjs +80 -22
  24. package/dist/rules/sort-named-exports.js +99 -31
  25. package/dist/rules/sort-named-exports.mjs +87 -28
  26. package/dist/rules/sort-named-imports.js +98 -31
  27. package/dist/rules/sort-named-imports.mjs +86 -28
  28. package/dist/rules/sort-object-types.js +90 -117
  29. package/dist/rules/sort-object-types.mjs +80 -108
  30. package/dist/rules/sort-objects.js +73 -64
  31. package/dist/rules/sort-objects.mjs +71 -56
  32. package/dist/rules/sort-sets.js +1 -0
  33. package/dist/rules/sort-sets.mjs +1 -0
  34. package/dist/rules/sort-svelte-attributes.js +29 -50
  35. package/dist/rules/sort-svelte-attributes.mjs +21 -39
  36. package/dist/rules/sort-union-types.js +163 -119
  37. package/dist/rules/sort-union-types.mjs +153 -113
  38. package/dist/rules/sort-variable-declarations.js +109 -24
  39. package/dist/rules/sort-variable-declarations.mjs +101 -25
  40. package/dist/rules/sort-vue-attributes.js +26 -46
  41. package/dist/rules/sort-vue-attributes.mjs +18 -35
  42. package/dist/utils/compare.js +12 -6
  43. package/dist/utils/compare.mjs +12 -6
  44. package/dist/utils/get-node-range.js +9 -3
  45. package/dist/utils/get-node-range.mjs +9 -3
  46. package/dist/utils/is-partition-comment.js +8 -10
  47. package/dist/utils/is-partition-comment.mjs +8 -10
  48. package/dist/utils/matches.js +15 -0
  49. package/dist/utils/matches.mjs +13 -0
  50. package/dist/utils/sort-nodes-by-dependencies.js +16 -0
  51. package/dist/utils/sort-nodes-by-dependencies.mjs +16 -1
  52. package/dist/utils/sort-nodes-by-groups.js +22 -0
  53. package/dist/utils/sort-nodes-by-groups.mjs +20 -0
  54. package/dist/utils/use-groups.js +4 -8
  55. package/dist/utils/use-groups.mjs +4 -13
  56. package/{license → license.md} +1 -1
  57. package/package.json +3 -3
package/dist/index.d.ts CHANGED
@@ -108,10 +108,14 @@ declare const _default: {
108
108
  }
109
109
  rules: {
110
110
  'sort-variable-declarations': RuleModule<
111
- 'unexpectedVariableDeclarationsOrder',
111
+ | 'unexpectedVariableDeclarationsDependencyOrder'
112
+ | 'unexpectedVariableDeclarationsOrder',
112
113
  [
113
114
  Partial<{
114
115
  type: 'alphabetical' | 'line-length' | 'natural'
116
+ partitionByComment: string[] | boolean | string
117
+ matcher: 'minimatch' | 'regex'
118
+ partitionByNewLine: boolean
115
119
  order: 'desc' | 'asc'
116
120
  ignoreCase: boolean
117
121
  }>,
@@ -125,6 +129,8 @@ declare const _default: {
125
129
  [
126
130
  Partial<{
127
131
  type: 'alphabetical' | 'line-length' | 'natural'
132
+ partitionByComment: string[] | boolean | string
133
+ matcher: 'minimatch' | 'regex'
128
134
  groups: (
129
135
  | (
130
136
  | 'object'
@@ -157,6 +163,7 @@ declare const _default: {
157
163
  | 'union'
158
164
  )
159
165
  )[]
166
+ partitionByNewLine: boolean
160
167
  order: 'desc' | 'asc'
161
168
  ignoreCase: boolean
162
169
  }>,
@@ -174,6 +181,7 @@ declare const _default: {
174
181
  }
175
182
  type: 'alphabetical' | 'line-length' | 'natural'
176
183
  groups: (string | string[])[]
184
+ matcher: 'minimatch' | 'regex'
177
185
  order: 'desc' | 'asc'
178
186
  ignoreCase: boolean
179
187
  }>,
@@ -190,6 +198,7 @@ declare const _default: {
190
198
  }
191
199
  type: 'alphabetical' | 'line-length' | 'natural'
192
200
  groups: (string | string[])[]
201
+ matcher: 'minimatch' | 'regex'
193
202
  order: 'desc' | 'asc'
194
203
  ignoreCase: boolean
195
204
  }>,
@@ -206,6 +215,7 @@ declare const _default: {
206
215
  }
207
216
  type: 'alphabetical' | 'line-length' | 'natural'
208
217
  groups: (string | string[])[]
218
+ matcher: 'minimatch' | 'regex'
209
219
  order: 'desc' | 'asc'
210
220
  ignoreCase: boolean
211
221
  }>,
@@ -225,6 +235,9 @@ declare const _default: {
225
235
  Partial<{
226
236
  groupKind: 'values-first' | 'types-first' | 'mixed'
227
237
  type: 'alphabetical' | 'line-length' | 'natural'
238
+ partitionByComment: string[] | boolean | string
239
+ matcher: 'minimatch' | 'regex'
240
+ partitionByNewLine: boolean
228
241
  order: 'desc' | 'asc'
229
242
  ignoreAlias: boolean
230
243
  ignoreCase: boolean
@@ -239,6 +252,9 @@ declare const _default: {
239
252
  Partial<{
240
253
  groupKind: 'values-first' | 'types-first' | 'mixed'
241
254
  type: 'alphabetical' | 'line-length' | 'natural'
255
+ partitionByComment: string[] | boolean | string
256
+ matcher: 'minimatch' | 'regex'
257
+ partitionByNewLine: boolean
242
258
  order: 'desc' | 'asc'
243
259
  ignoreCase: boolean
244
260
  }>,
@@ -255,7 +271,9 @@ declare const _default: {
255
271
  [x: string]: string | string[]
256
272
  }
257
273
  type: 'alphabetical' | 'line-length' | 'natural'
274
+ partitionByComment: string[] | boolean | string
258
275
  groups: (string | string[])[]
276
+ matcher: 'minimatch' | 'regex'
259
277
  partitionByNewLine: boolean
260
278
  order: 'desc' | 'asc'
261
279
  ignoreCase: boolean
@@ -269,6 +287,8 @@ declare const _default: {
269
287
  [
270
288
  Partial<{
271
289
  type: 'alphabetical' | 'line-length' | 'natural'
290
+ partitionByComment: string[] | boolean | string
291
+ matcher: 'minimatch' | 'regex'
272
292
  groups: (
273
293
  | (
274
294
  | 'object'
@@ -301,6 +321,7 @@ declare const _default: {
301
321
  | 'union'
302
322
  )
303
323
  )[]
324
+ partitionByNewLine: boolean
304
325
  order: 'desc' | 'asc'
305
326
  ignoreCase: boolean
306
327
  }>,
@@ -330,7 +351,9 @@ declare const _default: {
330
351
  [key: string]: string[] | string
331
352
  }
332
353
  type: 'alphabetical' | 'line-length' | 'natural'
354
+ partitionByComment: string[] | boolean | string
333
355
  groups: (string | string[])[]
356
+ matcher: 'minimatch' | 'regex'
334
357
  partitionByNewLine: boolean
335
358
  ignorePattern: string[]
336
359
  order: 'desc' | 'asc'
@@ -349,6 +372,7 @@ declare const _default: {
349
372
  }
350
373
  type: 'alphabetical' | 'line-length' | 'natural'
351
374
  groups: (string | string[])[]
375
+ matcher: 'minimatch' | 'regex'
352
376
  ignorePattern: string[]
353
377
  order: 'desc' | 'asc'
354
378
  ignoreCase: boolean
@@ -358,7 +382,9 @@ declare const _default: {
358
382
  RuleListener
359
383
  >
360
384
  'sort-classes': RuleModule<
361
- 'unexpectedClassesGroupOrder' | 'unexpectedClassesOrder',
385
+ | 'unexpectedClassesDependencyOrder'
386
+ | 'unexpectedClassesGroupOrder'
387
+ | 'unexpectedClassesOrder',
362
388
  SortClassesOptions,
363
389
  unknown,
364
390
  RuleListener
@@ -385,6 +411,7 @@ declare const _default: {
385
411
  type: 'alphabetical' | 'line-length' | 'natural'
386
412
  newlinesBetween: 'ignore' | 'always' | 'never'
387
413
  groups: (string | string[])[]
414
+ matcher: 'minimatch' | 'regex'
388
415
  environment: 'node' | 'bun'
389
416
  internalPattern: string[]
390
417
  sortSideEffects: boolean
@@ -400,7 +427,11 @@ declare const _default: {
400
427
  'unexpectedExportsOrder',
401
428
  [
402
429
  Partial<{
430
+ groupKind: 'values-first' | 'types-first' | 'mixed'
403
431
  type: 'alphabetical' | 'line-length' | 'natural'
432
+ partitionByComment: string[] | boolean | string
433
+ matcher: 'minimatch' | 'regex'
434
+ partitionByNewLine: boolean
404
435
  order: 'desc' | 'asc'
405
436
  ignoreCase: boolean
406
437
  }>,
@@ -409,7 +440,9 @@ declare const _default: {
409
440
  RuleListener
410
441
  >
411
442
  'sort-objects': RuleModule<
412
- 'unexpectedObjectsGroupOrder' | 'unexpectedObjectsOrder',
443
+ | 'unexpectedObjectsDependencyOrder'
444
+ | 'unexpectedObjectsGroupOrder'
445
+ | 'unexpectedObjectsOrder',
413
446
  [
414
447
  Partial<{
415
448
  customGroups: {
@@ -417,6 +450,7 @@ declare const _default: {
417
450
  }
418
451
  type: 'alphabetical' | 'line-length' | 'natural'
419
452
  partitionByComment: string[] | boolean | string
453
+ matcher: 'minimatch' | 'regex'
420
454
  groups: (string[] | string)[]
421
455
  partitionByNewLine: boolean
422
456
  styledComponents: boolean
@@ -430,7 +464,7 @@ declare const _default: {
430
464
  RuleListener
431
465
  >
432
466
  'sort-enums': RuleModule<
433
- 'unexpectedEnumsOrder',
467
+ 'unexpectedEnumsDependencyOrder' | 'unexpectedEnumsOrder',
434
468
  Options_2,
435
469
  unknown,
436
470
  RuleListener
@@ -446,6 +480,9 @@ declare const _default: {
446
480
  [
447
481
  Partial<{
448
482
  type: 'alphabetical' | 'line-length' | 'natural'
483
+ partitionByComment: string[] | boolean | string
484
+ matcher: 'minimatch' | 'regex'
485
+ partitionByNewLine: boolean
449
486
  order: 'desc' | 'asc'
450
487
  ignoreCase: boolean
451
488
  }>,
@@ -511,6 +548,9 @@ declare type Options = [
511
548
  Partial<{
512
549
  groupKind: 'literals-first' | 'spreads-first' | 'mixed'
513
550
  type: 'alphabetical' | 'line-length' | 'natural'
551
+ partitionByComment: string[] | boolean | string
552
+ matcher: 'minimatch' | 'regex'
553
+ partitionByNewLine: boolean
514
554
  order: 'desc' | 'asc'
515
555
  ignoreCase: boolean
516
556
  }>,
@@ -520,6 +560,8 @@ declare type Options_2 = [
520
560
  Partial<{
521
561
  type: 'alphabetical' | 'line-length' | 'natural'
522
562
  partitionByComment: string[] | boolean | string
563
+ matcher: 'minimatch' | 'regex'
564
+ partitionByNewLine: boolean
523
565
  forceNumericSort: boolean
524
566
  order: 'desc' | 'asc'
525
567
  sortByValue: boolean
@@ -585,6 +627,7 @@ declare type SortClassesOptions = [
585
627
  | CustomGroup[]
586
628
  type: 'alphabetical' | 'line-length' | 'natural'
587
629
  partitionByComment: string[] | boolean | string
630
+ matcher: 'minimatch' | 'regex'
588
631
  groups: (Group[] | Group)[]
589
632
  order: 'desc' | 'asc'
590
633
  ignoreCase: boolean
@@ -3,7 +3,10 @@ Object.defineProperties(exports, {
3
3
  __esModule: { value: true },
4
4
  [Symbol.toStringTag]: { value: 'Module' },
5
5
  })
6
+ const isPartitionComment = require('../utils/is-partition-comment.js')
7
+ const getCommentsBefore = require('../utils/get-comments-before.js')
6
8
  const createEslintRule = require('../utils/create-eslint-rule.js')
9
+ const getLinesBetween = require('../utils/get-lines-between.js')
7
10
  const getGroupNumber = require('../utils/get-group-number.js')
8
11
  const getSourceCode = require('../utils/get-source-code.js')
9
12
  const toSingleLine = require('../utils/to-single-line.js')
@@ -29,6 +32,11 @@ let jsonSchema = {
29
32
  type: 'string',
30
33
  enum: ['asc', 'desc'],
31
34
  },
35
+ matcher: {
36
+ description: 'Specifies the string matcher.',
37
+ type: 'string',
38
+ enum: ['minimatch', 'regex'],
39
+ },
32
40
  ignoreCase: {
33
41
  description: 'Controls whether sorting should be case-sensitive or not.',
34
42
  type: 'boolean',
@@ -38,6 +46,29 @@ let jsonSchema = {
38
46
  enum: ['mixed', 'literals-first', 'spreads-first'],
39
47
  type: 'string',
40
48
  },
49
+ partitionByComment: {
50
+ description:
51
+ 'Allows you to use comments to separate the array members into logical groups.',
52
+ anyOf: [
53
+ {
54
+ type: 'array',
55
+ items: {
56
+ type: 'string',
57
+ },
58
+ },
59
+ {
60
+ type: 'boolean',
61
+ },
62
+ {
63
+ type: 'string',
64
+ },
65
+ ],
66
+ },
67
+ partitionByNewLine: {
68
+ description:
69
+ 'Allows to use spaces to separate the nodes into logical groups.',
70
+ type: 'boolean',
71
+ },
41
72
  },
42
73
  additionalProperties: false,
43
74
  }
@@ -60,7 +91,10 @@ const sortArrayIncludes = createEslintRule.createEslintRule({
60
91
  type: 'alphabetical',
61
92
  order: 'asc',
62
93
  ignoreCase: true,
94
+ matcher: 'minimatch',
63
95
  groupKind: 'literals-first',
96
+ partitionByComment: false,
97
+ partitionByNewLine: false,
64
98
  },
65
99
  ],
66
100
  create: context => ({
@@ -87,70 +121,100 @@ let sortArray = (context, messageId, elements) => {
87
121
  groupKind: 'literals-first',
88
122
  type: 'alphabetical',
89
123
  ignoreCase: true,
124
+ matcher: 'minimatch',
90
125
  order: 'asc',
126
+ partitionByComment: false,
127
+ partitionByNewLine: false,
91
128
  })
92
129
  let sourceCode = getSourceCode.getSourceCode(context)
93
- let nodes = elements
94
- .reduce(
95
- (accumulator, element) => {
96
- if (element !== null) {
97
- let group = 'unknown'
98
- if (typeof options.groupKind === 'string') {
99
- group = element.type === 'SpreadElement' ? 'spread' : 'literal'
100
- }
101
- accumulator.at(0).push({
102
- name:
103
- element.type === 'Literal'
104
- ? `${element.value}`
105
- : sourceCode.text.slice(...element.range),
106
- size: rangeToDiff.rangeToDiff(element.range),
107
- type: element.type,
108
- node: element,
109
- group,
110
- })
130
+ let partitionComment = options.partitionByComment
131
+ let formattedMembers = elements.reduce(
132
+ (accumulator, element) => {
133
+ var _a
134
+ if (element !== null) {
135
+ let group = 'unknown'
136
+ if (typeof options.groupKind === 'string') {
137
+ group = element.type === 'SpreadElement' ? 'spread' : 'literal'
111
138
  }
112
- return accumulator
113
- },
114
- [[], []],
115
- )
116
- .flat()
117
- pairwise.pairwise(nodes, (left, right) => {
118
- let groupKindOrder = ['unknown']
119
- if (typeof options.groupKind === 'string') {
120
- groupKindOrder =
121
- options.groupKind === 'literals-first'
122
- ? ['literal', 'spread']
123
- : ['spread', 'literal']
124
- }
125
- let leftNum = getGroupNumber.getGroupNumber(groupKindOrder, left)
126
- let rightNum = getGroupNumber.getGroupNumber(groupKindOrder, right)
127
- if (
128
- (options.groupKind !== 'mixed' && leftNum > rightNum) ||
129
- ((options.groupKind === 'mixed' || leftNum === rightNum) &&
130
- isPositive.isPositive(compare.compare(left, right, options)))
131
- ) {
132
- context.report({
133
- messageId,
134
- data: {
135
- left: toSingleLine.toSingleLine(left.name),
136
- right: toSingleLine.toSingleLine(right.name),
137
- },
138
- node: right.node,
139
- fix: fixer => {
140
- let sortedNodes =
141
- options.groupKind !== 'mixed'
142
- ? groupKindOrder
143
- .map(group => nodes.filter(n => n.group === group))
144
- .map(groupedNodes =>
145
- sortNodes.sortNodes(groupedNodes, options),
146
- )
147
- .flat()
148
- : sortNodes.sortNodes(nodes, options)
149
- return makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode)
150
- },
151
- })
152
- }
153
- })
139
+ let lastSortingNode =
140
+ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
141
+ let sortingNode = {
142
+ name:
143
+ element.type === 'Literal'
144
+ ? `${element.value}`
145
+ : sourceCode.text.slice(...element.range),
146
+ size: rangeToDiff.rangeToDiff(element.range),
147
+ node: element,
148
+ group,
149
+ }
150
+ if (
151
+ (partitionComment &&
152
+ isPartitionComment.hasPartitionComment(
153
+ partitionComment,
154
+ getCommentsBefore.getCommentsBefore(element, sourceCode),
155
+ options.matcher,
156
+ )) ||
157
+ (options.partitionByNewLine &&
158
+ lastSortingNode &&
159
+ getLinesBetween.getLinesBetween(
160
+ sourceCode,
161
+ lastSortingNode,
162
+ sortingNode,
163
+ ))
164
+ ) {
165
+ accumulator.push([])
166
+ }
167
+ accumulator.at(-1).push(sortingNode)
168
+ }
169
+ return accumulator
170
+ },
171
+ [[]],
172
+ )
173
+ for (let nodes of formattedMembers) {
174
+ pairwise.pairwise(nodes, (left, right) => {
175
+ let groupKindOrder = ['unknown']
176
+ if (typeof options.groupKind === 'string') {
177
+ groupKindOrder =
178
+ options.groupKind === 'literals-first'
179
+ ? ['literal', 'spread']
180
+ : ['spread', 'literal']
181
+ }
182
+ let leftNum = getGroupNumber.getGroupNumber(groupKindOrder, left)
183
+ let rightNum = getGroupNumber.getGroupNumber(groupKindOrder, right)
184
+ if (
185
+ (options.groupKind !== 'mixed' && leftNum > rightNum) ||
186
+ ((options.groupKind === 'mixed' || leftNum === rightNum) &&
187
+ isPositive.isPositive(compare.compare(left, right, options)))
188
+ ) {
189
+ context.report({
190
+ messageId,
191
+ data: {
192
+ left: toSingleLine.toSingleLine(left.name),
193
+ right: toSingleLine.toSingleLine(right.name),
194
+ },
195
+ node: right.node,
196
+ fix: fixer => {
197
+ let sortedNodes =
198
+ options.groupKind !== 'mixed'
199
+ ? groupKindOrder
200
+ .map(group => nodes.filter(n => n.group === group))
201
+ .map(groupedNodes =>
202
+ sortNodes.sortNodes(groupedNodes, options),
203
+ )
204
+ .flat()
205
+ : sortNodes.sortNodes(nodes, options)
206
+ return makeFixes.makeFixes(
207
+ fixer,
208
+ nodes,
209
+ sortedNodes,
210
+ sourceCode,
211
+ options,
212
+ )
213
+ },
214
+ })
215
+ }
216
+ })
217
+ }
154
218
  }
155
219
  }
156
220
  exports.default = sortArrayIncludes
@@ -1,4 +1,7 @@
1
+ import { hasPartitionComment } from '../utils/is-partition-comment.mjs'
2
+ import { getCommentsBefore } from '../utils/get-comments-before.mjs'
1
3
  import { createEslintRule } from '../utils/create-eslint-rule.mjs'
4
+ import { getLinesBetween } from '../utils/get-lines-between.mjs'
2
5
  import { getGroupNumber } from '../utils/get-group-number.mjs'
3
6
  import { getSourceCode } from '../utils/get-source-code.mjs'
4
7
  import { toSingleLine } from '../utils/to-single-line.mjs'
@@ -24,6 +27,11 @@ let jsonSchema = {
24
27
  type: 'string',
25
28
  enum: ['asc', 'desc'],
26
29
  },
30
+ matcher: {
31
+ description: 'Specifies the string matcher.',
32
+ type: 'string',
33
+ enum: ['minimatch', 'regex'],
34
+ },
27
35
  ignoreCase: {
28
36
  description: 'Controls whether sorting should be case-sensitive or not.',
29
37
  type: 'boolean',
@@ -33,6 +41,29 @@ let jsonSchema = {
33
41
  enum: ['mixed', 'literals-first', 'spreads-first'],
34
42
  type: 'string',
35
43
  },
44
+ partitionByComment: {
45
+ description:
46
+ 'Allows you to use comments to separate the array members into logical groups.',
47
+ anyOf: [
48
+ {
49
+ type: 'array',
50
+ items: {
51
+ type: 'string',
52
+ },
53
+ },
54
+ {
55
+ type: 'boolean',
56
+ },
57
+ {
58
+ type: 'string',
59
+ },
60
+ ],
61
+ },
62
+ partitionByNewLine: {
63
+ description:
64
+ 'Allows to use spaces to separate the nodes into logical groups.',
65
+ type: 'boolean',
66
+ },
36
67
  },
37
68
  additionalProperties: false,
38
69
  }
@@ -55,7 +86,10 @@ const sortArrayIncludes = createEslintRule({
55
86
  type: 'alphabetical',
56
87
  order: 'asc',
57
88
  ignoreCase: true,
89
+ matcher: 'minimatch',
58
90
  groupKind: 'literals-first',
91
+ partitionByComment: false,
92
+ partitionByNewLine: false,
59
93
  },
60
94
  ],
61
95
  create: context => ({
@@ -82,68 +116,88 @@ let sortArray = (context, messageId, elements) => {
82
116
  groupKind: 'literals-first',
83
117
  type: 'alphabetical',
84
118
  ignoreCase: true,
119
+ matcher: 'minimatch',
85
120
  order: 'asc',
121
+ partitionByComment: false,
122
+ partitionByNewLine: false,
86
123
  })
87
124
  let sourceCode = getSourceCode(context)
88
- let nodes = elements
89
- .reduce(
90
- (accumulator, element) => {
91
- if (element !== null) {
92
- let group = 'unknown'
93
- if (typeof options.groupKind === 'string') {
94
- group = element.type === 'SpreadElement' ? 'spread' : 'literal'
95
- }
96
- accumulator.at(0).push({
97
- name:
98
- element.type === 'Literal'
99
- ? `${element.value}`
100
- : sourceCode.text.slice(...element.range),
101
- size: rangeToDiff(element.range),
102
- type: element.type,
103
- node: element,
104
- group,
105
- })
125
+ let partitionComment = options.partitionByComment
126
+ let formattedMembers = elements.reduce(
127
+ (accumulator, element) => {
128
+ var _a
129
+ if (element !== null) {
130
+ let group = 'unknown'
131
+ if (typeof options.groupKind === 'string') {
132
+ group = element.type === 'SpreadElement' ? 'spread' : 'literal'
106
133
  }
107
- return accumulator
108
- },
109
- [[], []],
110
- )
111
- .flat()
112
- pairwise(nodes, (left, right) => {
113
- let groupKindOrder = ['unknown']
114
- if (typeof options.groupKind === 'string') {
115
- groupKindOrder =
116
- options.groupKind === 'literals-first'
117
- ? ['literal', 'spread']
118
- : ['spread', 'literal']
119
- }
120
- let leftNum = getGroupNumber(groupKindOrder, left)
121
- let rightNum = getGroupNumber(groupKindOrder, right)
122
- if (
123
- (options.groupKind !== 'mixed' && leftNum > rightNum) ||
124
- ((options.groupKind === 'mixed' || leftNum === rightNum) &&
125
- isPositive(compare(left, right, options)))
126
- ) {
127
- context.report({
128
- messageId,
129
- data: {
130
- left: toSingleLine(left.name),
131
- right: toSingleLine(right.name),
132
- },
133
- node: right.node,
134
- fix: fixer => {
135
- let sortedNodes =
136
- options.groupKind !== 'mixed'
137
- ? groupKindOrder
138
- .map(group => nodes.filter(n => n.group === group))
139
- .map(groupedNodes => sortNodes(groupedNodes, options))
140
- .flat()
141
- : sortNodes(nodes, options)
142
- return makeFixes(fixer, nodes, sortedNodes, sourceCode)
143
- },
144
- })
145
- }
146
- })
134
+ let lastSortingNode =
135
+ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
136
+ let sortingNode = {
137
+ name:
138
+ element.type === 'Literal'
139
+ ? `${element.value}`
140
+ : sourceCode.text.slice(...element.range),
141
+ size: rangeToDiff(element.range),
142
+ node: element,
143
+ group,
144
+ }
145
+ if (
146
+ (partitionComment &&
147
+ hasPartitionComment(
148
+ partitionComment,
149
+ getCommentsBefore(element, sourceCode),
150
+ options.matcher,
151
+ )) ||
152
+ (options.partitionByNewLine &&
153
+ lastSortingNode &&
154
+ getLinesBetween(sourceCode, lastSortingNode, sortingNode))
155
+ ) {
156
+ accumulator.push([])
157
+ }
158
+ accumulator.at(-1).push(sortingNode)
159
+ }
160
+ return accumulator
161
+ },
162
+ [[]],
163
+ )
164
+ for (let nodes of formattedMembers) {
165
+ pairwise(nodes, (left, right) => {
166
+ let groupKindOrder = ['unknown']
167
+ if (typeof options.groupKind === 'string') {
168
+ groupKindOrder =
169
+ options.groupKind === 'literals-first'
170
+ ? ['literal', 'spread']
171
+ : ['spread', 'literal']
172
+ }
173
+ let leftNum = getGroupNumber(groupKindOrder, left)
174
+ let rightNum = getGroupNumber(groupKindOrder, right)
175
+ if (
176
+ (options.groupKind !== 'mixed' && leftNum > rightNum) ||
177
+ ((options.groupKind === 'mixed' || leftNum === rightNum) &&
178
+ isPositive(compare(left, right, options)))
179
+ ) {
180
+ context.report({
181
+ messageId,
182
+ data: {
183
+ left: toSingleLine(left.name),
184
+ right: toSingleLine(right.name),
185
+ },
186
+ node: right.node,
187
+ fix: fixer => {
188
+ let sortedNodes =
189
+ options.groupKind !== 'mixed'
190
+ ? groupKindOrder
191
+ .map(group => nodes.filter(n => n.group === group))
192
+ .map(groupedNodes => sortNodes(groupedNodes, options))
193
+ .flat()
194
+ : sortNodes(nodes, options)
195
+ return makeFixes(fixer, nodes, sortedNodes, sourceCode, options)
196
+ },
197
+ })
198
+ }
199
+ })
200
+ }
147
201
  }
148
202
  }
149
203
  export { sortArrayIncludes as default, jsonSchema, sortArray }