12
12
const { astHelpers } = require ( '../util/stylesheet' ) ;
13
13
14
14
const {
15
- getStyleDeclarations,
15
+ getStyleDeclarationsChunks,
16
+ getPropertiesChunks,
16
17
getStylePropertyIdentifier,
17
18
isStyleSheetDeclaration,
18
19
} = astHelpers ;
@@ -28,13 +29,41 @@ module.exports = (context) => {
28
29
const ignoreStyleProperties = options . ignoreStyleProperties ;
29
30
const isValidOrder = order === 'asc' ? ( a , b ) => a <= b : ( a , b ) => a >= b ;
30
31
31
- function report ( type , node , prev , current ) {
32
+ const sourceCode = context . getSourceCode ( ) ;
33
+
34
+ function sort ( array ) {
35
+ return [ ] . concat ( array ) . sort ( ( a , b ) => {
36
+ const identifierA = getStylePropertyIdentifier ( a ) ;
37
+ const identifierB = getStylePropertyIdentifier ( b ) ;
38
+
39
+ let sortOrder = 0 ;
40
+ if ( identifierA < identifierB ) {
41
+ sortOrder = - 1 ;
42
+ } else if ( identifierA > identifierB ) {
43
+ sortOrder = 1 ;
44
+ }
45
+ return sortOrder * ( order === 'asc' ? 1 : - 1 ) ;
46
+ } ) ;
47
+ }
48
+
49
+ function report ( array , type , node , prev , current ) {
32
50
const currentName = getStylePropertyIdentifier ( current ) ;
33
51
const prevName = getStylePropertyIdentifier ( prev ) ;
34
52
context . report ( {
35
53
node,
36
54
message : `Expected ${ type } to be in ${ order } ending order. '${ currentName } ' should be before '${ prevName } '.` ,
37
55
loc : current . key . loc ,
56
+ fix ( fixer ) {
57
+ const sortedArray = sort ( array ) ;
58
+ return array
59
+ . map ( ( item , i ) => {
60
+ if ( item !== sortedArray [ i ] ) {
61
+ return fixer . replaceText ( item , sourceCode . getText ( sortedArray [ i ] ) ) ;
62
+ }
63
+ return null ;
64
+ } )
65
+ . filter ( Boolean ) ;
66
+ } ,
38
67
} ) ;
39
68
}
40
69
@@ -51,7 +80,7 @@ module.exports = (context) => {
51
80
const currentName = getStylePropertyIdentifier ( current ) ;
52
81
53
82
if ( ! isValidOrder ( prevName , currentName ) ) {
54
- return report ( arrayName , node , previous , current ) ;
83
+ return report ( array , arrayName , node , previous , current ) ;
55
84
}
56
85
}
57
86
}
@@ -62,26 +91,33 @@ module.exports = (context) => {
62
91
return ;
63
92
}
64
93
65
- const classDefinitions = getStyleDeclarations ( node ) ;
94
+ const classDefinitionsChunks = getStyleDeclarationsChunks ( node ) ;
66
95
67
96
if ( ! ignoreClassNames ) {
68
- checkIsSorted ( classDefinitions , 'class names' , node ) ;
97
+ classDefinitionsChunks . forEach ( ( classDefinitions ) => {
98
+ checkIsSorted ( classDefinitions , 'class names' , node ) ;
99
+ } ) ;
69
100
}
70
101
71
102
if ( ignoreStyleProperties ) return ;
72
103
73
- classDefinitions . forEach ( ( classDefinition ) => {
74
- const styleProperties = classDefinition . value . properties ;
75
- if ( ! styleProperties || styleProperties . length < 2 ) {
76
- return ;
77
- }
78
-
79
- checkIsSorted ( styleProperties , 'style properties' , node ) ;
104
+ classDefinitionsChunks . forEach ( ( classDefinitions ) => {
105
+ classDefinitions . forEach ( ( classDefinition ) => {
106
+ const styleProperties = classDefinition . value . properties ;
107
+ if ( ! styleProperties || styleProperties . length < 2 ) {
108
+ return ;
109
+ }
110
+ const stylePropertyChunks = getPropertiesChunks ( styleProperties ) ;
111
+ stylePropertyChunks . forEach ( ( stylePropertyChunk ) => {
112
+ checkIsSorted ( stylePropertyChunk , 'style properties' , node ) ;
113
+ } ) ;
114
+ } ) ;
80
115
} ) ;
81
116
} ,
82
117
} ;
83
118
} ;
84
119
120
+ module . exports . fixable = 'code' ;
85
121
module . exports . schema = [
86
122
{
87
123
enum : [ 'asc' , 'desc' ] ,
0 commit comments