eslint-plugin-perfectionist 4.7.0 → 4.9.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 (51) hide show
  1. package/dist/rules/sort-array-includes/types.js +1 -1
  2. package/dist/rules/sort-array-includes.js +8 -19
  3. package/dist/rules/sort-classes/types.js +3 -6
  4. package/dist/rules/sort-classes.js +103 -104
  5. package/dist/rules/sort-decorators.js +62 -60
  6. package/dist/rules/sort-enums/does-custom-group-match.js +36 -0
  7. package/dist/rules/sort-enums/types.js +8 -0
  8. package/dist/rules/sort-enums.js +68 -15
  9. package/dist/rules/sort-exports.js +3 -2
  10. package/dist/rules/sort-heritage-clauses.js +11 -12
  11. package/dist/rules/sort-imports.js +14 -21
  12. package/dist/rules/sort-interfaces.js +18 -20
  13. package/dist/rules/sort-intersection-types.js +6 -8
  14. package/dist/rules/sort-jsx-props.js +65 -39
  15. package/dist/rules/sort-maps/types.js +1 -1
  16. package/dist/rules/sort-maps.js +10 -17
  17. package/dist/rules/sort-modules/types.js +2 -5
  18. package/dist/rules/sort-modules.js +17 -24
  19. package/dist/rules/sort-named-exports.js +20 -8
  20. package/dist/rules/sort-named-imports.js +3 -3
  21. package/dist/rules/sort-object-types/types.js +1 -1
  22. package/dist/rules/sort-object-types.js +28 -50
  23. package/dist/rules/sort-objects/types.js +2 -2
  24. package/dist/rules/sort-objects.js +19 -39
  25. package/dist/rules/sort-sets.js +5 -7
  26. package/dist/rules/sort-switch-case.js +8 -9
  27. package/dist/rules/sort-union-types.js +23 -26
  28. package/dist/rules/sort-variable-declarations.js +7 -7
  29. package/dist/utils/common-json-schemas.js +48 -32
  30. package/dist/utils/compare.js +16 -4
  31. package/dist/utils/get-comment-after.js +1 -1
  32. package/dist/utils/get-custom-groups-compare-options.js +2 -11
  33. package/dist/utils/get-decorator-name.js +10 -0
  34. package/dist/utils/get-newlines-between-option.js +20 -4
  35. package/dist/utils/get-newlines-errors.js +9 -0
  36. package/dist/utils/is-newlines-between-option.js +5 -0
  37. package/dist/utils/is-partition-comment.js +14 -10
  38. package/dist/utils/make-fixes.js +2 -0
  39. package/dist/utils/make-newlines-fixes.js +9 -0
  40. package/dist/utils/matches.js +9 -1
  41. package/dist/utils/report-all-errors.js +6 -3
  42. package/dist/utils/report-errors.js +29 -10
  43. package/dist/utils/sort-nodes-by-dependencies.js +1 -3
  44. package/dist/utils/validate-generated-groups-configuration.js +11 -8
  45. package/dist/utils/validate-groups-configuration.js +20 -39
  46. package/dist/utils/validate-newlines-and-partition-configuration.js +3 -2
  47. package/dist/utils/validate-newlines-between-inside-groups.js +18 -0
  48. package/dist/utils/validate-no-duplicated-groups.js +22 -0
  49. package/package.json +3 -3
  50. package/readme.md +3 -3
  51. package/dist/rules/sort-decorators/get-decorator-name.js +0 -23
@@ -4,7 +4,7 @@ const commonJsonSchemas = require('../../utils/common-json-schemas.js')
4
4
  let allSelectors = ['literal', 'spread']
5
5
  let singleCustomGroupJsonSchema = {
6
6
  selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors),
7
- elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema,
7
+ elementNamePattern: commonJsonSchemas.regexJsonSchema,
8
8
  }
9
9
  exports.allSelectors = allSelectors
