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.
@@ -8,12 +8,14 @@ const validateNewlinesAndPartitionConfiguration = require('../utils/validate-new
8
8
  const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js')
9
9
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
10
10
  const getCustomGroupsCompareOptions = require('../utils/get-custom-groups-compare-options.js')
11
+ const getMatchingContextOptions = require('../utils/get-matching-context-options.js')
11
12
  const generatePredefinedGroups = require('../utils/generate-predefined-groups.js')
12
13
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
13
14
  const sortObjectTypes_types = require('./sort-object-types.types.js')
14
15
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
15
16
  const isPartitionComment = require('../utils/is-partition-comment.js')
16
17
  const isNodeFunctionType = require('../utils/is-node-function-type.js')
18
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
17
19
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
18
20
  const getCommentsBefore = require('../utils/get-comments-before.js')
19
21
  const makeNewlinesFixes = require('../utils/make-newlines-fixes.js')
@@ -39,6 +41,7 @@ let defaultOptions = {
39
41
  partitionByNewLine: false,
40
42
  newlinesBetween: 'ignore',
41
43
  specialCharacters: 'keep',
44
+ useConfigurationIf: {},
42
45
  type: 'alphabetical',
43
46
  groupKind: 'mixed',
44
47
  ignorePattern: [],
@@ -50,46 +53,57 @@ let defaultOptions = {
50
53
  groups: [],
51
54
  }
52
55
  let jsonSchema = {
53
- properties: {
54
- ignorePattern: {
55
- description:
56
- 'Specifies names or patterns for nodes that should be ignored by rule.',
57
- items: {
56
+ items: {
57
+ properties: {
58
+ ignorePattern: {
59
+ description:
60
+ 'Specifies names or patterns for nodes that should be ignored by rule.',
61
+ items: {
62
+ type: 'string',
63
+ },
64
+ type: 'array',
65
+ },
66
+ useConfigurationIf: commonJsonSchemas.buildUseConfigurationIfJsonSchema({
67
+ additionalProperties: {
68
+ declarationMatchesPattern: {
69
+ type: 'string',
70
+ },
71
+ },
72
+ }),
73
+ partitionByComment: {
74
+ ...commonJsonSchemas.partitionByCommentJsonSchema,
75
+ description:
76
+ 'Allows you to use comments to separate members into logical groups.',
77
+ },
78
+ customGroups: {
79
+ oneOf: [
80
+ commonJsonSchemas.customGroupsJsonSchema,
81
+ commonJsonSchemas.buildCustomGroupsArrayJsonSchema({
82
+ singleCustomGroupJsonSchema:
83
+ sortObjectTypes_types.singleCustomGroupJsonSchema,
84
+ }),
85
+ ],
86
+ },
87
+ groupKind: {
88
+ enum: ['mixed', 'required-first', 'optional-first'],
89
+ description: 'Specifies top-level groups.',
58
90
  type: 'string',
59
91
  },
60
- type: 'array',
61
- },
62
- partitionByComment: {
63
- ...commonJsonSchemas.partitionByCommentJsonSchema,
64
- description:
65
- 'Allows you to use comments to separate members into logical groups.',
66
- },
67
- customGroups: {
68
- oneOf: [
69
- commonJsonSchemas.customGroupsJsonSchema,
70
- commonJsonSchemas.buildCustomGroupsArrayJsonSchema({
71
- singleCustomGroupJsonSchema:
72
- sortObjectTypes_types.singleCustomGroupJsonSchema,
73
- }),
74
- ],
92
+ type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }),
93
+ partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
94
+ specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
95
+ newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
96
+ ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
97
+ alphabet: commonJsonSchemas.alphabetJsonSchema,
98
+ locales: commonJsonSchemas.localesJsonSchema,
99
+ groups: commonJsonSchemas.groupsJsonSchema,
100
+ order: commonJsonSchemas.orderJsonSchema,
75
101
  },
76
- groupKind: {
77
- enum: ['mixed', 'required-first', 'optional-first'],
78
- description: 'Specifies top-level groups.',
79
- type: 'string',
80
- },
81
- partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
82
- specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
83
- newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
84
- ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
85
- alphabet: commonJsonSchemas.alphabetJsonSchema,
86
- locales: commonJsonSchemas.localesJsonSchema,
87
- groups: commonJsonSchemas.groupsJsonSchema,
88
- order: commonJsonSchemas.orderJsonSchema,
89
- type: commonJsonSchemas.typeJsonSchema,
102
+ additionalProperties: false,
103
+ type: 'object',
90
104
  },
91
- additionalProperties: false,
92
- type: 'object',
105
+ uniqueItems: true,
106
+ type: 'array',
93
107
  }
94
108
  const sortObjectTypes = createEslintRule.createEslintRule({
95
109
  meta: {
@@ -108,7 +122,7 @@ const sortObjectTypes = createEslintRule.createEslintRule({
108
122
  description: 'Enforce sorted object types.',
109
123
  recommended: true,
110
124
  },
111
- schema: [jsonSchema],
125
+ schema: jsonSchema,
112
126
  type: 'suggestion',
113
127
  fixable: 'code',
114
128
  },
@@ -142,11 +156,44 @@ let sortObjectTypeElements = ({
142
156
  return
143
157
  }
144
158
  let settings = getSettings.getSettings(context.settings)
145
- let options = complete.complete(
146
- context.options.at(0),
159
+ let sourceCode = getSourceCode.getSourceCode(context)
160
+ let matchedContextOptions = getMatchingContextOptions
161
+ .getMatchingContextOptions({
162
+ nodeNames: elements.map(node =>
163
+ getNodeName({ typeElement: node, sourceCode }),
164
+ ),
165
+ contextOptions: context.options,
166
+ })
167
+ .find(options2 => {
168
+ var _a
169
+ if (
170
+ !((_a = options2.useConfigurationIf) == null
171
+ ? void 0
172
+ : _a.declarationMatchesPattern)
173
+ ) {
174
+ return true
175
+ }
176
+ if (!parentNodeName) {
177
+ return false
178
+ }
179
+ return matches.matches(
180
+ parentNodeName,
181
+ options2.useConfigurationIf.declarationMatchesPattern,
182
+ )
183
+ })
184
+ let completeOptions = complete.complete(
185
+ matchedContextOptions,
147
186
  settings,
148
187
  defaultOptions,
149
188
  )
189
+ let { type } = completeOptions
190
+ if (type === 'unsorted') {
191
+ return
192
+ }
193
+ let options = {
194
+ ...completeOptions,
195
+ type,
196
+ }
150
197
  validateCustomSortConfiguration.validateCustomSortConfiguration(options)
151
198
  validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration({
152
199
  customGroups: options.customGroups,
@@ -164,58 +211,23 @@ let sortObjectTypeElements = ({
164
211
  ) {
165
212
  return
166
213
  }
167
- let sourceCode = getSourceCode.getSourceCode(context)
168
214
  let eslintDisabledLines = getEslintDisabledLines.getEslintDisabledLines({
169
215
  ruleName: context.id,
170
216
  sourceCode,
171
217
  })
172
218
  let formattedMembers = elements.reduce(
173
219
  (accumulator, typeElement) => {
174
- var _a, _b, _c, _d
175
- if (typeElement.type === 'TSCallSignatureDeclaration') {
220
+ if (
221
+ typeElement.type === 'TSCallSignatureDeclaration' ||
222
+ typeElement.type === 'TSConstructSignatureDeclaration'
223
+ ) {
176
224
  accumulator.push([])
177
225
  return accumulator
178
226
  }
179
- let name
180
- let lastSortingNode =
181
- (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
227
+ let lastGroup = accumulator.at(-1)
228
+ let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1)
182
229
  let { setCustomGroups, defineGroup, getGroup } =
183
230
  useGroups.useGroups(options)
184
- let formatName = value => value.replace(/[,;]$/u, '')
185
- if (typeElement.type === 'TSPropertySignature') {
186
- if (typeElement.key.type === 'Identifier') {
187
- ;({ name } = typeElement.key)
188
- } else if (typeElement.key.type === 'Literal') {
189
- name = `${typeElement.key.value}`
190
- } else {
191
- let end =
192
- ((_b = typeElement.typeAnnotation) == null
193
- ? void 0
194
- : _b.range.at(0)) ??
195
- typeElement.range.at(1) - (typeElement.optional ? '?'.length : 0)
196
- name = sourceCode.text.slice(typeElement.range.at(0), end)
197
- }
198
- } else if (typeElement.type === 'TSIndexSignature') {
199
- let endIndex =
200
- ((_c = typeElement.typeAnnotation) == null
201
- ? void 0
202
- : _c.range.at(0)) ?? typeElement.range.at(1)
203
- name = formatName(
204
- sourceCode.text.slice(typeElement.range.at(0), endIndex),
205
- )
206
- } else if (
207
- typeElement.type === 'TSMethodSignature' &&
208
- 'name' in typeElement.key
209
- ) {
210
- ;({ name } = typeElement.key)
211
- } else {
212
- name = formatName(
213
- sourceCode.text.slice(
214
- typeElement.range.at(0),
215
- typeElement.range.at(1),
216
- ),
217
- )
218
- }
219
231
  let selectors = []
220
232
  let modifiers = []
221
233
  if (typeElement.type === 'TSIndexSignature') {
@@ -229,8 +241,9 @@ let sortObjectTypeElements = ({
229
241
  selectors.push('multiline')
230
242
  }
231
243
  if (
232
- !selectors.includes('index-signature') &&
233
- !selectors.includes('method')
244
+ !['index-signature', 'method'].some(selector =>
245
+ selectors.includes(selector),
246
+ )
234
247
  ) {
235
248
  selectors.push('property')
236
249
  }
@@ -240,15 +253,15 @@ let sortObjectTypeElements = ({
240
253
  } else {
241
254
  modifiers.push('required')
242
255
  }
243
- for (let predefinedGroup of generatePredefinedGroups.generatePredefinedGroups(
244
- {
245
- cache: cachedGroupsByModifiersAndSelectors,
246
- selectors,
247
- modifiers,
248
- },
249
- )) {
256
+ let predefinedGroups = generatePredefinedGroups.generatePredefinedGroups({
257
+ cache: cachedGroupsByModifiersAndSelectors,
258
+ selectors,
259
+ modifiers,
260
+ })
261
+ for (let predefinedGroup of predefinedGroups) {
250
262
  defineGroup(predefinedGroup)
251
263
  }
264
+ let name = getNodeName({ typeElement, sourceCode })
252
265
  if (Array.isArray(options.customGroups)) {
253
266
  for (let customGroup of options.customGroups) {
254
267
  if (
@@ -301,9 +314,10 @@ let sortObjectTypeElements = ({
301
314
  sortingNode,
302
315
  ))
303
316
  ) {
304
- accumulator.push([])
317
+ lastGroup = []
318
+ accumulator.push(lastGroup)
305
319
  }
306
- ;(_d = accumulator.at(-1)) == null ? void 0 : _d.push(sortingNode)
320
+ lastGroup == null ? void 0 : lastGroup.push(sortingNode)
307
321
  return accumulator
308
322
  },
309
323
  [[]],
@@ -337,17 +351,18 @@ let sortObjectTypeElements = ({
337
351
  let sortedNodes = sortNodesExcludingEslintDisabled(false)
338
352
  let sortedNodesExcludingEslintDisabled =
339
353
  sortNodesExcludingEslintDisabled(true)
354
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
340
355
  pairwise.pairwise(nodes, (left, right) => {
341
356
  let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
342
357
  let rightNumber = getGroupNumber.getGroupNumber(options.groups, right)
343
- let indexOfLeft = sortedNodes.indexOf(left)
344
- let indexOfRight = sortedNodes.indexOf(right)
358
+ let leftIndex = nodeIndexMap.get(left)
359
+ let rightIndex = nodeIndexMap.get(right)
345
360
  let indexOfRightExcludingEslintDisabled =
346
361
  sortedNodesExcludingEslintDisabled.indexOf(right)
347
362
  let messageIds = []
348
363
  if (
349
- indexOfLeft > indexOfRight ||
350
- indexOfLeft >= indexOfRightExcludingEslintDisabled
364
+ leftIndex > rightIndex ||
365
+ leftIndex >= indexOfRightExcludingEslintDisabled
351
366
  ) {
352
367
  messageIds.push(
353
368
  leftNumber === rightNumber
@@ -399,6 +414,38 @@ let sortObjectTypeElements = ({
399
414
  })
400
415
  }
401
416
  }
417
+ let getNodeName = ({ typeElement, sourceCode }) => {
418
+ var _a, _b
419
+ let name
420
+ let formatName = value => value.replace(/[,;]$/u, '')
421
+ if (typeElement.type === 'TSPropertySignature') {
422
+ if (typeElement.key.type === 'Identifier') {
423
+ ;({ name } = typeElement.key)
424
+ } else if (typeElement.key.type === 'Literal') {
425
+ name = `${typeElement.key.value}`
426
+ } else {
427
+ let end =
428
+ ((_a = typeElement.typeAnnotation) == null ? void 0 : _a.range.at(0)) ??
429
+ typeElement.range.at(1) - (typeElement.optional ? '?'.length : 0)
430
+ name = sourceCode.text.slice(typeElement.range.at(0), end)
431
+ }
432
+ } else if (typeElement.type === 'TSIndexSignature') {
433
+ let endIndex =
434
+ ((_b = typeElement.typeAnnotation) == null ? void 0 : _b.range.at(0)) ??
435
+ typeElement.range.at(1)
436
+ name = formatName(sourceCode.text.slice(typeElement.range.at(0), endIndex))
437
+ } else if (
438
+ typeElement.type === 'TSMethodSignature' &&
439
+ 'name' in typeElement.key
440
+ ) {
441
+ ;({ name } = typeElement.key)
442
+ } else {
443
+ name = formatName(
444
+ sourceCode.text.slice(typeElement.range.at(0), typeElement.range.at(1)),
445
+ )
446
+ }
447
+ return name
448
+ }
402
449
  exports.default = sortObjectTypes
403
450
  exports.jsonSchema = jsonSchema
404
451
  exports.sortObjectTypeElements = sortObjectTypeElements
@@ -1,12 +1,15 @@
1
1
  'use strict'
2
+ const types = require('@typescript-eslint/types')
2
3
  const commonJsonSchemas = require('../utils/common-json-schemas.js')
3
4
  const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js')
4
5
  const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js')
5
6
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
7
+ const getFirstNodeParentWithType = require('../utils/get-first-node-parent-with-type.js')
6
8
  const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
7
9
  const getMatchingContextOptions = require('../utils/get-matching-context-options.js')
8
10
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
9
11
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
12
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
10
13
  const isPartitionComment = require('../utils/is-partition-comment.js')
11
14
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
12
15
  const getCommentsBefore = require('../utils/get-comments-before.js')
@@ -16,7 +19,6 @@ const createEslintRule = require('../utils/create-eslint-rule.js')
16
19
  const getLinesBetween = require('../utils/get-lines-between.js')
17
20
  const getGroupNumber = require('../utils/get-group-number.js')
18
21
  const getSourceCode = require('../utils/get-source-code.js')
19
- const getNodeParent = require('../utils/get-node-parent.js')
20
22
  const rangeToDiff = require('../utils/range-to-diff.js')
21
23
  const getSettings = require('../utils/get-settings.js')
22
24
  const isSortable = require('../utils/is-sortable.js')
@@ -47,24 +49,54 @@ let defaultOptions = {
47
49
  }
48
50
  const sortObjects = createEslintRule.createEslintRule({
49
51
  create: context => {
52
+ let settings = getSettings.getSettings(context.settings)
53
+ let sourceCode = getSourceCode.getSourceCode(context)
50
54
  let sortObject = nodeObject => {
51
55
  if (!isSortable.isSortable(nodeObject.properties)) {
52
56
  return
53
57
  }
54
- let settings = getSettings.getSettings(context.settings)
55
- let sourceCode = getSourceCode.getSourceCode(context)
56
- let matchedContextOptions =
57
- getMatchingContextOptions.getMatchingContextOptions({
58
+ let objectParent = getObjectParent({ node: nodeObject })
59
+ let matchedContextOptions = getMatchingContextOptions
60
+ .getMatchingContextOptions({
58
61
  nodeNames: nodeObject.properties
59
62
  .map(property => getNodeName({ sourceCode, property }))
60
63
  .filter(nodeName => nodeName !== null),
61
64
  contextOptions: context.options,
62
65
  })
63
- let options = complete.complete(
66
+ .find(options2 => {
67
+ var _a
68
+ if (
69
+ !((_a = options2.useConfigurationIf) == null
70
+ ? void 0
71
+ : _a.callingFunctionNamePattern)
72
+ ) {
73
+ return true
74
+ }
75
+ if (
76
+ (objectParent == null ? void 0 : objectParent.type) ===
77
+ 'VariableDeclarator' ||
78
+ !(objectParent == null ? void 0 : objectParent.name)
79
+ ) {
80
+ return false
81
+ }
82
+ return matches.matches(
83
+ objectParent.name,
84
+ options2.useConfigurationIf.callingFunctionNamePattern,
85
+ )
86
+ })
87
+ let completeOptions = complete.complete(
64
88
  matchedContextOptions,
65
89
  settings,
66
90
  defaultOptions,
67
91
  )
92
+ let { type } = completeOptions
93
+ if (type === 'unsorted') {
94
+ return
95
+ }
96
+ let options = {
97
+ ...completeOptions,
98
+ type,
99
+ }
68
100
  validateCustomSortConfiguration.validateCustomSortConfiguration(options)
69
101
  validateGroupsConfiguration.validateGroupsConfiguration(
70
102
  options.groups,
@@ -82,43 +114,13 @@ const sortObjects = createEslintRule.createEslintRule({
82
114
  } else if (options.destructureOnly || !options.objectDeclarations) {
83
115
  return
84
116
  }
85
- if (options.ignorePattern.length) {
86
- let variableParent = getNodeParent.getNodeParent(nodeObject, [
87
- 'VariableDeclarator',
88
- 'Property',
89
- ])
90
- let parentId =
91
- (variableParent == null ? void 0 : variableParent.type) ===
92
- 'VariableDeclarator'
93
- ? variableParent.id
94
- : variableParent == null
95
- ? void 0
96
- : variableParent.key
97
- let variableIdentifier =
98
- (parentId == null ? void 0 : parentId.type) === 'Identifier'
99
- ? parentId.name
100
- : null
101
- let checkMatch = identifier =>
102
- options.ignorePattern.some(pattern =>
103
- matches.matches(identifier, pattern),
104
- )
105
- if (
106
- typeof variableIdentifier === 'string' &&
107
- checkMatch(variableIdentifier)
108
- ) {
109
- return
110
- }
111
- let callParent = getNodeParent.getNodeParent(nodeObject, [
112
- 'CallExpression',
113
- ])
114
- let callIdentifier =
115
- (callParent == null ? void 0 : callParent.type) ===
116
- 'CallExpression' && callParent.callee.type === 'Identifier'
117
- ? callParent.callee.name
118
- : null
119
- if (callIdentifier && checkMatch(callIdentifier)) {
120
- return
121
- }
117
+ if (
118
+ (objectParent == null ? void 0 : objectParent.name) &&
119
+ options.ignorePattern.some(pattern =>
120
+ matches.matches(objectParent.name, pattern),
121
+ )
122
+ ) {
123
+ return
122
124
  }
123
125
  let isStyledCallExpression = identifier =>
124
126
  identifier.type === 'Identifier' && identifier.name === 'styled'
@@ -275,14 +277,13 @@ const sortObjects = createEslintRule.createEslintRule({
275
277
  lastProperty,
276
278
  propertySortingNode,
277
279
  )) ||
278
- (options.partitionByComment &&
279
- isPartitionComment.hasPartitionComment(
280
- options.partitionByComment,
281
- getCommentsBefore.getCommentsBefore({
282
- node: property,
283
- sourceCode,
284
- }),
285
- ))
280
+ isPartitionComment.hasPartitionComment(
281
+ options.partitionByComment,
282
+ getCommentsBefore.getCommentsBefore({
283
+ node: property,
284
+ sourceCode,
285
+ }),
286
+ )
286
287
  ) {
287
288
  accumulator.push([])
288
289
  }
@@ -313,18 +314,19 @@ const sortObjects = createEslintRule.createEslintRule({
313
314
  let sortedNodesExcludingEslintDisabled =
314
315
  sortNodesIgnoringEslintDisabledNodes(true)
315
316
  let nodes = formattedMembers.flat()
317
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
316
318
  pairwise.pairwise(nodes, (left, right) => {
317
319
  let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
318
320
  let rightNumber = getGroupNumber.getGroupNumber(options.groups, right)
319
- let indexOfLeft = sortedNodes.indexOf(left)
320
- let indexOfRight = sortedNodes.indexOf(right)
321
+ let leftIndex = nodeIndexMap.get(left)
322
+ let rightIndex = nodeIndexMap.get(right)
321
323
  let indexOfRightExcludingEslintDisabled =
322
324
  sortedNodesExcludingEslintDisabled.indexOf(right)
323
325
  let messageIds = []
324
326
  let firstUnorderedNodeDependentOnRight
325
327
  if (
326
- indexOfLeft > indexOfRight ||
327
- indexOfLeft >= indexOfRightExcludingEslintDisabled
328
+ leftIndex > rightIndex ||
329
+ leftIndex >= indexOfRightExcludingEslintDisabled
328
330
  ) {
329
331
  firstUnorderedNodeDependentOnRight =
330
332
  sortNodesByDependencies.getFirstUnorderedNodeDependentOn(
@@ -424,6 +426,14 @@ const sortObjects = createEslintRule.createEslintRule({
424
426
  },
425
427
  type: 'array',
426
428
  },
429
+ useConfigurationIf:
430
+ commonJsonSchemas.buildUseConfigurationIfJsonSchema({
431
+ additionalProperties: {
432
+ callingFunctionNamePattern: {
433
+ type: 'string',
434
+ },
435
+ },
436
+ }),
427
437
  partitionByComment: {
428
438
  ...commonJsonSchemas.partitionByCommentJsonSchema,
429
439
  description:
@@ -441,8 +451,8 @@ const sortObjects = createEslintRule.createEslintRule({
441
451
  description: 'Controls whether to sort styled components.',
442
452
  type: 'boolean',
443
453
  },
454
+ type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }),
444
455
  partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
445
- useConfigurationIf: commonJsonSchemas.useConfigurationIfJsonSchema,
446
456
  specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
447
457
  newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
448
458
  customGroups: commonJsonSchemas.customGroupsJsonSchema,
@@ -451,7 +461,6 @@ const sortObjects = createEslintRule.createEslintRule({
451
461
  locales: commonJsonSchemas.localesJsonSchema,
452
462
  groups: commonJsonSchemas.groupsJsonSchema,
453
463
  order: commonJsonSchemas.orderJsonSchema,
454
- type: commonJsonSchemas.typeJsonSchema,
455
464
  },
456
465
  additionalProperties: false,
457
466
  type: 'object',
@@ -492,4 +501,52 @@ let getNodeName = ({ sourceCode, property }) => {
492
501
  }
493
502
  return sourceCode.getText(property.key)
494
503
  }
504
+ let getObjectParent = ({ node }) => {
505
+ let variableParentName = getVariableParentName({ node })
506
+ if (variableParentName) {
507
+ return {
508
+ type: 'VariableDeclarator',
509
+ name: variableParentName,
510
+ }
511
+ }
512
+ let callParentName = getCallExpressionParentName({ node })
513
+ if (callParentName) {
514
+ return {
515
+ type: 'CallExpression',
516
+ name: callParentName,
517
+ }
518
+ }
519
+ return null
520
+ }
521
+ let getVariableParentName = ({ node }) => {
522
+ let variableParent = getFirstNodeParentWithType.getFirstNodeParentWithType({
523
+ allowedTypes: [
524
+ types.TSESTree.AST_NODE_TYPES.VariableDeclarator,
525
+ types.TSESTree.AST_NODE_TYPES.Property,
526
+ ],
527
+ node,
528
+ })
529
+ if (!variableParent) {
530
+ return null
531
+ }
532
+ let parentId
533
+ if (variableParent.type === 'VariableDeclarator') {
534
+ parentId = variableParent.id
535
+ } else if ('key' in variableParent) {
536
+ parentId = variableParent.key
537
+ } else {
538
+ return null
539
+ }
540
+ return parentId.type === 'Identifier' ? parentId.name : null
541
+ }
542
+ let getCallExpressionParentName = ({ node }) => {
543
+ let callParent = getFirstNodeParentWithType.getFirstNodeParentWithType({
544
+ allowedTypes: [types.TSESTree.AST_NODE_TYPES.CallExpression],
545
+ node,
546
+ })
547
+ if (!callParent) {
548
+ return null
549
+ }
550
+ return callParent.callee.type === 'Identifier' ? callParent.callee.name : null
551
+ }
495
552
  module.exports = sortObjects
@@ -20,20 +20,29 @@ const sortSets = createEslintRule.createEslintRule({
20
20
  node.arguments[0].type === 'ArrayExpression'
21
21
  ? node.arguments[0].elements
22
22
  : node.arguments[0].arguments
23
- sortArrayIncludes.sortArray(context, 'unexpectedSetsOrder', elements)
23
+ sortArrayIncludes.sortArray({
24
+ availableMessageIds: {
25
+ unexpectedGroupOrder: 'unexpectedSetsGroupOrder',
26
+ unexpectedOrder: 'unexpectedSetsOrder',
27
+ },
28
+ elements,
29
+ context,
30
+ })
24
31
  }
25
32
  },
26
33
  }),
27
34
  meta: {
35
+ messages: {
36
+ unexpectedSetsGroupOrder:
37
+ 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
38
+ unexpectedSetsOrder: 'Expected "{{right}}" to come before "{{left}}".',
39
+ },
28
40
  docs: {
29
41
  url: 'https://perfectionist.dev/rules/sort-sets',
30
42
  description: 'Enforce sorted sets.',
31
43
  recommended: true,
32
44
  },
33
- messages: {
34
- unexpectedSetsOrder: 'Expected "{{right}}" to come before "{{left}}".',
35
- },
36
- schema: [sortArrayIncludes.jsonSchema],
45
+ schema: sortArrayIncludes.jsonSchema,
37
46
  type: 'suggestion',
38
47
  fixable: 'code',
39
48
  },