Skip to content

Commit 2ce8369

Browse files
committed
feat(rules/sort-styles): Allow using StringLiteral and the simple part of TemplateLiteral
1 parent 35f7e07 commit 2ce8369

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/rules/sort-styles.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
const { astHelpers } = require('../util/stylesheet');
1313

14-
const { getStyleDeclarations, isStyleSheetDeclaration } = astHelpers;
14+
const {
15+
getStyleDeclarations,
16+
getStylePropertyIdentifier,
17+
isStyleSheetDeclaration,
18+
} = astHelpers;
1519

1620
//------------------------------------------------------------------------------
1721
// Rule Definition
@@ -25,8 +29,8 @@ module.exports = (context) => {
2529
const isValidOrder = order === 'asc' ? (a, b) => a <= b : (a, b) => a >= b;
2630

2731
function report(type, node, prev, current) {
28-
const currentName = current.key.name;
29-
const prevName = prev.key.name;
32+
const currentName = getStylePropertyIdentifier(current);
33+
const prevName = getStylePropertyIdentifier(prev);
3034
context.report({
3135
node,
3236
message: `Expected ${type} to be in ${order}ending order. '${currentName}' should be before '${prevName}'.`,
@@ -43,7 +47,10 @@ module.exports = (context) => {
4347
return;
4448
}
4549

46-
if (!isValidOrder(previous.key.name, current.key.name)) {
50+
const prevName = getStylePropertyIdentifier(previous);
51+
const currentName = getStylePropertyIdentifier(current);
52+
53+
if (!isValidOrder(prevName, currentName)) {
4754
return report(arrayName, node, previous, current);
4855
}
4956
}

lib/util/stylesheet.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,35 @@ const astHelpers = {
153153
return [];
154154
},
155155

156+
getExpressionIdentifier: function (node) {
157+
if (node) {
158+
switch (node.type) {
159+
case 'Identifier':
160+
return node.name;
161+
case 'Literal':
162+
return node.value;
163+
case 'TemplateLiteral':
164+
return node.quasis.reduce((result, quasi, index) => result
165+
+ quasi.value.cooked
166+
+ astHelpers.getExpressionIdentifier(node.expressions[index])
167+
, '');
168+
default:
169+
return '';
170+
}
171+
}
172+
173+
return '';
174+
},
175+
176+
getStylePropertyIdentifier: function (node) {
177+
if (
178+
node &&
179+
node.key
180+
) {
181+
return astHelpers.getExpressionIdentifier(node.key);
182+
}
183+
},
184+
156185
isStyleAttribute: function (node) {
157186
return Boolean(
158187
node.type === 'JSXAttribute' &&

0 commit comments

Comments
 (0)