10
10
  exports.singleCustomGroupJsonSchema = singleCustomGroupJsonSchema
@@ -4,6 +4,7 @@ Object.defineProperties(exports, {
4
4
  [Symbol.toStringTag]: { value: 'Module' },
5
5
  })
6
6
  const commonJsonSchemas = require('../utils/common-json-schemas.js')
7
+ const reportErrors = require('../utils/report-errors.js')
7
8
  const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js')
8
9
  const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js')
9
10
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
@@ -26,6 +27,7 @@ const useGroups = require('../utils/use-groups.js')
26
27
  const complete = require('../utils/complete.js')
27
28
  let cachedGroupsByModifiersAndSelectors = /* @__PURE__ */ new Map()
28
29
  let defaultOptions = {
30
+ fallbackSort: { type: 'unsorted' },
29
31
  groupKind: 'literals-first',
30
32
  specialCharacters: 'keep',
31
33
  partitionByComment: false,
@@ -53,7 +55,6 @@ let jsonSchema = {
53
55
  singleCustomGroupJsonSchema: types.singleCustomGroupJsonSchema,
54
56
  }),
55
57
  useConfigurationIf: commonJsonSchemas.buildUseConfigurationIfJsonSchema(),
56
- type: commonJsonSchemas.buildTypeJsonSchema({ withUnsorted: true }),
57
58
  partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema,
58
59
  partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
59
60
  newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
@@ -95,14 +96,11 @@ const sortArrayIncludes = createEslintRule.createEslintRule({
95
96
  }),
