eslint-plugin-perfectionist 3.4.0 → 3.6.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 (37) hide show
  1. package/dist/index.d.ts +14 -9
  2. package/dist/rules/sort-astro-attributes.js +8 -1
  3. package/dist/rules/sort-astro-attributes.mjs +8 -1
  4. package/dist/rules/sort-classes.js +10 -5
  5. package/dist/rules/sort-classes.mjs +7 -5
  6. package/dist/rules/sort-enums.js +9 -7
  7. package/dist/rules/sort-enums.mjs +7 -5
  8. package/dist/rules/sort-imports.js +32 -15
  9. package/dist/rules/sort-imports.mjs +31 -14
  10. package/dist/rules/sort-interfaces.js +16 -1
  11. package/dist/rules/sort-interfaces.mjs +10 -1
  12. package/dist/rules/sort-intersection-types.js +8 -1
  13. package/dist/rules/sort-intersection-types.mjs +8 -1
  14. package/dist/rules/sort-jsx-props.js +8 -1
  15. package/dist/rules/sort-jsx-props.mjs +8 -1
  16. package/dist/rules/sort-object-types.js +8 -1
  17. package/dist/rules/sort-object-types.mjs +8 -1
  18. package/dist/rules/sort-objects.js +84 -50
  19. package/dist/rules/sort-objects.mjs +78 -50
  20. package/dist/rules/sort-svelte-attributes.js +8 -1
  21. package/dist/rules/sort-svelte-attributes.mjs +8 -1
  22. package/dist/rules/sort-union-types.js +8 -1
  23. package/dist/rules/sort-union-types.mjs +8 -1
  24. package/dist/rules/sort-variable-declarations.js +39 -9
  25. package/dist/rules/sort-variable-declarations.mjs +39 -9
  26. package/dist/rules/sort-vue-attributes.js +8 -1
  27. package/dist/rules/sort-vue-attributes.mjs +8 -1
  28. package/dist/utils/get-comments-before.js +12 -0
  29. package/dist/utils/get-comments-before.mjs +10 -0
  30. package/dist/utils/get-node-range.js +26 -12
  31. package/dist/utils/get-node-range.mjs +26 -12
  32. package/dist/utils/is-partition-comment.js +3 -0
  33. package/dist/utils/is-partition-comment.mjs +3 -1
  34. package/package.json +4 -4
  35. package/readme.md +1 -0
  36. package/dist/utils/get-comment-before.js +0 -23
  37. package/dist/utils/get-comment-before.mjs +0 -21
@@ -63,20 +63,37 @@ const sortVariableDeclarations = createEslintRule({
63
63
  })
64
64
  let sourceCode = getSourceCode(context)
