Skip to content

Commit b5f116b

Browse files
committed
add spread test case
1 parent d1c72be commit b5f116b

File tree

3 files changed

+293
-157
lines changed

3 files changed

+293
-157
lines changed

lib/rules/sort-styles.js

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
const { astHelpers } = require('../util/stylesheet');
1313

1414
const {
15-
getStyleDeclarations,
15+
getStyleDeclarationsChunks,
16+
getPropertiesChunks,
1617
getStylePropertyIdentifier,
1718
isStyleSheetDeclaration,
1819
} = astHelpers;
@@ -30,18 +31,38 @@ module.exports = (context) => {
3031

3132
const sourceCode = context.getSourceCode();
3233

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) {
3450
const currentName = getStylePropertyIdentifier(current);
3551
const prevName = getStylePropertyIdentifier(prev);
3652
context.report({
3753
node,
3854
message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`,
3955
loc: current.key.loc,
4056
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);
4566
},
4667
});
4768
}
@@ -59,7 +80,7 @@ module.exports = (context) => {
5980
const currentName = getStylePropertyIdentifier(current);
6081

6182
if (!isValidOrder(prevName, currentName)) {
62-
return report(arrayName, node, previous, current);
83+
return report(array, arrayName, node, previous, current);
6384
}
6485
}
6586
}
@@ -70,21 +91,27 @@ module.exports = (context) => {
7091
return;
7192
}
7293

73-
const classDefinitions = getStyleDeclarations(node);
94+
const classDefinitionsChunks = getStyleDeclarationsChunks(node);
7495

7596
if (!ignoreClassNames) {
76-
checkIsSorted(classDefinitions, 'class names', node);
97+
classDefinitionsChunks.forEach((classDefinitions) => {
98+
checkIsSorted(classDefinitions, 'class names', node);
99+
});
77100
}
78101

79102
if (ignoreStyleProperties) return;
80103

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+
});
88115
});
89116
},
90117
};

0 commit comments

Comments
 (0)