eslint-plugin-perfectionist 4.3.0 → 4.4.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.
@@ -0,0 +1,30 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ const matches = require('../utils/matches.js')
4
+ let customGroupMatches = props => {
5
+ if ('anyOf' in props.customGroup) {
6
+ return props.customGroup.anyOf.some(subgroup =>
7
+ customGroupMatches({ ...props, customGroup: subgroup }),
8
+ )
9
+ }
10
+ if (
11
+ props.customGroup.selector &&
12
+ !props.selectors.includes(props.customGroup.selector)
13
+ ) {
14
+ return false
15
+ }
16
+ if (
17
+ 'elementNamePattern' in props.customGroup &&
18
+ props.customGroup.elementNamePattern
19
+ ) {
20
+ let matchesElementNamePattern = matches.matches(
21
+ props.elementName,
22
+ props.customGroup.elementNamePattern,
23
+ )
24
+ if (!matchesElementNamePattern) {
25
+ return false
26
+ }
27
+ }
28
+ return true
29
+ }
30
+ exports.customGroupMatches = customGroupMatches
@@ -4,58 +4,77 @@ Object.defineProperties(exports, {
4
4
  [Symbol.toStringTag]: { value: 'Module' },
5
5
  })
6
6
  const commonJsonSchemas = require('../utils/common-json-schemas.js')
7
+ const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js')
8
+ const getCustomGroupsCompareOptions = require('../utils/get-custom-groups-compare-options.js')
9
+ const getMatchingContextOptions = require('../utils/get-matching-context-options.js')
10
+ const generatePredefinedGroups = require('../utils/generate-predefined-groups.js')
7
11
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
12
+ const sortArrayIncludes_types = require('./sort-array-includes.types.js')
8
13
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
9
14
  const isPartitionComment = require('../utils/is-partition-comment.js')
15
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
16
+ const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
10
17
  const getCommentsBefore = require('../utils/get-comments-before.js')
18
+ const sortArrayIncludesUtils = require('./sort-array-includes-utils.js')
11
19
  const createEslintRule = require('../utils/create-eslint-rule.js')
12
20
  const getLinesBetween = require('../utils/get-lines-between.js')
21
+ const getGroupNumber = require('../utils/get-group-number.js')
13
22
  const getSourceCode = require('../utils/get-source-code.js')
14
23
  const toSingleLine = require('../utils/to-single-line.js')
15
24
  const rangeToDiff = require('../utils/range-to-diff.js')
16
25
  const getSettings = require('../utils/get-settings.js')
17
26
  const isSortable = require('../utils/is-sortable.js')
18
- const sortNodes = require('../utils/sort-nodes.js')
19
27
  const makeFixes = require('../utils/make-fixes.js')
28
+ const useGroups = require('../utils/use-groups.js')
20
29
  const complete = require('../utils/complete.js')
21
30
  const pairwise = require('../utils/pairwise.js')
31
+ let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map()
22
32
  let defaultOptions = {
23
33
  groupKind: 'literals-first',
24
34
  specialCharacters: 'keep',
25
35
  partitionByComment: false,
26
36
  partitionByNewLine: false,
37
+ useConfigurationIf: {},
27
38
  type: 'alphabetical',
28
39
  ignoreCase: true,
29
40
  locales: 'en-US',
41
+ customGroups: [],
30
42
  alphabet: '',
31
43
  order: 'asc',
44
+ groups: [],
32
45
  }
