Skip to content

Commit 8210dff

Browse files
committed
Fix bug in sort-styles when props included shorthands
If the style properties had a 'isEitherShorthand' case, it would return out of the whole checkIsSorted function instead of continuing the for loop. This caused some properties to not be reported as unsorted. It would be sufficient to replace `return` with `continue`, but there was a ESLint rule disallowing the usage of continue
1 parent 27193ec commit 8210dff

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/rules/sort-styles.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,9 @@ module.exports = (context) => {
9292
const prevName = getStylePropertyIdentifier(previous);
9393
const currentName = getStylePropertyIdentifier(current);
9494

95-
if (
96-
arrayName === 'style properties'
97-
&& isEitherShortHand(prevName, currentName)
98-
) {
99-
return;
100-
}
95+
const oneIsShorthandForTheOther = arrayName === 'style properties' && isEitherShortHand(prevName, currentName);
10196

102-
if (!isValidOrder(prevName, currentName)) {
97+
if (!oneIsShorthandForTheOther && !isValidOrder(prevName, currentName)) {
10398
return report(array, arrayName, node, previous, current);
10499
}
105100
}

tests/lib/rules/sort-styles.js

+26
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ const tests = {
190190
},
191191
],
192192
invalid: [
193+
{
194+
code: `
195+
const styles = StyleSheet.create({
196+
myClass: {
197+
flex: 1,
198+
flexDirection: 'row',
199+
backgroundColor: 'red',
200+
},
201+
})
202+
`,
203+
output: `
204+
const styles = StyleSheet.create({
205+
myClass: {
206+
backgroundColor: 'red',
207+
flex: 1,
208+
flexDirection: 'row',
209+
},
210+
})
211+
`,
212+
errors: [
213+
{
214+
message:
215+
"Expected style properties to be in ascending order. 'backgroundColor' should be before 'flexDirection'.",
216+
},
217+
],
218+
},
193219
{
194220
code: `
195221
const styles = StyleSheet.create({

0 commit comments

Comments
 (0)