Skip to content

Commit 12bf72a

Browse files
committed
add spread test case
1 parent d1c72be commit 12bf72a

File tree

3 files changed

+290
-157
lines changed

3 files changed

+290
-157
lines changed

lib/rules/sort-styles.js

Lines changed: 40 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,35 @@ 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+
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) {
3447
const currentName = getStylePropertyIdentifier(current);
3548
const prevName = getStylePropertyIdentifier(prev);
3649
context.report({
3750
node,
3851
message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`,
3952
loc: current.key.loc,
4053
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);
4563
},
4664
});
4765
}
@@ -59,7 +77,7 @@ module.exports = (context) => {
5977
const currentName = getStylePropertyIdentifier(current);
6078

6179
if (!isValidOrder(prevName, currentName)) {
62-
return report(arrayName, node, previous, current);
80+
return report(array, arrayName, node, previous, current);
6381
}
6482
}
6583
}
@@ -70,21 +88,27 @@ module.exports = (context) => {
7088
return;
7189
}
7290

73-
const classDefinitions = getStyleDeclarations(node);
91+
const classDefinitionsChunks = getStyleDeclarationsChunks(node);
7492

7593
if (!ignoreClassNames) {
76-
checkIsSorted(classDefinitions, 'class names', node);
94+
classDefinitionsChunks.forEach((classDefinitions) => {
95+
checkIsSorted(classDefinitions, 'class names', node);
96+
});
7797
}
7898

7999
if (ignoreStyleProperties) return;
80100

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+
});
88112
});
89113
},
90114
};

0 commit comments

Comments
 (0)