@@ -40,56 +40,49 @@ function isReservedPropName(name, list) {
40
40
return list . indexOf ( name ) >= 0 ;
41
41
}
42
42
43
-
44
43
function alphabeticalCompare ( a , b , ignoreCase ) {
45
44
if ( ignoreCase ) {
46
45
a = a . toLowerCase ( ) ;
47
46
b = b . toLowerCase ( ) ;
48
47
}
49
-
50
- if ( a < b ) {
51
- return - 1 ;
52
- } else if ( a > b ) {
53
- return 1 ;
54
- }
55
-
56
- return 0 ;
48
+ return a . localeCompare ( b ) ;
57
49
}
58
50
59
51
function generateFixerFunction ( node , context ) {
60
52
var sourceCode = context . getSourceCode ( ) ;
61
- var sortedAttributes = node . attributes . slice ( 0 ) ;
53
+ var attributes = node . attributes . slice ( 0 ) ;
62
54
var configuration = context . options [ 0 ] || { } ;
63
55
var ignoreCase = configuration . ignoreCase || false ;
64
56
65
57
// Sort props according to the context. Only supports ignoreCase.
66
- sortedAttributes . sort ( function ( a , b ) {
67
- // Put spread attributes at the end.
68
- if ( a . type === 'JSXSpreadAttribute' && b . type !== 'JSXSpreadAttribute' ) {
69
- return 1 ;
70
- } else if ( a . type !== 'JSXSpreadAttribute' && b . type === 'JSXSpreadAttribute' ) {
71
- return - 1 ;
72
- } else if ( a . type === 'JSXSpreadAttribute' && b . type === 'JSXSpreadAttribute' ) {
73
- return alphabeticalCompare (
74
- sourceCode . getText ( a ) ,
75
- sourceCode . getText ( b ) ,
76
- ignoreCase
77
- ) ;
78
- }
79
-
80
- var propNameA = propName ( a ) ;
81
- var propNameB = propName ( b ) ;
58
+ // Since we cannot safely move JSXSpreadAttributes (due to potential variable overrides),
59
+ // we only consider the sortable attributes.
60
+ const sortableAttributes = attributes . filter ( function ( attr ) {
61
+ return attr . type !== 'JSXSpreadAttribute' ;
62
+ } ) ;
82
63
83
- return alphabeticalCompare ( propNameA , propNameB , ignoreCase ) ;
64
+ sortableAttributes . sort ( function ( a , b ) {
65
+ return alphabeticalCompare ( propName ( a ) , propName ( b ) , ignoreCase ) ;
84
66
} ) ;
85
67
86
68
return function ( fixer ) {
87
69
// Replace each unsorted attribute with the sorted one.
88
- return node . attributes . map ( function ( attr , index ) {
89
- var sortedAttr = sortedAttributes [ index ] ;
70
+ const fixers = [ ] ;
71
+ let skipIndex = 0 ; // skip over spread attributes
72
+
73
+ node . attributes . forEach ( function ( attr , index ) {
74
+ if ( attr . type === 'JSXSpreadAttribute' ) {
75
+ skipIndex ++ ;
76
+ return ;
77
+ }
78
+
79
+ var sortedAttr = sortableAttributes [ index - skipIndex ] ;
90
80
var sortedAttrText = sourceCode . getText ( sortedAttr ) ;
91
- return fixer . replaceTextRange ( [ attr . start , attr . end ] , sortedAttrText ) ;
81
+ fixers . push (
82
+ fixer . replaceTextRange ( [ attr . start , attr . end ] , sortedAttrText )
83
+ ) ;
92
84
} ) ;
85
+ return fixers ;
93
86
} ;
94
87
}
95
88
0 commit comments