65
65
  let extractDependencies = init => {
66
- if (!init) {
67
- return []
68
- }
69
66
  let dependencies = []
70
67
  let checkNode = nodeValue => {
68
+ if (
69
+ nodeValue.type === 'ArrowFunctionExpression' ||
70
+ nodeValue.type === 'FunctionExpression'
71
+ ) {
72
+ return
73
+ }
71
74
  if (nodeValue.type === 'Identifier') {
72
75
  dependencies.push(nodeValue.name)
73
76
  }
77
+ if (nodeValue.type === 'Property') {
78
+ traverseNode(nodeValue.key)
79
+ traverseNode(nodeValue.value)
80
+ }
81
+ if (nodeValue.type === 'ConditionalExpression') {
82
+ traverseNode(nodeValue.test)
83
+ traverseNode(nodeValue.consequent)
84
+ traverseNode(nodeValue.alternate)
85
+ }
74
86
  if (
75
- 'body' in nodeValue &&
76
- nodeValue.body &&
77
- !Array.isArray(nodeValue.body)
87
+ 'expression' in nodeValue &&
88
+ typeof nodeValue.expression !== 'boolean'
78
89
  ) {
79
- traverseNode(nodeValue.body)
90
+ traverseNode(nodeValue.expression)
91
+ }
92
+ if ('object' in nodeValue) {
93
+ traverseNode(nodeValue.object)
94
+ }
95
+ if ('callee' in nodeValue) {
96
+ traverseNode(nodeValue.callee)
80
97
  }
81
98
  if ('left' in nodeValue) {
82
99
  traverseNode(nodeValue.left)
@@ -88,9 +105,19 @@ const sortVariableDeclarations = createEslintRule({
88
105
  nodeValue.elements
89
106
  .filter(currentNode => currentNode !== null)
90
107
  .forEach(traverseNode)
91
- } else if ('arguments' in nodeValue) {
108
+ }
109
+ if ('argument' in nodeValue && nodeValue.argument) {
110
+ traverseNode(nodeValue.argument)
111
+ }
112
+ if ('arguments' in nodeValue) {
92
113
  nodeValue.arguments.forEach(traverseNode)
93
114
  }
115
+ if ('properties' in nodeValue) {
116
+ nodeValue.properties.forEach(traverseNode)
117
+ }
118
+ if ('expressions' in nodeValue) {
119
+ nodeValue.expressions.forEach(traverseNode)
120
+ }
94
121
  }
95
122
  let traverseNode = nodeValue => {
96
123
  checkNode(nodeValue)
@@ -108,7 +135,10 @@ const sortVariableDeclarations = createEslintRule({
108
135
  } else {
109
136
  ;({ name } = declaration.id)
110
137
  }
111
- let dependencies = extractDependencies(declaration.init)
138
+ let dependencies = []
139
+ if (declaration.init) {
140
+ dependencies = extractDependencies(declaration.init)
141
+ }
112
142
  return {
113
143
  size: rangeToDiff(declaration.range),
114
144
  node: declaration,
@@ -79,6 +79,8 @@ const sortVueAttributes = createEslintRule.createEslintRule({
79
79
  },
80
80
  ],
81
81
  messages: {
82
+ unexpectedVueAttributesGroupOrder:
83
+ 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
82
84
  unexpectedVueAttributesOrder:
83
85
  'Expected "{{right}}" to come before "{{left}}".',
84
86
  },
@@ -167,10 +169,15 @@ const sortVueAttributes = createEslintRule.createEslintRule({
167
169
  isPositive.isPositive(compare.compare(left, right, options)))
168
170
  ) {
169
171
  context.report({
170
- messageId: 'unexpectedVueAttributesOrder',
172
+ messageId:
173
+ leftNum !== rightNum
174
+ ? 'unexpectedVueAttributesGroupOrder'
175
+ : 'unexpectedVueAttributesOrder',
171
176
  data: {
172
177
  left: left.name,
178
+ leftGroup: left.group,
173
179
  right: right.name,
180
+ rightGroup: right.group,
174
181
  },
175
182
  // @ts-ignore
176
183
  node: right.node,
@@ -78,6 +78,8 @@ const sortVueAttributes = createEslintRule({
78
78
  },
79
79
  ],
80
80
  messages: {
81
+ unexpectedVueAttributesGroupOrder:
82
+ 'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
81
83
  unexpectedVueAttributesOrder:
82
84
  'Expected "{{right}}" to come before "{{left}}".',
83
85
  },
@@ -164,10 +166,15 @@ const sortVueAttributes = createEslintRule({
164
166
  isPositive(compare(left, right, options)))
165
167
  ) {
166
168
  context.report({
167
- messageId: 'unexpectedVueAttributesOrder',
169
+ messageId:
170
+ leftNum !== rightNum
171
+ ? 'unexpectedVueAttributesGroupOrder'
172
+ : 'unexpectedVueAttributesOrder',
168
173
  data: {
169
174
  left: left.name,
175
+ leftGroup: left.group,
170
176
  right: right.name,
177
+ rightGroup: right.group,
171
178
  },
172
179
  // @ts-ignore
173
180
  node: right.node,
@@ -0,0 +1,12 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
+ let getCommentsBefore = (node, source) =>
4
+ source.getCommentsBefore(node).filter(comment => {
5
+ let tokenBeforeComment = source.getTokenBefore(comment)
6
+ return (
7
+ (tokenBeforeComment == null
8
+ ? void 0
9
+ : tokenBeforeComment.loc.end.line) !== comment.loc.end.line
10
+ )
11
+ })
12
+ exports.getCommentsBefore = getCommentsBefore
@@ -0,0 +1,10 @@
1
+ let getCommentsBefore = (node, source) =>
2
+ source.getCommentsBefore(node).filter(comment => {
3
+ let tokenBeforeComment = source.getTokenBefore(comment)
4
+ return (
5
+ (tokenBeforeComment == null
6
+ ? void 0
7
+ : tokenBeforeComment.loc.end.line) !== comment.loc.end.line
8
+ )
9
+ })
10
+ export { getCommentsBefore }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
3
  const utils = require('@typescript-eslint/utils')
4
4
  const isPartitionComment = require('./is-partition-comment.js')
5
- const getCommentBefore = require('./get-comment-before.js')
5
+ const getCommentsBefore = require('./get-comments-before.js')
6
6
  let getNodeRange = (node, sourceCode, additionalOptions) => {
7
7
  var _a
8
8
  let start = node.range.at(0)
@@ -20,7 +20,6 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
20
20
  start = bodyOpeningParen.range.at(0)
21
21
  end = bodyClosingParen.range.at(1)
22
22
  }
23
- let comment = getCommentBefore.getCommentBefore(node, sourceCode)
24
23
  if (raw.endsWith(';') || raw.endsWith(',')) {
25
24
  let tokensAfter = sourceCode.getTokensAfter(node, {
26
25
  includeComments: true,
@@ -33,16 +32,31 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
33
32
  end -= 1
34
33
  }
35
34
  }
36
- if (
37
- comment &&
38
- !isPartitionComment.isPartitionComment(
39
- (additionalOptions == null
40
- ? void 0
41
- : additionalOptions.partitionComment) ?? false,
42
- comment.value,
43
- )
44
- ) {
45
- start = comment.range.at(0)
35
+ let comments = getCommentsBefore.getCommentsBefore(node, sourceCode)
36
+ let relevantTopComment
37
+ for (let i = comments.length - 1; i >= 0; i--) {
38
+ let comment = comments[i]
39
+ if (
40
+ isPartitionComment.isPartitionComment(
41
+ (additionalOptions == null
42
+ ? void 0
43
+ : additionalOptions.partitionComment) ?? false,
44
+ comment.value,
45
+ )
46
+ ) {
47
+ break
48
+ }
49
+ let previousCommentOrNodeStartLine =
50
+ i === comments.length - 1
51
+ ? node.loc.start.line
52
+ : comments[i + 1].loc.start.line
53
+ if (comment.loc.end.line !== previousCommentOrNodeStartLine - 1) {
54
+ break
55
+ }
56
+ relevantTopComment = comment
57
+ }
58
+ if (relevantTopComment) {
59
+ start = relevantTopComment.range.at(0)
46
60
  }
47
61
  return [start, end]
48
62
  }
@@ -1,6 +1,6 @@
1
1
  import { ASTUtils } from '@typescript-eslint/utils'
2
2
  import { isPartitionComment } from './is-partition-comment.mjs'
3
- import { getCommentBefore } from './get-comment-before.mjs'
3
+ import { getCommentsBefore } from './get-comments-before.mjs'
4
4
  let getNodeRange = (node, sourceCode, additionalOptions) => {
5
5
  var _a
6
6
  let start = node.range.at(0)
@@ -18,7 +18,6 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
18
18
  start = bodyOpeningParen.range.at(0)
19
19
  end = bodyClosingParen.range.at(1)
20
20
  }
21
- let comment = getCommentBefore(node, sourceCode)
22
21
  if (raw.endsWith(';') || raw.endsWith(',')) {
23
22
  let tokensAfter = sourceCode.getTokensAfter(node, {
24
23
  includeComments: true,
@@ -31,16 +30,31 @@ let getNodeRange = (node, sourceCode, additionalOptions) => {
31
30
  end -= 1
32
31
  }
33
32
  }
34
- if (
35
- comment &&
36
- !isPartitionComment(
37
- (additionalOptions == null
38
- ? void 0
39
- : additionalOptions.partitionComment) ?? false,
40
- comment.value,
41
- )
42
- ) {
43
- start = comment.range.at(0)
33
+ let comments = getCommentsBefore(node, sourceCode)
34
+ let relevantTopComment
35
+ for (let i = comments.length - 1; i >= 0; i--) {
36
+ let comment = comments[i]
37
+ if (
38
+ isPartitionComment(
39
+ (additionalOptions == null
40
+ ? void 0
41
+ : additionalOptions.partitionComment) ?? false,
42
+ comment.value,
43
+ )
44
+ ) {
45
+ break
46
+ }
47
+ let previousCommentOrNodeStartLine =
48
+ i === comments.length - 1
49
+ ? node.loc.start.line
50
+ : comments[i + 1].loc.start.line
51
+ if (comment.loc.end.line !== previousCommentOrNodeStartLine - 1) {
52
+ break
53
+ }
54
+ relevantTopComment = comment
55
+ }
56
+ if (relevantTopComment) {
57
+ start = relevantTopComment.range.at(0)
44
58
  }
45
59
  return [start, end]
46
60
  }
@@ -13,4 +13,7 @@ let isPartitionComment = (partitionComment, comment) =>
13
13
  nocomment: true,
14
14
  })) ||
15
15
  partitionComment === true
16
+ let hasPartitionComment = (partitionComment, comments) =>
17
+ comments.some(comment => isPartitionComment(partitionComment, comment.value))
18
+ exports.hasPartitionComment = hasPartitionComment
16
19
  exports.isPartitionComment = isPartitionComment
@@ -11,4 +11,6 @@ let isPartitionComment = (partitionComment, comment) =>
11
11
  nocomment: true,
12
12
  })) ||
13
13
  partitionComment === true
14
- export { isPartitionComment }
14
+ let hasPartitionComment = (partitionComment, comments) =>
15
+ comments.some(comment => isPartitionComment(partitionComment, comment.value))
16
+ export { hasPartitionComment, isPartitionComment }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "eslint-plugin-perfectionist",
3
3
  "description": "ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.",
4
- "version": "3.4.0",
4
+ "version": "3.6.0",
5
5
  "homepage": "https://perfectionist.dev",
6
6
  "repository": "azat-io/eslint-plugin-perfectionist",
7
7
  "author": "Azat S. <to@azat.io>",
@@ -49,12 +49,12 @@
49
49
  "astro-eslint-parser": "^1.0.2",
50
50
  "eslint": ">=8.0.0",
51
51
  "svelte": ">=3.0.0",
52
- "svelte-eslint-parser": "^0.41.0",
52
+ "svelte-eslint-parser": "^0.41.1",
53
53
  "vue-eslint-parser": ">=9.0.0"
54
54
  },
55
55
  "dependencies": {
56
- "@typescript-eslint/types": "^8.4.0",
57
- "@typescript-eslint/utils": "^8.4.0",
56
+ "@typescript-eslint/types": "^8.5.0",
57
+ "@typescript-eslint/utils": "^8.5.0",
58
58
  "minimatch": "^9.0.5",
59
59
  "natural-compare-lite": "^1.4.0"
60
60
  }
package/readme.md CHANGED
@@ -188,6 +188,7 @@ module.exports = {
188
188
  | [sort-named-imports](https://perfectionist.dev/rules/sort-named-imports) | Enforce sorted named imports | 🔧 |
189
189
  | [sort-object-types](https://perfectionist.dev/rules/sort-object-types) | Enforce sorted object types | 🔧 |
190
190
  | [sort-objects](https://perfectionist.dev/rules/sort-objects) | Enforce sorted objects | 🔧 |
191
+ | [sort-sets](https://perfectionist.dev/rules/sort-sets) | Enforce sorted Set elements | 🔧 |
191
192
  | [sort-svelte-attributes](https://perfectionist.dev/rules/sort-svelte-attributes) | Enforce sorted Svelte attributes | 🔧 |
192
193
  | [sort-switch-case](https://perfectionist.dev/rules/sort-switch-case) | Enforce sorted switch case statements | 🔧 |
193
194
  | [sort-union-types](https://perfectionist.dev/rules/sort-union-types) | Enforce sorted union types | 🔧 |
@@ -1,23 +0,0 @@
1
- 'use strict'
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
3
- let getCommentBefore = (node, source) => {
4
- let [tokenBefore, tokenOrCommentBefore] = source.getTokensBefore(node, {
5
- filter: ({ value, type }) =>
6
- !(type === 'Punctuator' && [',', ';'].includes(value)),
7
- includeComments: true,
8
- count: 2,
9
- })
10
- if (
11
- ((tokenOrCommentBefore == null ? void 0 : tokenOrCommentBefore.type) ===
12
- 'Block' ||
13
- (tokenOrCommentBefore == null ? void 0 : tokenOrCommentBefore.type) ===
14
- 'Line') &&
15
- node.loc.start.line - tokenOrCommentBefore.loc.end.line <= 1 &&
16
- (tokenBefore == null ? void 0 : tokenBefore.loc.end.line) !==
17
- tokenOrCommentBefore.loc.start.line
18
- ) {
19
- return tokenOrCommentBefore
20
- }
21
- return null
22
- }
23
- exports.getCommentBefore = getCommentBefore
@@ -1,21 +0,0 @@
1
- let getCommentBefore = (node, source) => {
2
- let [tokenBefore, tokenOrCommentBefore] = source.getTokensBefore(node, {
3
- filter: ({ value, type }) =>
4
- !(type === 'Punctuator' && [',', ';'].includes(value)),
5
- includeComments: true,
6
- count: 2,
7
- })
8
- if (
9
- ((tokenOrCommentBefore == null ? void 0 : tokenOrCommentBefore.type) ===
10
- 'Block' ||
11
- (tokenOrCommentBefore == null ? void 0 : tokenOrCommentBefore.type) ===
12
- 'Line') &&
13
- node.loc.start.line - tokenOrCommentBefore.loc.end.line <= 1 &&
14
- (tokenBefore == null ? void 0 : tokenBefore.loc.end.line) !==
15
- tokenOrCommentBefore.loc.start.line
16
- ) {
17
- return tokenOrCommentBefore
18
- }
19
- return null
20
- }
21
- export { getCommentBefore }