96
97
  meta: {
97
98
  messages: {
98
- unexpectedArrayIncludesGroupOrder:
99
- 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
100
99
  missedSpacingBetweenArrayIncludesMembers:
101
- 'Missed spacing between "{{left}}" and "{{right}}" members.',
102
- extraSpacingBetweenArrayIncludesMembers:
103
- 'Extra spacing between "{{left}}" and "{{right}}" members.',
104
- unexpectedArrayIncludesOrder:
105
- 'Expected "{{right}}" to come before "{{left}}".',
100
+ reportErrors.MISSED_SPACING_ERROR,
101
+ extraSpacingBetweenArrayIncludesMembers: reportErrors.EXTRA_SPACING_ERROR,
102
+ unexpectedArrayIncludesGroupOrder: reportErrors.GROUP_ORDER_ERROR,
103
+ unexpectedArrayIncludesOrder: reportErrors.ORDER_ERROR,
106
104
  },
107
105
  docs: {
108
106
  description: 'Enforce sorted arrays before include method.',
@@ -129,25 +127,16 @@ let sortArray = ({ availableMessageIds, elements, context }) => {
129
127
  .map(element => getNodeName({ sourceCode, element })),
130
128
  contextOptions: context.options,
131
129
  })
132
- let completeOptions = complete.complete(
130
+ let options = complete.complete(
133
131
  matchedContextOptions[0],
134
132
  settings,
135
133
  defaultOptions,
136
134
  )
137
- let { type } = completeOptions
138
- if (type === 'unsorted') {
139
- return
140
- }
141
- let options = {
142
- ...completeOptions,
143
- type,
144
- }
145
135
  validateCustomSortConfiguration.validateCustomSortConfiguration(options)
146
136
  validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration({
147
- customGroups: options.customGroups,
148
137
  selectors: types.allSelectors,
149
- groups: options.groups,
150
138
  modifiers: [],
139
+ options,
151
140
  })
152
141
  validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration(
153
142
  options,
@@ -26,15 +26,12 @@ let allModifiers = [
26
26
  'optional',
27
27
  ]
28
28
  let singleCustomGroupJsonSchema = {
29
- decoratorNamePattern: {
30
- description: 'Decorator name pattern filter.',
31
- type: 'string',
32
- },
33
29
  modifiers:
34
30
  commonJsonSchemas.buildCustomGroupModifiersJsonSchema(allModifiers),
35
31
  selector: commonJsonSchemas.buildCustomGroupSelectorJsonSchema(allSelectors),
36
- elementValuePattern: commonJsonSchemas.elementValuePatternJsonSchema,
37
- elementNamePattern: commonJsonSchemas.elementNamePatternJsonSchema,
32
+ decoratorNamePattern: commonJsonSchemas.regexJsonSchema,
33
+ elementValuePattern: commonJsonSchemas.regexJsonSchema,
34
+ elementNamePattern: commonJsonSchemas.regexJsonSchema,
38
35
  }
39
36
  exports.allModifiers = allModifiers
40
37
  exports.allSelectors = allSelectors
@@ -1,5 +1,6 @@
1
1
  'use strict'
2
2
  const commonJsonSchemas = require('../utils/common-json-schemas.js')
3
+ const reportErrors = require('../utils/report-errors.js')
3
4
  const validateNewlinesAndPartitionConfiguration = require('../utils/validate-newlines-and-partition-configuration.js')
4
5
  const validateGeneratedGroupsConfiguration = require('../utils/validate-generated-groups-configuration.js')
5
6
  const types = require('./sort-classes/types.js')
@@ -12,7 +13,9 @@ const doesCustomGroupMatch = require('./sort-classes/does-custom-group-match.js'
12
13
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
13
14
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
14
15
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
16
+ const getNodeDecorators = require('../utils/get-node-decorators.js')
15
17
  const createEslintRule = require('../utils/create-eslint-rule.js')
18
+ const getDecoratorName = require('../utils/get-decorator-name.js')
16
19
  const reportAllErrors = require('../utils/report-all-errors.js')
17
20
  const shouldPartition = require('../utils/should-partition.js')
18
21
  const getGroupNumber = require('../utils/get-group-number.js')
@@ -50,6 +53,7 @@ let defaultOptions = {
50
53
  'unknown',
51
54
  ],
52
55
  ignoreCallbackDependenciesPatterns: [],
56
+ fallbackSort: { type: 'unsorted' },
53
57
  partitionByComment: false,
54
58
  partitionByNewLine: false,
55
59
  newlinesBetween: 'ignore',
@@ -77,10 +81,9 @@ const sortClasses = createEslintRule.createEslintRule({
77
81
  validateCustomSortConfiguration.validateCustomSortConfiguration(options)
78
82
  validateGeneratedGroupsConfiguration.validateGeneratedGroupsConfiguration(
79
83
  {
80
- customGroups: options.customGroups,
81
84
  modifiers: types.allModifiers,
82
85
  selectors: types.allSelectors,
83
- groups: options.groups,
86
+ options,
84
87
  },
85
88
  )
86
89
  validateNewlinesAndPartitionConfiguration.validateNewlinesAndPartitionConfiguration(
@@ -185,8 +188,9 @@ const sortClasses = createEslintRule.createEslintRule({
185
188
  'name' in nodeValue.callee ? nodeValue.callee.name : null
186
189
  shouldIgnore =
187
190
  functionName !== null &&
188
- options.ignoreCallbackDependenciesPatterns.some(pattern =>
189
- matches.matches(functionName, pattern),
191
+ matches.matches(
192
+ functionName,
193
+ options.ignoreCallbackDependenciesPatterns,
190
194
  )
191
195
  }
192
196
  if (!shouldIgnore) {
@@ -227,7 +231,7 @@ const sortClasses = createEslintRule.createEslintRule({
227
231
  getOverloadSignatureGroups.getOverloadSignatureGroups(node.body)
228
232
  let formattedNodes = node.body.reduce(
229
233
  (accumulator, member) => {
230
- var _a2, _b, _c
234
+ var _a2, _b, _c, _d, _e
231
235
  let name
232
236
  let dependencies = []
233
237
  let { defineGroup, getGroup } = useGroups.useGroups(options)
@@ -250,25 +254,73 @@ const sortClasses = createEslintRule.createEslintRule({
250
254
  let decorated = false
251
255
  let decorators = []
252
256
  if ('decorators' in member) {
253
- decorated = member.decorators.length > 0
254
- for (let decorator of member.decorators) {
255
- if (decorator.expression.type === 'Identifier') {
256
- decorators.push(decorator.expression.name)
257
- } else if (
258
- decorator.expression.type === 'CallExpression' &&
259
- decorator.expression.callee.type === 'Identifier'
260
- ) {
261
- decorators.push(decorator.expression.callee.name)
262
- }
263
- }
257
+ decorators = getNodeDecorators
258
+ .getNodeDecorators(member)
259
+ .map(decorator =>
260
+ getDecoratorName.getDecoratorName({ sourceCode, decorator }),
261
+ )
262
+ decorated = decorators.length > 0
264
263
  }
265
264
  let memberValue
266
265
  let modifiers = []
267
266
  let selectors = []
268
267
  let addSafetySemicolonWhenInline = true
269
268
  switch (member.type) {
269
+ case 'TSAbstractPropertyDefinition':
270
+ case 'PropertyDefinition':
271
+ if ('static' in member && member.static) {
272
+ modifiers.push('static')
273
+ }
274
+ if ('declare' in member && member.declare) {
275
+ modifiers.push('declare')
276
+ }
277
+ if (member.type === 'TSAbstractPropertyDefinition') {
278
+ modifiers.push('abstract')
279
+ }
280
+ if (decorated) {
281
+ modifiers.push('decorated')
282
+ }
283
+ if ('override' in member && member.override) {
284
+ modifiers.push('override')
285
+ }
286
+ if ('readonly' in member && member.readonly) {
287
+ modifiers.push('readonly')
288
+ }
289
+ if (
290
+ 'accessibility' in member &&
291
+ member.accessibility === 'protected'
292
+ ) {
293
+ modifiers.push('protected')
294
+ } else if (
295
+ ('accessibility' in member &&
296
+ member.accessibility === 'private') ||
297
+ isPrivateHash
298
+ ) {
299
+ modifiers.push('private')
300
+ } else {
301
+ modifiers.push('public')
302
+ }
303
+ if ('optional' in member && member.optional) {
304
+ modifiers.push('optional')
305
+ }
306
+ if (
307
+ ((_b = member.value) == null ? void 0 : _b.type) ===
308
+ 'ArrowFunctionExpression' ||
309
+ ((_c = member.value) == null ? void 0 : _c.type) ===
310
+ 'FunctionExpression'
311
+ ) {
312
+ if (member.value.async) {
313
+ modifiers.push('async')
314
+ }
315
+ selectors.push('function-property')
316
+ } else if (member.value) {
317
+ memberValue = sourceCode.getText(member.value)
318
+ dependencies = extractDependencies(member.value, member.static)
319
+ }
320
+ selectors.push('property')
321
+ break
270
322
  case 'TSAbstractMethodDefinition':
271
- case 'MethodDefinition': {
323
+ case 'MethodDefinition':
272
324
  if (member.static) {
273
325
  modifiers.push('static')
274
326
  }
@@ -307,9 +359,8 @@ const sortClasses = createEslintRule.createEslintRule({
307
359
  }
308
360
  selectors.push('method')
309
361
  break
310
- }
311
362
  case 'TSAbstractAccessorProperty':
312
- case 'AccessorProperty': {
363
+ case 'AccessorProperty':
313
364
  if (member.static) {
314
365
  modifiers.push('static')
315
366
  }
@@ -331,8 +382,7 @@ const sortClasses = createEslintRule.createEslintRule({
331
382
  }
332
383
  selectors.push('accessor-property')
333
384
  break
334
- }
335
- case 'TSIndexSignature': {
385
+ case 'TSIndexSignature':
336
386
  if (member.static) {
337
387
  modifiers.push('static')
338
388
  }
@@ -341,70 +391,11 @@ const sortClasses = createEslintRule.createEslintRule({
341
391
  }
342
392
  selectors.push('index-signature')
343
393
  break
344
- }
345
- case 'StaticBlock': {
394
+ case 'StaticBlock':
346
395
  addSafetySemicolonWhenInline = false
347
396
  selectors.push('static-block')
348
397
  dependencies = extractDependencies(member, true)
349
398
  break
350
- }
351
- default: {
352
- if ('static' in member && member.static) {
353
- modifiers.push('static')
354
- }
355
- if ('declare' in member && member.declare) {
356
- modifiers.push('declare')
357
- }
358
- if (member.type === 'TSAbstractPropertyDefinition') {
359
- modifiers.push('abstract')
360
- }
361
- if (decorated) {
362
- modifiers.push('decorated')
363
- }
364
- if ('override' in member && member.override) {
365
- modifiers.push('override')
366
- }
367
- if ('readonly' in member && member.readonly) {
368
- modifiers.push('readonly')
369
- }
370
- if (
371
- 'accessibility' in member &&
372
- member.accessibility === 'protected'
373
- ) {
374
- modifiers.push('protected')
375
- } else if (
376
- ('accessibility' in member &&
377
- member.accessibility === 'private') ||
378
- isPrivateHash
379
- ) {
380
- modifiers.push('private')
381
- } else {
382
- modifiers.push('public')
383
- }
384
- if ('optional' in member && member.optional) {
385
- modifiers.push('optional')
386
- }
387
- if ('value' in member && member.value) {
388
- if (
389
- member.value.type === 'ArrowFunctionExpression' ||
390
- member.value.type === 'FunctionExpression'
391
- ) {
392
- if (member.value.async) {
393
- modifiers.push('async')
394
- }
395
- selectors.push('function-property')
396
- } else {
397
- memberValue = sourceCode.getText(member.value)
398
- }
399
- if (member.value.type !== 'TSEmptyBodyFunctionExpression') {
400
- dependencies = extractDependencies(
401
- member.value,
402
- member.static,
403
- )
404
- }
405
- }
406
- selectors.push('property')
407
- }
408
399
  }
409
400
  let predefinedGroups =
410
401
  generatePredefinedGroups.generatePredefinedGroups({
@@ -432,12 +423,15 @@ const sortClasses = createEslintRule.createEslintRule({
432
423
  }
433
424
  }
434
425
  }
435
- let overloadSignatureGroupMember =
436
- (_b = overloadSignatureGroups.find(overloadSignatures =>
426
+ let overloadSignatureGroupMemberIndex =
427
+ overloadSignatureGroups.findIndex(overloadSignatures =>
437
428
  overloadSignatures.includes(member),
438
- )) == null
429
+ )
430
+ let overloadSignatureGroupMember =
431
+ (_d = overloadSignatureGroups[overloadSignatureGroupMemberIndex]) ==
432
+ null
439
433
  ? void 0
440
- : _b.at(-1)
434
+ : _d.at(-1)
441
435
  let sortingNode = {
442
436
  dependencyName: getDependencyName({
443
437
  nodeNameWithoutStartingHash: name.startsWith('#')
@@ -446,6 +440,10 @@ const sortClasses = createEslintRule.createEslintRule({
446
440
  isStatic: modifiers.includes('static'),
447
441
  isPrivateHash,
448
442
  }),
443
+ overloadSignaturesGroupId:
444
+ overloadSignatureGroupMemberIndex === -1
445
+ ? null
446
+ : overloadSignatureGroupMemberIndex,
449
447
  size: overloadSignatureGroupMember
450
448
  ? rangeToDiff.rangeToDiff(
451
449
  overloadSignatureGroupMember,
@@ -463,7 +461,7 @@ const sortClasses = createEslintRule.createEslintRule({
463
461
  name,
464
462
  }
465
463
  let lastSortingNode =
466
- (_c = accumulator.at(-1)) == null ? void 0 : _c.at(-1)
464
+ (_e = accumulator.at(-1)) == null ? void 0 : _e.at(-1)
467
465
  if (
468
466
  shouldPartition.shouldPartition({
469
467
  lastSortingNode,
@@ -500,6 +498,19 @@ const sortClasses = createEslintRule.createEslintRule({
500
498
  )
501
499
  let nodes = formattedNodes.flat()
502
500
  reportAllErrors.reportAllErrors({
501
+ newlinesBetweenValueGetter: ({
502
+ computedNewlinesBetween,
503
+ right,
504
+ left,
505
+ }) => {
506
+ if (
507
+ left.overloadSignaturesGroupId !== null &&
508
+ left.overloadSignaturesGroupId === right.overloadSignaturesGroupId
509
+ ) {
510
+ return 'never'
511
+ }
512
+ return computedNewlinesBetween
513
+ },
503
514
  availableMessageIds: {
504
515
  missedSpacingBetweenMembers: 'missedSpacingBetweenClassMembers',
505
516
  extraSpacingBetweenMembers: 'extraSpacingBetweenClassMembers',
@@ -520,21 +531,13 @@ const sortClasses = createEslintRule.createEslintRule({
520
531
  {
521
532
  properties: {
522
533
  ...commonJsonSchemas.commonJsonSchemas,
523
- ignoreCallbackDependenciesPatterns: {
524
- description:
525
- 'Patterns that should be ignored when detecting dependencies in method callbacks.',
526
- items: {
527
- type: 'string',
528
- },
529
- type: 'array',
530
- },
531
534
  customGroups: commonJsonSchemas.buildCustomGroupsArrayJsonSchema({
532
535
  singleCustomGroupJsonSchema: types.singleCustomGroupJsonSchema,
533
536
  }),
537
+ ignoreCallbackDependenciesPatterns: commonJsonSchemas.regexJsonSchema,
534
538
  partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema,
535
539
  partitionByNewLine: commonJsonSchemas.partitionByNewLineJsonSchema,
536
540
  newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
537
- type: commonJsonSchemas.buildTypeJsonSchema(),
538
541
  groups: commonJsonSchemas.groupsJsonSchema,
539
542
  },
540
543
  additionalProperties: false,
@@ -542,15 +545,11 @@ const sortClasses = createEslintRule.createEslintRule({
542
545
  },
543
546
  ],
544
547
  messages: {
545
- unexpectedClassesGroupOrder:
546
- 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
547
- unexpectedClassesDependencyOrder:
548
- 'Expected dependency "{{right}}" to come before "{{nodeDependentOnRight}}".',
549
- missedSpacingBetweenClassMembers:
550
- 'Missed spacing between "{{left}}" and "{{right}}" objects.',
551
- extraSpacingBetweenClassMembers:
552
- 'Extra spacing between "{{left}}" and "{{right}}" objects.',
553
- unexpectedClassesOrder: 'Expected "{{right}}" to come before "{{left}}".',
548
+ unexpectedClassesDependencyOrder: reportErrors.DEPENDENCY_ORDER_ERROR,
549
+ missedSpacingBetweenClassMembers: reportErrors.MISSED_SPACING_ERROR,
550
+ extraSpacingBetweenClassMembers: reportErrors.EXTRA_SPACING_ERROR,
551
+ unexpectedClassesGroupOrder: reportErrors.GROUP_ORDER_ERROR,
552
+ unexpectedClassesOrder: reportErrors.ORDER_ERROR,
554
553
  },
555
554
  docs: {
556
555
  url: 'https://perfectionist.dev/rules/sort-classes',
@@ -4,9 +4,10 @@ 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 getDecoratorName = require('./sort-decorators/get-decorator-name.js')
7
+ const reportErrors = require('../utils/report-errors.js')
8
8
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
9
9
  const getNodeDecorators = require('../utils/get-node-decorators.js')
10
+ const getDecoratorName = require('../utils/get-decorator-name.js')
10
11
  const createEslintRule = require('../utils/create-eslint-rule.js')
11
12
  const reportAllErrors = require('../utils/report-all-errors.js')
12
13
  const shouldPartition = require('../utils/should-partition.js')
@@ -17,6 +18,7 @@ const isSortable = require('../utils/is-sortable.js')
17
18
  const useGroups = require('../utils/use-groups.js')
18
19
  const complete = require('../utils/complete.js')
19
20
  let defaultOptions = {
21
+ fallbackSort: { type: 'unsorted' },
20
22
  specialCharacters: 'keep',
21
23
  partitionByComment: false,
22
24
  sortOnProperties: true,
@@ -33,59 +35,6 @@ let defaultOptions = {
33
35
  groups: [],
34
36
  }
35
37
  const sortDecorators = createEslintRule.createEslintRule({
36
- meta: {
37
- schema: [
38
- {
39
- properties: {
40
- ...commonJsonSchemas.commonJsonSchemas,
41
- sortOnParameters: {
42
- description:
43
- 'Controls whether sorting should be enabled for method parameter decorators.',
44
- type: 'boolean',
45
- },
46
- sortOnProperties: {
47
- description:
48
- 'Controls whether sorting should be enabled for class property decorators.',
49
- type: 'boolean',
50
- },
51
- sortOnAccessors: {
52
- description:
53
- 'Controls whether sorting should be enabled for class accessor decorators.',
54
- type: 'boolean',
55
- },
56
- sortOnMethods: {
57
- description:
58
- 'Controls whether sorting should be enabled for class method decorators.',
59
- type: 'boolean',
60
- },
61
- sortOnClasses: {
62
- description:
63
- 'Controls whether sorting should be enabled for class decorators.',
64
- type: 'boolean',
65
- },
66
- partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema,
67
- customGroups: commonJsonSchemas.customGroupsJsonSchema,
68
- type: commonJsonSchemas.buildTypeJsonSchema(),
69
- groups: commonJsonSchemas.groupsJsonSchema,
70
- },
71
- additionalProperties: false,
72
- type: 'object',
73
- },
74
- ],
75
- messages: {
76
- unexpectedDecoratorsGroupOrder:
77
- 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
78
- unexpectedDecoratorsOrder:
79
- 'Expected "{{right}}" to come before "{{left}}".',
80
- },
81
- docs: {
82
- url: 'https://perfectionist.dev/rules/sort-decorators',
83
- description: 'Enforce sorted decorators.',
84
- recommended: true,
85
- },
86
- type: 'suggestion',
87
- fixable: 'code',
88
- },
89
38
  create: context => {
90
39
  let settings = getSettings.getSettings(context.settings)
91
40
  let options = complete.complete(
@@ -94,11 +43,11 @@ const sortDecorators = createEslintRule.createEslintRule({
94
43
  defaultOptions,
95
44
  )
96
45
  validateCustomSortConfiguration.validateCustomSortConfiguration(options)
97
- validateGroupsConfiguration.validateGroupsConfiguration(
98
- options.groups,
99
- ['unknown'],
100
- Object.keys(options.customGroups),
101
- )
46
+ validateGroupsConfiguration.validateGroupsConfiguration({
47
+ allowedCustomGroups: Object.keys(options.customGroups),
48
+ allowedPredefinedGroups: ['unknown'],
49
+ options,
50
+ })
102
51
  return {
103
52
  Decorator: decorator => {
104
53
  if (!options.sortOnParameters) {
@@ -150,6 +99,56 @@ const sortDecorators = createEslintRule.createEslintRule({
150
99
  : null,
151
100
  }
152
101
  },
102
+ meta: {
103
+ schema: [
104
+ {
105
+ properties: {
106
+ ...commonJsonSchemas.commonJsonSchemas,
107
+ sortOnParameters: {
108
+ description:
109
+ 'Controls whether sorting should be enabled for method parameter decorators.',
110
+ type: 'boolean',
111
+ },
112
+ sortOnProperties: {
113
+ description:
114
+ 'Controls whether sorting should be enabled for class property decorators.',
115
+ type: 'boolean',
116
+ },
117
+ sortOnAccessors: {
118
+ description:
119
+ 'Controls whether sorting should be enabled for class accessor decorators.',
120
+ type: 'boolean',
121
+ },
122
+ sortOnMethods: {
123
+ description:
124
+ 'Controls whether sorting should be enabled for class method decorators.',
125
+ type: 'boolean',
126
+ },
127
+ sortOnClasses: {
128
+ description:
129
+ 'Controls whether sorting should be enabled for class decorators.',
130
+ type: 'boolean',
131
+ },
132
+ partitionByComment: commonJsonSchemas.partitionByCommentJsonSchema,
133
+ customGroups: commonJsonSchemas.customGroupsJsonSchema,
134
+ groups: commonJsonSchemas.groupsJsonSchema,
135
+ },
136
+ additionalProperties: false,
137
+ type: 'object',
138
+ },
139
+ ],
140
+ docs: {
141
+ url: 'https://perfectionist.dev/rules/sort-decorators',
142
+ description: 'Enforce sorted decorators.',
143
+ recommended: true,
144
+ },
145
+ messages: {
146
+ unexpectedDecoratorsGroupOrder: reportErrors.GROUP_ORDER_ERROR,
147
+ unexpectedDecoratorsOrder: reportErrors.ORDER_ERROR,
148
+ },
149
+ type: 'suggestion',
150
+ fixable: 'code',
151
+ },
153
152
  defaultOptions: [defaultOptions],
154
153
  name: 'sort-decorators',
155
154
  })
@@ -166,7 +165,10 @@ let sortDecorators$1 = (context, options, decorators) => {
166
165
  (accumulator, decorator) => {
167
166
  var _a
168
167
  let { setCustomGroups, getGroup } = useGroups.useGroups(options)
169
- let name = getDecoratorName.getDecoratorName(decorator)
168
+ let name = getDecoratorName.getDecoratorName({
169
+ sourceCode,
170
+ decorator,
171
+ })
170
172
  setCustomGroups(options.customGroups, name)
171
173
  let sortingNode = {
172
174
  isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled(
@@ -0,0 +1,36 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ const matches = require('../../utils/matches.js')
4
+ let doesCustomGroupMatch = props => {
5
+ if ('anyOf' in props.customGroup) {
6
+ return props.customGroup.anyOf.some(subgroup =>
7
+ doesCustomGroupMatch({ ...props, customGroup: subgroup }),
8
+ )
9
+ }
10
+ if (
11
+ 'elementNamePattern' in props.customGroup &&
12
+ props.customGroup.elementNamePattern
13
+ ) {
14
+ let matchesElementNamePattern = matches.matches(
15
+ props.elementName,
16
+ props.customGroup.elementNamePattern,
17
+ )
18
+ if (!matchesElementNamePattern) {
19
+ return false
20
+ }
21
+ }
22
+ if (
23
+ 'elementValuePattern' in props.customGroup &&
24
+ props.customGroup.elementValuePattern
25
+ ) {
26
+ let matchesElementValuePattern = matches.matches(
27
+ props.elementValue,
28
+ props.customGroup.elementValuePattern,
29
+ )
30
+ if (!matchesElementValuePattern) {
31
+ return false
32
+ }
33
+ }
34
+ return true
35
+ }
36
+ exports.doesCustomGroupMatch = doesCustomGroupMatch
@@ -0,0 +1,8 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ const commonJsonSchemas = require('../../utils/common-json-schemas.js')
4
+ let singleCustomGroupJsonSchema = {
5
+ elementValuePattern: commonJsonSchemas.regexJsonSchema,
6
+ elementNamePattern: commonJsonSchemas.regexJsonSchema,
7
+ }
8
+ exports.singleCustomGroupJsonSchema = singleCustomGroupJsonSchema