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 ;
@@ -30,18 +31,38 @@ module.exports = (context) => {
30
31
31
32
const sourceCode = context . getSourceCode ( ) ;
32
33
33
- function report ( type , node , prev , current ) {
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 ) {
34
50
const currentName = getStylePropertyIdentifier ( current ) ;
35
51
const prevName = getStylePropertyIdentifier ( prev ) ;
36
52
context . report ( {
37
53
node,
38
54
message : `Expected ${ type } to be in ${ order } ending order. '${ currentName } ' should be before '${ prevName } '.` ,
39
55
loc : current . key . loc ,
40
56
fix ( fixer ) {
41
- return [
42
- fixer . replaceText ( prev , sourceCode . getText ( current ) ) ,
43
- fixer . replaceText ( current , sourceCode . getText ( prev ) ) ,
44
- ] ;
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 ) ;
45
66
} ,
46
67
} ) ;
47
68
}
@@ -59,7 +80,7 @@ module.exports = (context) => {
59
80
const currentName = getStylePropertyIdentifier ( current ) ;
60
81
61
82
if ( ! isValidOrder ( prevName , currentName ) ) {
62
- return report ( arrayName , node , previous , current ) ;
83
+ return report ( array , arrayName , node , previous , current ) ;
63
84
}
64
85
}
65
86
}
@@ -70,21 +91,27 @@ module.exports = (context) => {
70
91
return ;
71
92
}
72
93
73
- const classDefinitions = getStyleDeclarations ( node ) ;
94
+ const classDefinitionsChunks = getStyleDeclarationsChunks ( node ) ;
74
95
75
96
if ( ! ignoreClassNames ) {
76
- checkIsSorted ( classDefinitions , 'class names' , node ) ;
97
+ classDefinitionsChunks . forEach ( ( classDefinitions ) => {
98
+ checkIsSorted ( classDefinitions , 'class names' , node ) ;
99
+ } ) ;
77
100
}
78
101
79
102
if ( ignoreStyleProperties ) return ;
80
103
81
- classDefinitions . forEach ( ( classDefinition ) => {
82
- const styleProperties = classDefinition . value . properties ;
83
- if ( ! styleProperties || styleProperties . length < 2 ) {
84
- return ;
85
- }
86
-
87
- 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
+ } ) ;
88
115
} ) ;
89
116
} ,
90
117
} ;
0 commit comments