Skip to content

Commit 89c3a11

Browse files
authored
Merge pull request #215 from shirohana/fix/literal-stylesheet-property
Allow using StringLiteral as properties of StyleSheet
2 parents 0b0f010 + 9e13485 commit 89c3a11

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

lib/rules/sort-styles.js

+11-4
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

+29
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' &&

tests/lib/rules/sort-styles.js

+35
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,41 @@ const tests = {
154154
message: 'Expected class names to be in ascending order. \'a\' should be before \'b\'.',
155155
}],
156156
},
157+
{
158+
code: `
159+
const styles = StyleSheet.create({
160+
'b': {},
161+
'a': {},
162+
})
163+
`,
164+
errors: [{
165+
message: 'Expected class names to be in ascending order. \'a\' should be before \'b\'.',
166+
}],
167+
},
168+
{
169+
code: `
170+
const styles = StyleSheet.create({
171+
['b']: {},
172+
[\`a\`]: {},
173+
})
174+
`,
175+
errors: [{
176+
message: 'Expected class names to be in ascending order. \'a\' should be before \'b\'.',
177+
}],
178+
},
179+
{
180+
code: `
181+
const a = 'a';
182+
const b = 'b';
183+
const styles = StyleSheet.create({
184+
[\`\${a}-\${b}-b\`]: {},
185+
[\`a-\${b}-a\`]: {},
186+
})
187+
`,
188+
errors: [{
189+
message: 'Expected class names to be in ascending order. \'a-b-a\' should be before \'a-b-b\'.',
190+
}],
191+
},
157192
],
158193
};
159194

0 commit comments

Comments
 (0)