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,35 @@ 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
+ return (
40
+ ( identifierA === identifierB ? 0 : identifierA < identifierB ? - 1 : 1 ) *
41
+ ( order === 'asc' ? 1 : - 1 )
42
+ ) ;
43
+ } ) ;
44
+ }
45
+
46
+ function report ( array , type , node , prev , current ) {
34
47
const currentName = getStylePropertyIdentifier ( current ) ;
35
48
const prevName = getStylePropertyIdentifier ( prev ) ;
36
49
context . report ( {
37
50
node,
38
51
message : `Expected ${ type } to be in ${ order } ending order. '${ currentName } ' should be before '${ prevName } '.` ,
39
52
loc : current . key . loc ,
40
53
fix ( fixer ) {
41
- return [
42
- fixer . replaceText ( prev , sourceCode . getText ( current ) ) ,
43
- fixer . replaceText ( current , sourceCode . getText ( prev ) ) ,
44
- ] ;
54
+ const sortedArray = sort ( array ) ;
55
+ return array
56
+ . map ( ( item , i ) => {
57
+ if ( item !== sortedArray [ i ] ) {
58
+ return fixer . replaceText ( item , sourceCode . getText ( sortedArray [ i ] ) ) ;
59
+ }
60
+ return null ;
61
+ } )
62
+ . filter ( Boolean ) ;
45
63
} ,
46
64
} ) ;
47
65
}
@@ -59,7 +77,7 @@ module.exports = (context) => {
59
77
const currentName = getStylePropertyIdentifier ( current ) ;
60
78
61
79
if ( ! isValidOrder ( prevName , currentName ) ) {
62
- return report ( arrayName , node , previous , current ) ;
80
+ return report ( array , arrayName , node , previous , current ) ;
63
81
}
64
82
}
65
83
}
@@ -70,21 +88,27 @@ module.exports = (context) => {
70
88
return ;
71
89
}
72
90
73
- const classDefinitions = getStyleDeclarations ( node ) ;
91
+ const classDefinitionsChunks = getStyleDeclarationsChunks ( node ) ;
74
92
75
93
if ( ! ignoreClassNames ) {
76
- checkIsSorted ( classDefinitions , 'class names' , node ) ;
94
+ classDefinitionsChunks . forEach ( ( classDefinitions ) => {
95
+ checkIsSorted ( classDefinitions , 'class names' , node ) ;
96
+ } ) ;
77
97
}
78
98
79
99
if ( ignoreStyleProperties ) return ;
80
100
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 ) ;
101
+ classDefinitionsChunks . forEach ( ( classDefinitions ) => {
102
+ classDefinitions . forEach ( ( classDefinition ) => {
103
+ const styleProperties = classDefinition . value . properties ;
104
+ if ( ! styleProperties || styleProperties . length < 2 ) {
105
+ return ;
106
+ }
107
+ const stylePropertyChunks = getPropertiesChunks ( styleProperties ) ;
108
+ stylePropertyChunks . forEach ( ( styleProperties ) => {
109
+ checkIsSorted ( styleProperties , 'style properties' , node ) ;
110
+ } ) ;
111
+ } ) ;
88
112
} ) ;
89
113
} ,
90
114
} ;
0 commit comments