33
46
  let jsonSchema = {
34
- properties: {
35
- partitionByComment: {
36
- ...commonJsonSchemas.partitionByCommentJsonSchema,
37
- description:
38
- 'Allows you to use comments to separate the array members into logical groups.',
47
+ items: {
48
+ properties: {
49
+ partitionByComment: {
50
+ ...commonJsonSchemas.partitionByCommentJsonSchema,
51
+ description:
52
+ 'Allows you to use comments to separate the array members into logical groups.',
53
+ },
54
+ groupKind: {
55
+ enum: ['mixed', 'literals-first', 'spreads-first'],
56
+ description: 'Specifies top-level groups.',
57
+ type: 'string',
58
+ },
59
+ customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({
60
+ singleCustomGroupJsonSchema:
61
+ sortArrayIncludes_types.singleCustomGroupJsonSchema,
62
+ }),
63
+ useConfigurationIf: commonJsonSchemas.buildUseConfigurationIfJsonSchema(),
64
+ type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }),
65
+ partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
66
+ specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
67
+ ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
68
+ alphabet: commonJsonSchemas.alphabetJsonSchema,
69
+ locales: commonJsonSchemas.localesJsonSchema,
70
+ groups: commonJsonSchemas.groupsJsonSchema,
71
+ order: commonJsonSchemas.orderJsonSchema,
39
72
  },
40
- groupKind: {
41
- enum: ['mixed', 'literals-first', 'spreads-first'],
42
- description: 'Specifies top-level groups.',
43
- type: 'string',
44
- },
45
- partitionByNewLine: {
46
- description:
47
- 'Allows to use spaces to separate the nodes into logical groups.',
48
- type: 'boolean',
49
- },
50
- specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
51
- ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
52
- alphabet: commonJsonSchemas.alphabetJsonSchema,
53
- locales: commonJsonSchemas.localesJsonSchema,
54
- order: commonJsonSchemas.orderJsonSchema,
55
- type: commonJsonSchemas.typeJsonSchema,
73
+ additionalProperties: false,
74
+ type: 'object',
56
75
  },
57
- additionalProperties: false,
58
- type: 'object',
76
+ uniqueItems: true,
77
+ type: 'array',
59
78
  }
60
79
  const sortArrayIncludes = createEslintRule.createEslintRule({
61
80
  create: context => ({
@@ -70,38 +89,68 @@ const sortArrayIncludes = createEslintRule.createEslintRule({
70
89
  node.object.type === 'ArrayExpression'
71
90
  ? node.object.elements
72
91
  : node.object.arguments
73
- sortArray(context, 'unexpectedArrayIncludesOrder', elements)
92
+ sortArray({
93
+ availableMessageIds: {
94
+ unexpectedGroupOrder: 'unexpectedArrayIncludesGroupOrder',
95
+ unexpectedOrder: 'unexpectedArrayIncludesOrder',
96
+ },
97
+ elements,
98
+ context,
99
+ })
74
100
  }
75
101
  },
76
102
  }),
77
103
  meta: {
104
+ messages: {
105
+ unexpectedArrayIncludesGroupOrder:
106
+ 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
107
+ unexpectedArrayIncludesOrder:
108
+ 'Expected "{{right}}" to come before "{{left}}".',
109
+ },
78
110
  docs: {
79
111
  description: 'Enforce sorted arrays before include method.',
80
112
  url: 'https://perfectionist.dev/rules/sort-array-includes',
81
113
  recommended: true,
82
114
  },
83
- messages: {
84
- unexpectedArrayIncludesOrder:
85
- 'Expected "{{right}}" to come before "{{left}}".',
86
- },
87
- schema: [jsonSchema],
115
+ schema: jsonSchema,
88
116
  type: 'suggestion',
89
117
  fixable: 'code',
90
118
  },
91
119
  defaultOptions: [defaultOptions],
92
120
  name: 'sort-array-includes',
93
121
  })
