eslint-plugin-perfectionist 3.6.0 → 3.7.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 (42) hide show
  1. package/dist/index.d.ts +29 -4
  2. package/dist/rules/sort-array-includes.js +118 -60
  3. package/dist/rules/sort-array-includes.mjs +106 -58
  4. package/dist/rules/sort-astro-attributes.js +18 -44
  5. package/dist/rules/sort-astro-attributes.mjs +9 -31
  6. package/dist/rules/sort-classes.js +56 -35
  7. package/dist/rules/sort-classes.mjs +56 -35
  8. package/dist/rules/sort-enums.js +69 -39
  9. package/dist/rules/sort-enums.mjs +66 -37
  10. package/dist/rules/sort-exports.js +88 -12
  11. package/dist/rules/sort-exports.mjs +85 -12
  12. package/dist/rules/sort-interfaces.js +82 -106
  13. package/dist/rules/sort-interfaces.mjs +66 -89
  14. package/dist/rules/sort-intersection-types.js +151 -119
  15. package/dist/rules/sort-intersection-types.mjs +147 -113
  16. package/dist/rules/sort-jsx-props.js +18 -44
  17. package/dist/rules/sort-jsx-props.mjs +9 -31
  18. package/dist/rules/sort-maps.js +78 -22
  19. package/dist/rules/sort-maps.mjs +74 -22
  20. package/dist/rules/sort-named-exports.js +87 -31
  21. package/dist/rules/sort-named-exports.mjs +81 -28
  22. package/dist/rules/sort-named-imports.js +87 -31
  23. package/dist/rules/sort-named-imports.mjs +81 -28
  24. package/dist/rules/sort-object-types.js +80 -119
  25. package/dist/rules/sort-object-types.mjs +72 -104
  26. package/dist/rules/sort-objects.js +48 -50
  27. package/dist/rules/sort-objects.mjs +48 -43
  28. package/dist/rules/sort-svelte-attributes.js +18 -44
  29. package/dist/rules/sort-svelte-attributes.mjs +9 -31
  30. package/dist/rules/sort-union-types.js +151 -119
  31. package/dist/rules/sort-union-types.mjs +147 -113
  32. package/dist/rules/sort-variable-declarations.js +97 -24
  33. package/dist/rules/sort-variable-declarations.mjs +95 -25
  34. package/dist/rules/sort-vue-attributes.js +18 -45
  35. package/dist/rules/sort-vue-attributes.mjs +9 -32
  36. package/dist/utils/get-node-range.js +4 -6
  37. package/dist/utils/get-node-range.mjs +4 -8
  38. package/dist/utils/sort-nodes-by-dependencies.js +16 -0
  39. package/dist/utils/sort-nodes-by-dependencies.mjs +16 -1
  40. package/dist/utils/sort-nodes-by-groups.js +22 -0
  41. package/dist/utils/sort-nodes-by-groups.mjs +20 -0
  42. package/package.json +3 -3
package/dist/index.d.ts CHANGED
@@ -108,10 +108,13 @@ 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
+ partitionByNewLine: boolean
115
118
  order: 'desc' | 'asc'
116
119
  ignoreCase: boolean
117
120
  }>,
