@@ -10,6 +10,29 @@ const naturalCompare = require("natural-compare");
10
10
const getKeyName = ( node ) =>
11
11
node . key . type === "Identifier" ? node . key . name : "" ;
12
12
13
+ /**
14
+ * @param {import('estree').Expression } arg
15
+ * @returns {string }
16
+ */
17
+ const getArgName = ( arg ) => ( arg . type === "Identifier" ? arg . name : "" ) ;
18
+
19
+ /**
20
+ * @param {import('estree').Property | import('estree').SpreadElement } a
21
+ * @param {import('estree').Property | import('estree').SpreadElement } b
22
+ * @returns {-1 | 0 | 1 }
23
+ */
24
+ const compareNodes = ( a , b ) => {
25
+ if ( a . type === "Property" && b . type === "Property" ) {
26
+ return naturalCompare ( getKeyName ( a ) , getKeyName ( b ) ) ;
27
+ }
28
+
29
+ if ( a . type === "SpreadElement" && b . type === "SpreadElement" ) {
30
+ return naturalCompare ( getArgName ( a . argument ) , getArgName ( b . argument ) ) ;
31
+ }
32
+
33
+ return a . type === "SpreadElement" ? - 1 : 1 ;
34
+ } ;
35
+
13
36
/** @type {import('eslint').Rule.RuleModule } */
14
37
module . exports = {
15
38
meta : {
@@ -30,16 +53,13 @@ module.exports = {
30
53
return ;
31
54
}
32
55
33
- const properties = value . properties . filter (
34
- /** @returns {node is import('estree').Property } */
35
- ( node ) => node . type === "Property"
36
- ) ;
37
- const sorted = [ ...properties ] . sort ( ( a , b ) =>
38
- naturalCompare ( getKeyName ( a ) , getKeyName ( b ) )
39
- ) ;
56
+ const properties = value . properties ;
57
+ const sorted = [ ...properties ] . sort ( compareNodes ) ;
40
58
const sameOrder = properties . every ( ( v , i ) => v === sorted [ i ] ) ;
41
59
42
- if ( sameOrder ) return ;
60
+ if ( sameOrder ) {
61
+ return ;
62
+ }
43
63
44
64
context . report ( {
45
65
node,
0 commit comments