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.
@@ -2,6 +2,7 @@
2
2
  const commonJsonSchemas = require('../utils/common-json-schemas.js')
3
3
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-configuration.js')
4
4
  const makeCommentAfterFixes = require('../utils/make-comment-after-fixes.js')
5
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
5
6
  const createEslintRule = require('../utils/create-eslint-rule.js')
6
7
  const getSourceCode = require('../utils/get-source-code.js')
7
8
  const rangeToDiff = require('../utils/range-to-diff.js')
@@ -70,10 +71,13 @@ const sortSwitchCase = createEslintRule.createEslintRule({
70
71
  (hasUnsortedNodes = sortedCaseNameSortingNodes.some(
71
72
  (node, index) => node !== caseNodesSortingNodeGroup[index],
72
73
  ))
74
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(
75
+ sortedCaseNameSortingNodes,
76
+ )
73
77
  pairwise.pairwise(caseNodesSortingNodeGroup, (left, right) => {
74
- let indexOfLeft = sortedCaseNameSortingNodes.indexOf(left)
75
- let indexOfRight = sortedCaseNameSortingNodes.indexOf(right)
76
- if (indexOfLeft < indexOfRight) {
78
+ let leftIndex = nodeIndexMap.get(left)
79
+ let rightIndex = nodeIndexMap.get(right)
80
+ if (leftIndex < rightIndex) {
77
81
  return
78
82
  }
79
83
  context.report({
@@ -222,9 +226,9 @@ const sortSwitchCase = createEslintRule.createEslintRule({
222
226
  specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
223
227
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
224
228
  alphabet: commonJsonSchemas.alphabetJsonSchema,
229
+ type: commonJsonSchemas.buildTypeJsonSchema(),
225
230
  locales: commonJsonSchemas.localesJsonSchema,
226
231
  order: commonJsonSchemas.orderJsonSchema,
227
- type: commonJsonSchemas.typeJsonSchema,
228
232
  },
229
233
  additionalProperties: false,
230
234
  type: 'object',
@@ -267,18 +271,12 @@ let reduceCaseSortingNodes = (caseNodes, endsBlock) =>
267
271
  )
268
272
  let caseHasBreakOrReturn = caseNode => {
269
273
  var _a
270
- if (caseNode.consequent.length === 0) {
271
- return false
272
- }
273
- if (
274
+ let statements =
274
275
  ((_a = caseNode.consequent[0]) == null ? void 0 : _a.type) ===
275
276
  'BlockStatement'
276
- ) {
277
- return caseNode.consequent[0].body.some(statementIsBreakOrReturn)
278
- }
279
- return caseNode.consequent.some(currentConsequent =>
280
- statementIsBreakOrReturn(currentConsequent),
281
- )
277
+ ? caseNode.consequent[0].body
278
+ : caseNode.consequent
279
+ return statements.some(statementIsBreakOrReturn)
282
280
  }
283
281
  let statementIsBreakOrReturn = statement =>
284
282
  statement.type === 'BreakStatement' || statement.type === 'ReturnStatement'
@@ -5,6 +5,7 @@ const validateCustomSortConfiguration = require('../utils/validate-custom-sort-c
5
5
  const validateGroupsConfiguration = require('../utils/validate-groups-configuration.js')
6
6
  const getEslintDisabledLines = require('../utils/get-eslint-disabled-lines.js')
7
7
  const isNodeEslintDisabled = require('../utils/is-node-eslint-disabled.js')
8
+ const createNodeIndexMap = require('../utils/create-node-index-map.js')
8
9
  const isPartitionComment = require('../utils/is-partition-comment.js')
9
10
  const sortNodesByGroups = require('../utils/sort-nodes-by-groups.js')
10
11
  const getCommentsBefore = require('../utils/get-comments-before.js')
@@ -72,7 +73,6 @@ const sortUnionTypes = createEslintRule.createEslintRule({
72
73
  })
73
74
  let formattedMembers = node.types.reduce(
74
75
  (accumulator, type) => {
75
- var _a, _b
76
76
  let { defineGroup, getGroup } = useGroups.useGroups(options)
77
77
  switch (type.type) {
78
78
  case 'TSTemplateLiteralType':
@@ -131,8 +131,8 @@ const sortUnionTypes = createEslintRule.createEslintRule({
131
131
  defineGroup('union')
132
132
  break
133
133
  }
134
- let lastSortingNode =
135
- (_a = accumulator.at(-1)) == null ? void 0 : _a.at(-1)
134
+ let lastGroup = accumulator.at(-1)
135
+ let lastSortingNode = lastGroup == null ? void 0 : lastGroup.at(-1)
136
136
  let sortingNode = {
137
137
  isEslintDisabled: isNodeEslintDisabled.isNodeEslintDisabled(
138
138
  type,
@@ -144,15 +144,14 @@ const sortUnionTypes = createEslintRule.createEslintRule({
144
144
  node: type,
145
145
  }
146
146
  if (
147
- (options.partitionByComment &&
148
- isPartitionComment.hasPartitionComment(
149
- options.partitionByComment,
150
- getCommentsBefore.getCommentsBefore({
151
- tokenValueToIgnoreBefore: '|',
152
- node: type,
153
- sourceCode,
154
- }),
155
- )) ||
147
+ isPartitionComment.hasPartitionComment(
148
+ options.partitionByComment,
149
+ getCommentsBefore.getCommentsBefore({
150
+ tokenValueToIgnoreBefore: '|',
151
+ node: type,
152
+ sourceCode,
153
+ }),
154
+ ) ||
156
155
  (options.partitionByNewLine &&
157
156
  lastSortingNode &&
158
157
  getLinesBetween.getLinesBetween(
@@ -161,9 +160,10 @@ const sortUnionTypes = createEslintRule.createEslintRule({
161
160
  sortingNode,
162
161
  ))
163
162
  ) {
164
- accumulator.push([])
163
+ lastGroup = []
164
+ accumulator.push(lastGroup)
165
165
  }
166
- ;(_b = accumulator.at(-1)) == null ? void 0 : _b.push(sortingNode)
166
+ lastGroup == null ? void 0 : lastGroup.push(sortingNode)
167
167
  return accumulator
168
168
  },
169
169
  [[]],
@@ -176,17 +176,18 @@ const sortUnionTypes = createEslintRule.createEslintRule({
176
176
  let sortedNodes = sortNodesExcludingEslintDisabled(false)
177
177
  let sortedNodesExcludingEslintDisabled =
178
178
  sortNodesExcludingEslintDisabled(true)
179
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
179
180
  pairwise.pairwise(nodes, (left, right) => {
180
181
  let leftNumber = getGroupNumber.getGroupNumber(options.groups, left)
181
182
  let rightNumber = getGroupNumber.getGroupNumber(options.groups, right)
182
- let indexOfLeft = sortedNodes.indexOf(left)
183
- let indexOfRight = sortedNodes.indexOf(right)
183
+ let leftIndex = nodeIndexMap.get(left)
184
+ let rightIndex = nodeIndexMap.get(right)
184
185
  let indexOfRightExcludingEslintDisabled =
185
186
  sortedNodesExcludingEslintDisabled.indexOf(right)
186
187
  let messageIds = []
187
188
  if (
188
- indexOfLeft > indexOfRight ||
189
- indexOfLeft >= indexOfRightExcludingEslintDisabled
189
+ leftIndex > rightIndex ||
190
+ leftIndex >= indexOfRightExcludingEslintDisabled
190
191
  ) {
191
192
  messageIds.push(
192
193
  leftNumber === rightNumber
@@ -253,10 +254,10 @@ const sortUnionTypes = createEslintRule.createEslintRule({
253
254
  newlinesBetween: commonJsonSchemas.newlinesBetweenJsonSchema,
254
255
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
255
256
  alphabet: commonJsonSchemas.alphabetJsonSchema,
257
+ type: commonJsonSchemas.buildTypeJsonSchema(),
256
258
  locales: commonJsonSchemas.localesJsonSchema,
257
259
  groups: commonJsonSchemas.groupsJsonSchema,
258
260
  order: commonJsonSchemas.orderJsonSchema,
259
- type: commonJsonSchemas.typeJsonSchema,
260
261
  },
261
262
  additionalProperties: false,
262
263
  type: 'object',
@@ -4,6 +4,7 @@ const sortNodesByDependencies = require('../utils/sort-nodes-by-dependencies.js'
4
4
  const validateCustomSortConfiguration = require('../utils/validate-custom-sort-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 isPartitionComment = require('../utils/is-partition-comment.js')
8
9
  const getCommentsBefore = require('../utils/get-comments-before.js')
9
10
  const createEslintRule = require('../utils/create-eslint-rule.js')
@@ -146,14 +147,13 @@ const sortVariableDeclarations = createEslintRule.createEslintRule({
146
147
  name,
147
148
  }
148
149
  if (
149
- (options.partitionByComment &&
150
- isPartitionComment.hasPartitionComment(
151
- options.partitionByComment,
152
- getCommentsBefore.getCommentsBefore({
153
- node: declaration,
154
- sourceCode,
155
- }),
156
- )) ||
150
+ isPartitionComment.hasPartitionComment(
151
+ options.partitionByComment,
152
+ getCommentsBefore.getCommentsBefore({
153
+ node: declaration,
154
+ sourceCode,
155
+ }),
156
+ ) ||
157
157
  (options.partitionByNewLine &&
158
158
  lastSortingNode &&
159
159
  getLinesBetween.getLinesBetween(
@@ -184,14 +184,15 @@ const sortVariableDeclarations = createEslintRule.createEslintRule({
184
184
  let sortedNodesExcludingEslintDisabled =
185
185
  sortNodesIgnoringEslintDisabledNodes(true)
186
186
  let nodes = formattedMembers.flat()
187
+ let nodeIndexMap = createNodeIndexMap.createNodeIndexMap(sortedNodes)
187
188
  pairwise.pairwise(nodes, (left, right) => {
188
- let indexOfLeft = sortedNodes.indexOf(left)
189
- let indexOfRight = sortedNodes.indexOf(right)
189
+ let leftIndex = nodeIndexMap.get(left)
190
+ let rightIndex = nodeIndexMap.get(right)
190
191
  let indexOfRightExcludingEslintDisabled =
191
192
  sortedNodesExcludingEslintDisabled.indexOf(right)
192
193
  if (
193
- indexOfLeft < indexOfRight &&
194
- indexOfLeft < indexOfRightExcludingEslintDisabled
194
+ leftIndex < rightIndex &&
195
+ leftIndex < indexOfRightExcludingEslintDisabled
195
196
  ) {
196
197
  return
197
198
  }
@@ -235,9 +236,9 @@ const sortVariableDeclarations = createEslintRule.createEslintRule({
235
236
  specialCharacters: commonJsonSchemas.specialCharactersJsonSchema,
236
237
  ignoreCase: commonJsonSchemas.ignoreCaseJsonSchema,
237
238
  alphabet: commonJsonSchemas.alphabetJsonSchema,
239
+ type: commonJsonSchemas.buildTypeJsonSchema(),
238
240
  locales: commonJsonSchemas.localesJsonSchema,
239
241
  order: commonJsonSchemas.orderJsonSchema,
240
- type: commonJsonSchemas.typeJsonSchema,
241
242
  },
242
243
  additionalProperties: false,
243
244
  type: 'object',
@@ -1,9 +1,15 @@
1
1
  'use strict'
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
- let typeJsonSchema = {
4
- enum: ['alphabetical', 'natural', 'line-length', 'custom'],
5
- description: 'Specifies the sorting method.',
6
- type: 'string',
3
+ let buildTypeJsonSchema = ({ withUnsorted } = {}) => {
4
+ let enumValues = ['alphabetical', 'natural', 'line-length', 'custom']
5
+ if (withUnsorted) {
6
+ enumValues.push('unsorted')
7
+ }
8
+ return {
9
+ description: 'Specifies the sorting method.',
10
+ enum: enumValues,
11
+ type: 'string',
12
+ }
7
13
  }
8
14
  let orderJsonSchema = {
9
15
  description:
@@ -100,15 +106,16 @@ let newlinesBetweenJsonSchema = {
100
106
  enum: ['ignore', 'always', 'never'],
101
107
  type: 'string',
102
108
  }
103
- let useConfigurationIfJsonSchema = {
109
+ let buildUseConfigurationIfJsonSchema = ({ additionalProperties } = {}) => ({
104
110
  properties: {
105
111
  allNamesMatchPattern: {
106
112
  type: 'string',
107
113
  },
114
+ ...additionalProperties,
108
115
  },
109
116
  additionalProperties: false,
110
117
  type: 'object',
111
- }
118
+ })
112
119
  let customGroupSortJsonSchema = {
113
120
  type: {
114
121
  enum: ['alphabetical', 'line-length', 'natural', 'unsorted'],
@@ -187,6 +194,8 @@ exports.buildCustomGroupModifiersJsonSchema =
187
194
  buildCustomGroupModifiersJsonSchema
188
195
  exports.buildCustomGroupSelectorJsonSchema = buildCustomGroupSelectorJsonSchema
189
196
  exports.buildCustomGroupsArrayJsonSchema = buildCustomGroupsArrayJsonSchema
197
+ exports.buildTypeJsonSchema = buildTypeJsonSchema
198
+ exports.buildUseConfigurationIfJsonSchema = buildUseConfigurationIfJsonSchema
190
199
  exports.customGroupsJsonSchema = customGroupsJsonSchema
191
200
  exports.elementNamePatternJsonSchema = elementNamePatternJsonSchema
192
201
  exports.groupsJsonSchema = groupsJsonSchema
@@ -197,5 +206,3 @@ exports.orderJsonSchema = orderJsonSchema
197
206
  exports.partitionByCommentJsonSchema = partitionByCommentJsonSchema
198
207
  exports.partitionByNewLineJsonSchema = partitionByNewLineJsonSchema
199
208
  exports.specialCharactersJsonSchema = specialCharactersJsonSchema
200
- exports.typeJsonSchema = typeJsonSchema
201
- exports.useConfigurationIfJsonSchema = useConfigurationIfJsonSchema
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ let createNodeIndexMap = nodes => {
4
+ let nodeIndexMap = /* @__PURE__ */ new Map()
5
+ for (let [index, node] of nodes.entries()) {
6
+ nodeIndexMap.set(node, index)
7
+ }
8
+ return nodeIndexMap
9
+ }
10
+ exports.createNodeIndexMap = createNodeIndexMap
@@ -1,14 +1,13 @@
1
1
  'use strict'
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
- let getNodeParent = (node, type) => {
4
- let types = type
3
+ let getFirstNodeParentWithType = ({ allowedTypes, node }) => {
5
4
  let { parent } = node
6
5
  while (parent) {
7
- if (types.includes(parent.type)) {
6
+ if (allowedTypes.includes(parent.type)) {
8
7
  return parent
9
8
  }
10
9
  ;({ parent } = parent)
11
10
  }
12
11
  return null
13
12
  }
14
- exports.getNodeParent = getNodeParent
13
+ exports.getFirstNodeParentWithType = getFirstNodeParentWithType
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
3
  const matches = require('./matches.js')
4
4
  let getMatchingContextOptions = ({ contextOptions, nodeNames }) =>
5
- contextOptions.find(options => {
5
+ contextOptions.filter(options => {
6
6
  var _a
7
7
  let allNamesMatchPattern =
8
8
  (_a = options.useConfigurationIf) == null
@@ -3,7 +3,10 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
3
  const getEslintDisabledRules = require('./get-eslint-disabled-rules.js')
4
4
  const matches = require('./matches.js')
5
5
  let isPartitionComment = (partitionComment, comment) => {
6
- if (getEslintDisabledRules.getEslintDisabledRules(comment)) {
6
+ if (
7
+ getEslintDisabledRules.getEslintDisabledRules(comment) ||
8
+ !partitionComment
9
+ ) {
7
10
  return false
8
11
  }
9
12
  return (
@@ -8,9 +8,7 @@ let pairwise = (nodes, callback) => {
8
8
  for (let i = 1; i < nodes.length; i++) {
9
9
  let left = nodes.at(i - 1)
10
10
  let right = nodes.at(i)
11
- if (left && right) {
12
- callback(left, right, i - 1)
13
- }
11
+ callback(left, right, i - 1)
14
12
  }
15
13
  }
16
14
  exports.pairwise = pairwise
@@ -19,11 +19,17 @@ let validateGroupsConfiguration = (
19
19
  }
20
20
  let validateNoDuplicatedGroups = groups => {
21
21
  let flattenGroups = groups.flat()
22
- let duplicatedGroups = flattenGroups.filter(
23
- (group, index) => flattenGroups.indexOf(group) !== index,
24
- )
25
- if (duplicatedGroups.length) {
26
- throw new Error(`Duplicated group(s): ${duplicatedGroups.join(', ')}`)
22
+ let seenGroups = /* @__PURE__ */ new Set()
23
+ let duplicatedGroups = /* @__PURE__ */ new Set()
24
+ for (let group of flattenGroups) {
25
+ if (seenGroups.has(group)) {
26
+ duplicatedGroups.add(group)
27
+ } else {
28
+ seenGroups.add(group)
29
+ }
30
+ }
31
+ if (duplicatedGroups.size > 0) {
32
+ throw new Error(`Duplicated group(s): ${[...duplicatedGroups].join(', ')}`)
27
33
  }
28
34
  }
29
35
  exports.validateGroupsConfiguration = validateGroupsConfiguration
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-perfectionist",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.",
5
5
  "keywords": [
6
6
  "eslint",
@@ -27,8 +27,8 @@
27
27
  "./dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@typescript-eslint/types": "^8.18.0",
31
- "@typescript-eslint/utils": "^8.18.0",
30
+ "@typescript-eslint/types": "^8.18.1",
31
+ "@typescript-eslint/utils": "^8.18.1",
32
32
  "natural-orderby": "^5.0.0"
33
33
  },
34
34
  "peerDependencies": {