@@ -125,6 +128,7 @@ declare const _default: {
125
128
  [
126
129
  Partial<{
127
130
  type: 'alphabetical' | 'line-length' | 'natural'
131
+ partitionByComment: string[] | boolean | string
128
132
  groups: (
129
133
  | (
130
134
  | 'object'
@@ -157,6 +161,7 @@ declare const _default: {
157
161
  | 'union'
158
162
  )
159
163
  )[]
164
+ partitionByNewLine: boolean
160
165
  order: 'desc' | 'asc'
161
166
  ignoreCase: boolean
162
167
  }>,
@@ -225,6 +230,8 @@ declare const _default: {
225
230
  Partial<{
226
231
  groupKind: 'values-first' | 'types-first' | 'mixed'
227
232
  type: 'alphabetical' | 'line-length' | 'natural'
233
+ partitionByComment: string[] | boolean | string
234
+ partitionByNewLine: boolean
228
235
  order: 'desc' | 'asc'
229
236
  ignoreAlias: boolean
230
237
  ignoreCase: boolean
@@ -239,6 +246,8 @@ declare const _default: {
239
246
  Partial<{
240
247
  groupKind: 'values-first' | 'types-first' | 'mixed'
241
248
  type: 'alphabetical' | 'line-length' | 'natural'
249
+ partitionByComment: string[] | boolean | string
250
+ partitionByNewLine: boolean
242
251
  order: 'desc' | 'asc'
243
252
  ignoreCase: boolean
244
253
  }>,
@@ -255,6 +264,7 @@ declare const _default: {
255
264
  [x: string]: string | string[]
256
265
  }
257
266
  type: 'alphabetical' | 'line-length' | 'natural'
267
+ partitionByComment: string[] | boolean | string
258
268
  groups: (string | string[])[]
259
269
  partitionByNewLine: boolean
260
270
  order: 'desc' | 'asc'
@@ -269,6 +279,7 @@ declare const _default: {
269
279
  [
270
280
  Partial<{
271
281
  type: 'alphabetical' | 'line-length' | 'natural'
282
+ partitionByComment: string[] | boolean | string
272
283
  groups: (
273
284
  | (
274
285
  | 'object'
@@ -301,6 +312,7 @@ declare const _default: {
301
312
  | 'union'
302
313
  )
303
314
  )[]
315
+ partitionByNewLine: boolean
304
316
  order: 'desc' | 'asc'
305
317
  ignoreCase: boolean
306
318
  }>,
@@ -330,6 +342,7 @@ declare const _default: {
330
342
  [key: string]: string[] | string
331
343
  }
332
344
  type: 'alphabetical' | 'line-length' | 'natural'
345
+ partitionByComment: string[] | boolean | string
333
346
  groups: (string | string[])[]
334
347
  partitionByNewLine: boolean
335
348
  ignorePattern: string[]
@@ -358,7 +371,9 @@ declare const _default: {
358
371
  RuleListener
359
372
  >
360
373
  'sort-classes': RuleModule<
361
- 'unexpectedClassesGroupOrder' | 'unexpectedClassesOrder',
374
+ | 'unexpectedClassesDependencyOrder'
375
+ | 'unexpectedClassesGroupOrder'
376
+ | 'unexpectedClassesOrder',
362
377
  SortClassesOptions,
363
378
  unknown,
364
379
  RuleListener
@@ -400,7 +415,10 @@ declare const _default: {
400
415
  'unexpectedExportsOrder',
401
416
  [
402
417
  Partial<{
418
+ groupKind: 'values-first' | 'types-first' | 'mixed'
403
419
  type: 'alphabetical' | 'line-length' | 'natural'
420
+ partitionByComment: string[] | boolean | string
421
+ partitionByNewLine: boolean
404
422
  order: 'desc' | 'asc'
405
423
  ignoreCase: boolean
406
424
  }>,
@@ -409,7 +427,9 @@ declare const _default: {
409
427
  RuleListener
410
428
  >
411
429
  'sort-objects': RuleModule<
412
- 'unexpectedObjectsGroupOrder' | 'unexpectedObjectsOrder',
430
+ | 'unexpectedObjectsDependencyOrder'
431
+ | 'unexpectedObjectsGroupOrder'
432
+ | 'unexpectedObjectsOrder',
413
433
  [
414
434
  Partial<{
415
435
  customGroups: {
@@ -430,7 +450,7 @@ declare const _default: {
430
450
  RuleListener
431
451
  >
432
452
  'sort-enums': RuleModule<
433
- 'unexpectedEnumsOrder',
453
+ 'unexpectedEnumsDependencyOrder' | 'unexpectedEnumsOrder',
434
454
  Options_2,
435
455
  unknown,
436
456
  RuleListener
@@ -446,6 +466,8 @@ declare const _default: {
446
466
  [
447
467
  Partial<{
448
468
  type: 'alphabetical' | 'line-length' | 'natural'
469
+ partitionByComment: string[] | boolean | string
470
+ partitionByNewLine: boolean
449
471
  order: 'desc' | 'asc'
450
472
  ignoreCase: boolean
451
473
  }>,
@@ -511,6 +533,8 @@ declare type Options = [
511
533
  Partial<{
512
534
  groupKind: 'literals-first' | 'spreads-first' | 'mixed'
513
535
  type: 'alphabetical' | 'line-length' | 'natural'
536
+ partitionByComment: string[] | boolean | string
537
+ partitionByNewLine: boolean
514
538
  order: 'desc' | 'asc'
515
539
  ignoreCase: boolean
516
540
  }>,
@@ -520,6 +544,7 @@ declare type Options_2 = [
520
544
  Partial<{
521
545
  type: 'alphabetical' | 'line-length' | 'natural'
522
546
  partitionByComment: string[] | boolean | string
547
+ partitionByNewLine: boolean
523
548
  forceNumericSort: boolean
524
549
  order: 'desc' | 'asc'
525
550
  sortByValue: 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')
@@ -38,6 +41,29 @@ let jsonSchema = {
38
41
  enum: ['mixed', 'literals-first', 'spreads-first'],
39
42
  type: 'string',
40
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
+ },
41
67
  },
42
68
  additionalProperties: false,
43
69
  }
@@ -61,6 +87,8 @@ const sortArrayIncludes = createEslintRule.createEslintRule({
61
87
  order: 'asc',
62
88
  ignoreCase: true,
63
89
  groupKind: 'literals-first',
90
+ partitionByComment: false,
91
+ partitionByNewLine: false,
64
92
  },
65
93
  ],
66
94
  create: context => ({
@@ -88,69 +116,99 @@ let sortArray = (context, messageId, elements) => {
88
116
  type: 'alphabetical',
89
117
  ignoreCase: true,
90
118
  order: 'asc',
119
+ partitionByComment: false,
120
+ partitionByNewLine: false,
91
121
  })
92
122
  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
- })
123
+ let partitionComment = options.partitionByComment
124
+ let formattedMembers = elements.reduce(
125
+ (accumulator, element) => {
126
+ var _a
127
+ if (element !== null) {
128
+ let group = 'unknown'
129
+ if (typeof options.groupKind === 'string') {
130
+ group = element.type === 'SpreadElement' ? 'spread' : 'literal'
111
131
  }
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
- })
132
+ let lastSortingNode =
133
+ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
134
+ let sortingNode = {
135
+ name:
136
+ element.type === 'Literal'
137
+ ? `${element.value}`
138
+ : sourceCode.text.slice(...element.range),
139
+ size: rangeToDiff.rangeToDiff(element.range),
140
+ node: element,
141
+ group,
142
+ }
143
+ if (
144
+ (partitionComment &&
145
+ isPartitionComment.hasPartitionComment(
146
+ partitionComment,
147
+ getCommentsBefore.getCommentsBefore(element, sourceCode),
148
+ )) ||
149
+ (options.partitionByNewLine &&
150
+ lastSortingNode &&
151
+ getLinesBetween.getLinesBetween(
152
+ sourceCode,
153
+ lastSortingNode,
154
+ sortingNode,
155
+ ))
156
+ ) {
157
+ accumulator.push([])
158
+ }
159
+ accumulator.at(-1).push(sortingNode)
160
+ }
161
+ return accumulator
162
+ },
163
+ [[]],
164
+ )
165
+ for (let nodes of formattedMembers) {
166
+ pairwise.pairwise(nodes, (left, right) => {
167
+ let groupKindOrder = ['unknown']
168
+ if (typeof options.groupKind === 'string') {
169
+ groupKindOrder =
170
+ options.groupKind === 'literals-first'
171
+ ? ['literal', 'spread']
172
+ : ['spread', 'literal']
173
+ }
174
+ let leftNum = getGroupNumber.getGroupNumber(groupKindOrder, left)
175
+ let rightNum = getGroupNumber.getGroupNumber(groupKindOrder, right)
176
+ if (
177
+ (options.groupKind !== 'mixed' && leftNum > rightNum) ||
178
+ ((options.groupKind === 'mixed' || leftNum === rightNum) &&
179
+ isPositive.isPositive(compare.compare(left, right, options)))
180
+ ) {
181
+ context.report({
182
+ messageId,
183
+ data: {
184
+ left: toSingleLine.toSingleLine(left.name),
185
+ right: toSingleLine.toSingleLine(right.name),
186
+ },
187
+ node: right.node,
188
+ fix: fixer => {
189
+ let sortedNodes =
190
+ options.groupKind !== 'mixed'
191
+ ? groupKindOrder
192
+ .map(group => nodes.filter(n => n.group === group))
193
+ .map(groupedNodes =>
194
+ sortNodes.sortNodes(groupedNodes, options),
195
+ )
196
+ .flat()
197
+ : sortNodes.sortNodes(nodes, options)
198
+ return makeFixes.makeFixes(
199
+ fixer,
200
+ nodes,
201
+ sortedNodes,
202
+ sourceCode,
203
+ {
204
+ partitionComment,
205
+ },
206
+ )
207
+ },
208
+ })
209
+ }
210
+ })
211
+ }
154
212
  }
155
213
  }
156
214
  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'
@@ -33,6 +36,29 @@ let jsonSchema = {
33
36
  enum: ['mixed', 'literals-first', 'spreads-first'],
34
37
  type: 'string',
35
38
  },
39
+ partitionByComment: {
40
+ description:
41
+ 'Allows you to use comments to separate the array members into logical groups.',
42
+ anyOf: [
43
+ {
44
+ type: 'array',
45
+ items: {
46
+ type: 'string',
47
+ },
48
+ },
49
+ {
50
+ type: 'boolean',
51
+ },
52
+ {
53
+ type: 'string',
54
+ },
55
+ ],
56
+ },
57
+ partitionByNewLine: {
58
+ description:
59
+ 'Allows to use spaces to separate the nodes into logical groups.',
60
+ type: 'boolean',
61
+ },
36
62
  },
37
63
  additionalProperties: false,
38
64
  }
@@ -56,6 +82,8 @@ const sortArrayIncludes = createEslintRule({
56
82
  order: 'asc',
57
83
  ignoreCase: true,
58
84
  groupKind: 'literals-first',
85
+ partitionByComment: false,
86
+ partitionByNewLine: false,
59
87
  },
60
88
  ],
61
89
  create: context => ({
@@ -83,67 +111,87 @@ let sortArray = (context, messageId, elements) => {
83
111
  type: 'alphabetical',
84
112
  ignoreCase: true,
85
113
  order: 'asc',
114
+ partitionByComment: false,
115
+ partitionByNewLine: false,
86
116
  })
87
117
  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
- })
118
+ let partitionComment = options.partitionByComment
119
+ let formattedMembers = elements.reduce(
120
+ (accumulator, element) => {
121
+ var _a
122
+ if (element !== null) {
123
+ let group = 'unknown'
124
+ if (typeof options.groupKind === 'string') {
125
+ group = element.type === 'SpreadElement' ? 'spread' : 'literal'
106
126
  }
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
- })
127
+ let lastSortingNode =
128
+ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
129
+ let sortingNode = {
130
+ name:
131
+ element.type === 'Literal'
132
+ ? `${element.value}`
133
+ : sourceCode.text.slice(...element.range),
134
+ size: rangeToDiff(element.range),
135
+ node: element,
136
+ group,
137
+ }
138
+ if (
139
+ (partitionComment &&
140
+ hasPartitionComment(
141
+ partitionComment,
142
+ getCommentsBefore(element, sourceCode),
143
+ )) ||
144
+ (options.partitionByNewLine &&
145
+ lastSortingNode &&
146
+ getLinesBetween(sourceCode, lastSortingNode, sortingNode))
147
+ ) {
148
+ accumulator.push([])
149
+ }
150
+ accumulator.at(-1).push(sortingNode)
151
+ }
152
+ return accumulator
153
+ },
154
+ [[]],
155
+ )
156
+ for (let nodes of formattedMembers) {
157
+ pairwise(nodes, (left, right) => {
158
+ let groupKindOrder = ['unknown']
159
+ if (typeof options.groupKind === 'string') {
160
+ groupKindOrder =
161
+ options.groupKind === 'literals-first'
162
+ ? ['literal', 'spread']
163
+ : ['spread', 'literal']
164
+ }
165
+ let leftNum = getGroupNumber(groupKindOrder, left)
166
+ let rightNum = getGroupNumber(groupKindOrder, right)
167
+ if (
168
+ (options.groupKind !== 'mixed' && leftNum > rightNum) ||
169
+ ((options.groupKind === 'mixed' || leftNum === rightNum) &&
170
+ isPositive(compare(left, right, options)))
171
+ ) {
172
+ context.report({
173
+ messageId,
174
+ data: {
175
+ left: toSingleLine(left.name),
176
+ right: toSingleLine(right.name),
177
+ },
178
+ node: right.node,
179
+ fix: fixer => {
180
+ let sortedNodes =
181
+ options.groupKind !== 'mixed'
182
+ ? groupKindOrder
183
+ .map(group => nodes.filter(n => n.group === group))
184
+ .map(groupedNodes => sortNodes(groupedNodes, options))
185
+ .flat()
186
+ : sortNodes(nodes, options)
187
+ return makeFixes(fixer, nodes, sortedNodes, sourceCode, {
188
+ partitionComment,
189
+ })
190
+ },
191
+ })
192
+ }
193
+ })
194
+ }
147
195
  }
148
196
  }
149
197
  export { sortArrayIncludes as default, jsonSchema, sortArray }
@@ -1,18 +1,16 @@
1
1
  'use strict'
2
2
  const path = require('node:path')
3
3
  const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
4
+ const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
4
5
  const createEslintRule = require('../utils/create-eslint-rule.js')
5
6
  const getGroupNumber = require('../utils/get-group-number.js')
6
7
  const getSourceCode = require('../utils/get-source-code.js')
7
8
  const rangeToDiff = require('../utils/range-to-diff.js')
8
9
  const getSettings = require('../utils/get-settings.js')
9
- const isPositive = require('../utils/is-positive.js')
10
10
  const useGroups = require('../utils/use-groups.js')
11
11
  const makeFixes = require('../utils/make-fixes.js')
12
- const sortNodes = require('../utils/sort-nodes.js')
13
12
  const pairwise = require('../utils/pairwise.js')
14
13
  const complete = require('../utils/complete.js')
15
- const compare = require('../utils/compare.js')
16
14
  const sortAstroAttributes = createEslintRule.createEslintRule({
17
15
  name: 'sort-astro-attributes',
18
16
  meta: {
@@ -152,17 +150,22 @@ const sortAstroAttributes = createEslintRule.createEslintRule({
152
150
  [[]],
153
151
  )
154
152
  for (let nodes of parts) {
153
+ let sortedNodes = sortNodesByGroups.sortNodesByGroups(
154
+ nodes,
155
+ options,
156
+ )
155
157
  pairwise.pairwise(nodes, (left, right) => {
156
- let leftNum = getGroupNumber.getGroupNumber(options.groups, left)
157
- let rightNum = getGroupNumber.getGroupNumber(
158
- options.groups,
159
- right,
160
- )
161
- if (
162
- leftNum > rightNum ||
163
- (leftNum === rightNum &&
164
- isPositive.isPositive(compare.compare(left, right, options)))
165
- ) {
158
+ let indexOfLeft = sortedNodes.indexOf(left)
159
+ let indexOfRight = sortedNodes.indexOf(right)
160
+ if (indexOfLeft > indexOfRight) {
161
+ let leftNum = getGroupNumber.getGroupNumber(
162
+ options.groups,
163
+ left,
164
+ )
165
+ let rightNum = getGroupNumber.getGroupNumber(
166
+ options.groups,
167
+ right,
168
+ )
166
169
  context.report({
167
170
  messageId:
168
171
  leftNum !== rightNum
@@ -175,37 +178,8 @@ const sortAstroAttributes = createEslintRule.createEslintRule({
175
178
  rightGroup: right.group,
176
179
  },
177
180
  node: right.node,
178
- fix: fixer => {
179
- let grouped = {}
180
- for (let currentNode of nodes) {
181
- let groupNum = getGroupNumber.getGroupNumber(
182
- options.groups,
183
- currentNode,
184
- )
185
- if (!(groupNum in grouped)) {
186
- grouped[groupNum] = [currentNode]
187
- } else {
188
- grouped[groupNum] = sortNodes.sortNodes(
189
- [...grouped[groupNum], currentNode],
190
- options,
191
- )
192
- }
193
- }
194
- let sortedNodes = []
195
- for (let group of Object.keys(grouped).sort(
196
- (a, b) => Number(a) - Number(b),
197
- )) {
198
- sortedNodes.push(
199
- ...sortNodes.sortNodes(grouped[group], options),
200
- )
201
- }
202
- return makeFixes.makeFixes(
203
- fixer,
204
- nodes,
205
- sortedNodes,
206
- sourceCode,
207
- )
208
- },
181
+ fix: fixer =>
182
+ makeFixes.makeFixes(fixer, nodes, sortedNodes, sourceCode),
209
183
  })
210
184
  }
211
185
  })