94
- let sortArray = (context, messageId, elements) => {
122
+ let sortArray = ({ availableMessageIds, elements, context }) => {
95
123
  if (!isSortable.isSortable(elements)) {
96
124
  return
97
125
  }
126
+ let sourceCode = getSourceCode.getSourceCode(context)
98
127
  let settings = getSettings.getSettings(context.settings)
99
- let options = complete.complete(
100
- context.options.at(0),
128
+ let matchedContextOptions =
129
+ getMatchingContextOptions.getMatchingContextOptions({
130
+ nodeNames: elements
131
+ .filter(element => element !== null)
132
+ .map(element => getNodeName({ sourceCode, element })),
133
+ contextOptions: context.options,
134
+ })
135
+ let completeOptions = complete.complete(
136
+ matchedContextOptions[0],
101
137
  settings,
102
138
  defaultOptions,
103
139
  )
104
- let sourceCode = getSourceCode.getSourceCode(context)
140
+ let { type } = completeOptions
141
+ if (type === 'unsorted') {
142
+ return
143
+ }
144
+ let options = {
145
+ ...completeOptions,
146
+ type,
147
+ }
148
+ validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration({
149
+ customGroups: options.customGroups,
150
+ selectors: sortArrayIncludes_types.allSelectors,
151
+ groups: options.groups,
152
+ modifiers: [],
153
+ })
105
154
  let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({
106
155
  ruleName: context.id,
107
156
  sourceCode,
@@ -112,30 +161,60 @@ let sortArray = (context, messageId, elements) => {
112
161
  if (element === null) {
113
162
  return accumulator
114
163
  }
115
- let lastSortingNode =
116
- (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
164
+ let { defineGroup, getGroup } = useGroups.useGroups(options)
165
+ let groupKind
166
+ let selector
167
+ if (element.type === 'SpreadElement') {
168
+ groupKind = 'spread'
169
+ selector = 'spread'
170
+ } else {
171
+ groupKind = 'literal'
172
+ selector = 'literal'
173
+ }
174
+ let predefinedGroups = generatePredefinedGroups.generatePredefinedGroups({
175
+ cache: cachedGroupsByModifiersAndSelectors,
176
+ selectors: [selector],
177
+ modifiers: [],
178
+ })
179
+ for (let predefinedGroup of predefinedGroups) {
180
+ defineGroup(predefinedGroup)
181
+ }
182
+ let name = getNodeName({ sourceCode, element })
183
+ for (let customGroup of options.customGroups) {
184
+ if (
185
+ sortArrayIncludesUtils.customGroupMatches({
186
+ selectors: [selector],
187
+ elementName: name,
188
+ customGroup,
189
+ })
190
+ ) {
191
+ defineGroup(customGroup.groupName, true)
192
+ if (getGroup() === customGroup.groupName) {
193
+ break
194
+ }
195
+ }
196
+ }
117
197
  let sortingNode = {
118
- name:
119
- element.type === 'Literal'
120
- ? `${element.value}`
121
- : sourceCode.getText(element),
122
198
  isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled(
123
199
  element,
124
200
  eslintDisabledLines,
125
201
  ),
126
- groupKind: element.type === 'SpreadElement' ? 'spread' : 'literal',
127
202
  size: rangeToDiff.rangeToDiff(element, sourceCode),
203
+ group: getGroup(),
128
204
  node: element,
205
+ groupKind,
206
+ name,
129
207
  }
208
+ let lastSortingNode =
209
+ (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
130
210
  if (
131
- (options.partitionByComment &&
132
- isPartitionComment.hasPartitionComment(
133
- options.partitionByComment,
134
- getCommentsBefore.getCommentsBefore({
135
- node: element,
136
- sourceCode,
137
- }),
138
- )) ||
211
+ isPartitionComment.hasPartitionComment(
212
+ options.partitionByComment,
213
+ getCommentsBefore.getCommentsBefore({
214
+ node: element,
215
+ sourceCode,
216
+ }),
217
+ ) ||
139
218
  (options.partitionByNewLine &&
140
219
  lastSortingNode &&
141
220
  getLinesBetween.getLinesBetween(
@@ -168,21 +247,29 @@ let sortArray = (context, messageId, elements) => {
168
247
  )
169
248
  let sortNodesIgnoringEslintDisabledNodes = ignoreEslintDisabledNodes =>
170
249
  filteredGroupKindNodes.flatMap(groupedNodes =>
171
- sortNodes.sortNodes(groupedNodes, options, {
250
+ sortNodesByGroups.sortNodesByGroups(groupedNodes, options, {
251
+ getGroupCompareOptions: groupNumber =>
252
+ getCustomGroupsCompareOptions.getCustomGroupsCompareOptions(
253
+ options,
254
+ groupNumber,
255
+ ),
172
256
  ignoreEslintDisabledNodes,
173
257
  }),
174
258
  )
175
259
  let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false)
176
260
  let sortedNodesExcludingEslintDisabled =
177
261
  sortNodesIgnoringEslintDisabledNodes(true)
262
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
178
263
  pairwise.pairwise(nodes, (left, right) => {
179
- let indexOfLeft = sortedNodes.indexOf(left)
180
- let indexOfRight = sortedNodes.indexOf(right)
264
+ let leftIndex = nodeIndexMap.get(left)
265
+ let rightIndex = nodeIndexMap.get(right)
266
+ let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
267
+ let rightNumber = getGroupNumber.getGroupNumber(options.groups, right)
181
268
  let indexOfRightExcludingEslintDisabled =
182
269
  sortedNodesExcludingEslintDisabled.indexOf(right)
183
270
  if (
184
- indexOfLeft < indexOfRight &&
185
- indexOfLeft < indexOfRightExcludingEslintDisabled
271
+ leftIndex < rightIndex &&
272
+ leftIndex < indexOfRightExcludingEslintDisabled
186
273
  ) {
187
274
  return
188
275
  }
@@ -198,13 +285,20 @@ let sortArray = (context, messageId, elements) => {
198
285
  data: {
199
286
  right: toSingleLine.toSingleLine(right.name),
200
287
  left: toSingleLine.toSingleLine(left.name),
288
+ rightGroup: right.group,
289
+ leftGroup: left.group,
201
290
  },
291
+ messageId:
292
+ leftNumber === rightNumber
293
+ ? availableMessageIds.unexpectedOrder
294
+ : availableMessageIds.unexpectedGroupOrder,
202
295
  node: right.node,
203
- messageId,
204
296
  })
205
297
  })
206
298
  }
207
299
  }
300
+ let getNodeName = ({ sourceCode, element }) =>
301
+ element.type === 'Literal' ? `${element.value}` : sourceCode.getText(element)
208
302
  exports.default = sortArrayIncludes
209
303
  exports.defaultOptions = defaultOptions
210
304
  exports.jsonSchema = jsonSchema
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ const commonJsonSchemas = require('../utils/common-json-schemas.js')
4
+ let allSelectors = ['literal', 'spread']
5
+ let singleCustomGroupJsonSchema = {
6
+ selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors),
7
+ elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema,
8
+ }
9
+ exports.allSelectors = allSelectors
10
+ exports.singleCustomGroupJsonSchema = singleCustomGroupJsonSchema
@@ -394,14 +394,14 @@ const sortClasses = createEslintRule.createEslintRule({
394
394
  }
395
395
  selectors.push('property')
396
396
  }
397
- for (let officialGroup of generatePredefinedGroups.generatePredefinedGroups(
398
- {
397
+ let predefinedGroups =
398
+ generatePredefinedGroups.generatePredefinedGroups({
399
399
  cache: cachedGroupsByModifiersAndSelectors,
400
400
  selectors,
401
401
  modifiers,
402
- },
403
- )) {
404
- defineGroup(officialGroup)
402
+ })
403
+ for (let predefinedGroup of predefinedGroups) {
404
+ defineGroup(predefinedGroup)
405
405
  }
406
406
  for (let customGroup of options.customGroups) {
407
407
  if (
@@ -460,14 +460,13 @@ const sortClasses = createEslintRule.createEslintRule({
460
460
  lastMember,
461
461
  sortingNode,
462
462
  )) ||
463
- (options.partitionByComment &&
464
- isPartitionComment.hasPartitionComment(
465
- options.partitionByComment,
466
- getCommentsBefore.getCommentsBefore({
467
- node: member,
468
- sourceCode,
469
- }),
470
- ))
463
+ isPartitionComment.hasPartitionComment(
464
+ options.partitionByComment,
465
+ getCommentsBefore.getCommentsBefore({
466
+ node: member,
467
+ sourceCode,
468
+ }),
469
+ )
471
470
  ) {
472
471
  accumulator.push([])
473
472
  }
@@ -598,10 +597,10 @@ const sortClasses = createEslintRule.createEslintRule({
598
597
  newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
599
598
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
600
599
  alphabet: commonJsonSchemas.alphabetJsonSchema,
600
+ type: commonJsonSchemas.buildTypeJsonSchema(),
601
601
  locales: commonJsonSchemas.localesJsonSchema,
602
602
  groups: commonJsonSchemas.groupsJsonSchema,
603
603
  order: commonJsonSchemas.orderJsonSchema,
604
- type: commonJsonSchemas.typeJsonSchema,
605
604
  },
606
605
  additionalProperties: false,
607
606
  type: 'object',
@@ -5,6 +5,7 @@ const validateGroupsConfiguration = require('../utils/validate-groups-configurat
5
5
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
6
6
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
7
7
  const isPartitionComment = require('../utils/is-partition-comment.js')
8
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
8
9
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
9
10
  const getCommentsBefore = require('../utils/get-comments-before.js')
10
11
  const getNodeDecorators = require('../utils/get-node-decorators.js')
@@ -75,10 +76,10 @@ const sortDecorators = createEslintRule.createEslintRule({
75
76
  customGroups: commonJsonSchemas.customGroupsJsonSchema,
76
77
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
77
78
  alphabet: commonJsonSchemas.alphabetJsonSchema,
79
+ type: commonJsonSchemas.buildTypeJsonSchema(),
78
80
  locales: commonJsonSchemas.localesJsonSchema,
79
81
  groups: commonJsonSchemas.groupsJsonSchema,
80
82
  order: commonJsonSchemas.orderJsonSchema,
81
- type: commonJsonSchemas.typeJsonSchema,
82
83
  },
83
84
  additionalProperties: false,
84
85
  type: 'object',
@@ -177,7 +178,6 @@ let sortDecorators$1 = (context, options, decorators) => {
177
178
  let formattedMembers = decorators.reduce(
178
179
  (accumulator, decorator) => {
179
180
  if (
180
- options.partitionByComment &&
181
181
  isPartitionComment.hasPartitionComment(
182
182
  options.partitionByComment,
183
183
  getCommentsBefore.getCommentsBefore({
@@ -216,14 +216,15 @@ let sortDecorators$1 = (context, options, decorators) => {
216
216
  let sortedNodesExcludingEslintDisabled =
217
217
  sortNodesExcludingEslintDisabled(true)
218
218
  let nodes = formattedMembers.flat()
219
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
219
220
  pairwise.pairwise(nodes, (left, right) => {
220
- let indexOfLeft = sortedNodes.indexOf(left)
221
- let indexOfRight = sortedNodes.indexOf(right)
221
+ let leftIndex = nodeIndexMap.get(left)
222
+ let rightIndex = nodeIndexMap.get(right)
222
223
  let indexOfRightExcludingEslintDisabled =
223
224
  sortedNodesExcludingEslintDisabled.indexOf(right)
224
225
  if (
225
- indexOfLeft < indexOfRight &&
226
- indexOfLeft < indexOfRightExcludingEslintDisabled
226
+ leftIndex < rightIndex &&
227
+ leftIndex < indexOfRightExcludingEslintDisabled
227
228
  ) {
228
229
  return
229
230
  }
@@ -5,6 +5,7 @@ const validateCustomSortConfiguration = require('../utils/validate-custom-sort-c
5
5
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
6
6
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
7
7
  const isPartitionComment = require('../utils/is-partition-comment.js')
8
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
8
9
  const getCommentsBefore = require('../utils/get-comments-before.js')
9
10
  const createEslintRule = require('../utils/create-eslint-rule.js')
10
11
  const getLinesBetween = require('../utils/get-lines-between.js')
@@ -54,30 +55,29 @@ const sortEnums = createEslintRule.createEslintRule({
54
55
  })
55
56
  let extractDependencies = (expression, enumName) => {
56
57
  let dependencies = []
57
- let checkNode = nodeValue => {
58
+ let stack = [expression]
59
+ while (stack.length) {
60
+ let node = stack.pop()
58
61
  if (
59
- nodeValue.type === 'MemberExpression' &&
60
- nodeValue.object.type === 'Identifier' &&
61
- nodeValue.object.name === enumName &&
62
- nodeValue.property.type === 'Identifier'
62
+ node.type === 'MemberExpression' &&
63
+ node.object.type === 'Identifier' &&
64
+ node.object.name === enumName &&
65
+ node.property.type === 'Identifier'
63
66
  ) {
64
- dependencies.push(nodeValue.property.name)
65
- } else if (nodeValue.type === 'Identifier') {
66
- dependencies.push(nodeValue.name)
67
+ dependencies.push(node.property.name)
68
+ } else if (node.type === 'Identifier') {
69
+ dependencies.push(node.name)
67
70
  }
68
- if ('left' in nodeValue) {
69
- checkNode(nodeValue.left)
71
+ if ('left' in node) {
72
+ stack.push(node.left)
70
73
  }
71
- if ('right' in nodeValue) {
72
- checkNode(nodeValue.right)
74
+ if ('right' in node) {
75
+ stack.push(node.right)
73
76
  }
74
- if ('expressions' in nodeValue) {
75
- for (let currentExpression of nodeValue.expressions) {
76
- checkNode(currentExpression)
77
- }
77
+ if ('expressions' in node) {
78
+ stack.push(...node.expressions)
78
79
  }
79
80
  }
80
- checkNode(expression)
81
81
  return dependencies
82
82
  }
83
83
  let formattedMembers = members.reduce(
@@ -109,14 +109,13 @@ const sortEnums = createEslintRule.createEslintRule({
109
109
  dependencies,
110
110
  }
111
111
  if (
112
- (options.partitionByComment &&
113
- isPartitionComment.hasPartitionComment(
114
- options.partitionByComment,
115
- getCommentsBefore.getCommentsBefore({
116
- node: member,
117
- sourceCode,
118
- }),
119
- )) ||
112
+ isPartitionComment.hasPartitionComment(
113
+ options.partitionByComment,
114
+ getCommentsBefore.getCommentsBefore({
115
+ node: member,
116
+ sourceCode,
117
+ }),
118
+ ) ||
120
119
  (options.partitionByNewLine &&
121
120
  lastSortingNode &&
122
121
  getLinesBetween.getLinesBetween(
@@ -161,7 +160,10 @@ const sortEnums = createEslintRule.createEslintRule({
161
160
  return ''
162
161
  }
163
162
  : null,
164
- // If the enum is numeric, and we sort by value, always use the `natural` sort type, which will correctly sort them.
163
+ /**
164
+ * If the enum is numeric, and we sort by value, always use the
165
+ * `natural` sort type, which will correctly sort them.
166
+ */
165
167
  type:
166
168
  isNumericEnum && (options.forceNumericSort || options.sortByValue)
167
169
  ? 'natural'
@@ -186,14 +188,15 @@ const sortEnums = createEslintRule.createEslintRule({
186
188
  let sortedNodes = sortNodesIgnoringEslintDisabledNodes(false)
187
189
  let sortedNodesExcludingEslintDisabled =
188
190
  sortNodesIgnoringEslintDisabledNodes(true)
191
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
189
192
  pairwise.pairwise(sortingNodes, (left, right) => {
190
- let indexOfLeft = sortedNodes.indexOf(left)
191
- let indexOfRight = sortedNodes.indexOf(right)
193
+ let leftIndex = nodeIndexMap.get(left)
194
+ let rightIndex = nodeIndexMap.get(right)
192
195
  let indexOfRightExcludingEslintDisabled =
193
196
  sortedNodesExcludingEslintDisabled.indexOf(right)
194
197
  if (
195
- indexOfLeft < indexOfRight &&
196
- indexOfLeft < indexOfRightExcludingEslintDisabled
198
+ leftIndex < rightIndex &&
199
+ leftIndex < indexOfRightExcludingEslintDisabled
197
200
  ) {
198
201
  return
199
202
  }
@@ -249,9 +252,9 @@ const sortEnums = createEslintRule.createEslintRule({
249
252
  specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
250
253
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
251
254
  alphabet: commonJsonSchemas.alphabetJsonSchema,
255
+ type: commonJsonSchemas.buildTypeJsonSchema(),
252
256
  locales: commonJsonSchemas.localesJsonSchema,
253
257
  order: commonJsonSchemas.orderJsonSchema,
254
- type: commonJsonSchemas.typeJsonSchema,
255
258
  },
256
259
  additionalProperties: false,
257
260
  type: 'object',
@@ -3,6 +3,7 @@ const commonJsonSchemas = require('../utils/common-json-schemas.js')
3
3
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
4
4
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
5
5
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
6
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
6
7
  const isPartitionComment = require('../utils/is-partition-comment.js')
7
8
  const getCommentsBefore = require('../utils/get-comments-before.js')
8
9
  const createEslintRule = require('../utils/create-eslint-rule.js')
@@ -98,14 +99,15 @@ const sortExports = createEslintRule.createEslintRule({
98
99
  let sortedNodes = sortNodesExcludingEslintDisabled(false)
99
100
  let sortedNodesExcludingEslintDisabled =
100
101
  sortNodesExcludingEslintDisabled(true)
102
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
101
103
  pairwise.pairwise(nodes, (left, right) => {
102
- let indexOfLeft = sortedNodes.indexOf(left)
103
- let indexOfRight = sortedNodes.indexOf(right)
104
+ let leftIndex = nodeIndexMap.get(left)
105
+ let rightIndex = nodeIndexMap.get(right)
104
106
  let indexOfRightExcludingEslintDisabled =
105
107
  sortedNodesExcludingEslintDisabled.indexOf(right)
106
108
  if (
107
- indexOfLeft < indexOfRight &&
108
- indexOfLeft < indexOfRightExcludingEslintDisabled
109
+ leftIndex < rightIndex &&
110
+ leftIndex < indexOfRightExcludingEslintDisabled
109
111
  ) {
110
112
  return
111
113
  }
@@ -154,9 +156,9 @@ const sortExports = createEslintRule.createEslintRule({
154
156
  specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
155
157
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
156
158
  alphabet: commonJsonSchemas.alphabetJsonSchema,
159
+ type: commonJsonSchemas.buildTypeJsonSchema(),
157
160
  locales: commonJsonSchemas.localesJsonSchema,
158
161
  order: commonJsonSchemas.orderJsonSchema,
159
- type: commonJsonSchemas.typeJsonSchema,
160
162
  },
161
163
  additionalProperties: false,
162
164
  type: 'object',
@@ -4,6 +4,7 @@ const validateCustomSortConfiguration = require('../utils/validate-custom-sort-c
4
4
  const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
5
5
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
6
6
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
7
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
7
8
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
8
9
  const createEslintRule = require('../utils/create-eslint-rule.js')
9
10
  const getGroupNumber = require('../utils/get-group-number.js')
@@ -35,10 +36,10 @@ const sortHeritageClauses = createEslintRule.createEslintRule({
35
36
  customGroups: commonJsonSchemas.customGroupsJsonSchema,
36
37
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
37
38
  alphabet: commonJsonSchemas.alphabetJsonSchema,
39
+ type: commonJsonSchemas.buildTypeJsonSchema(),
38
40
  locales: commonJsonSchemas.localesJsonSchema,
39
41
  groups: commonJsonSchemas.groupsJsonSchema,
40
42
  order: commonJsonSchemas.orderJsonSchema,
41
- type: commonJsonSchemas.typeJsonSchema,
42
43
  },
43
44
  additionalProperties: false,
44
45
  type: 'object',
@@ -82,7 +83,7 @@ const sortHeritageClauses = createEslintRule.createEslintRule({
82
83
  name: 'sort-heritage-clauses',
83
84
  })
84
85
  let sortHeritageClauses$1 = (context, options, heritageClauses) => {
85
- if (!heritageClauses || !isSortable.isSortable(heritageClauses)) {
86
+ if (!isSortable.isSortable(heritageClauses)) {
86
87
  return
87
88
  }
88
89
  let sourceCode = getSourceCode.getSourceCode(context)
@@ -112,14 +113,15 @@ let sortHeritageClauses$1 = (context, options, heritageClauses) => {
112
113
  let sortedNodes = sortNodesExcludingEslintDisabled(false)
113
114
  let sortedNodesExcludingEslintDisabled =
114
115
  sortNodesExcludingEslintDisabled(true)
116
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
115
117
  pairwise.pairwise(nodes, (left, right) => {
116
- let indexOfLeft = sortedNodes.indexOf(left)
117
- let indexOfRight = sortedNodes.indexOf(right)
118
+ let leftIndex = nodeIndexMap.get(left)
119
+ let rightIndex = nodeIndexMap.get(right)
118
120
  let indexOfRightExcludingEslintDisabled =
119
121
  sortedNodesExcludingEslintDisabled.indexOf(right)
120
122
  if (
121
- indexOfLeft < indexOfRight &&
122
- indexOfLeft < indexOfRightExcludingEslintDisabled
123
+ leftIndex < rightIndex &&
124
+ leftIndex < indexOfRightExcludingEslintDisabled
123
125
  ) {
124
126
  return
125
127
  }