Skip to content

Commit a81ab29

Browse files
committed
Find StyleSheet declarations that are not variables
Fixes #214
1 parent 89c3a11 commit a81ab29

File tree

5 files changed

+45
-19
lines changed

5 files changed

+45
-19
lines changed

lib/rules/no-color-literals.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = Components.detect((context) => {
3131
}
3232

3333
return {
34-
VariableDeclarator: (node) => {
34+
CallExpression: (node) => {
3535
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
3636
const styles = astHelpers.getStyleDeclarations(node);
3737

lib/rules/no-unused-styles.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ module.exports = Components.detect((context, components) => {
4242
},
4343

4444
VariableDeclarator: function (node) {
45-
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
45+
if (astHelpers.isStyleSheetDeclaration(node.init, context.settings)) {
4646
const styleSheetName = astHelpers.getStyleSheetName(node);
47-
const styles = astHelpers.getStyleDeclarations(node);
47+
const styles = astHelpers.getStyleDeclarations(node.init);
4848

4949
styleSheets.add(styleSheetName, styles);
5050
}

lib/rules/sort-styles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module.exports = (context) => {
5757
}
5858

5959
return {
60-
VariableDeclarator: function (node) {
60+
CallExpression: function (node) {
6161
if (!isStyleSheetDeclaration(node, context.settings)) {
6262
return;
6363
}

lib/util/stylesheet.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@ const astHelpers = {
9999
containsStyleSheetObject: function (node, objectNames) {
100100
return Boolean(
101101
node &&
102-
node.init &&
103-
node.init.callee &&
104-
node.init.callee.object &&
105-
node.init.callee.object.name &&
106-
objectNames.includes(node.init.callee.object.name)
102+
node.type === 'CallExpression' &&
103+
node.callee &&
104+
node.callee.object &&
105+
node.callee.object.name &&
106+
objectNames.includes(node.callee.object.name)
107107
);
108108
},
109109

110110
containsCreateCall: function (node) {
111111
return Boolean(
112112
node &&
113-
node.init &&
114-
node.init.callee &&
115-
node.init.callee.property &&
116-
node.init.callee.property.name === 'create'
113+
node &&
114+
node.callee &&
115+
node.callee.property &&
116+
node.callee.property.name === 'create'
117117
);
118118
},
119119

@@ -138,13 +138,11 @@ const astHelpers = {
138138
getStyleDeclarations: function (node) {
139139
if (
140140
node &&
141-
node.init &&
142-
node.init.arguments &&
143-
node.init.arguments[0] &&
144-
node.init.arguments[0].properties
141+
node.arguments &&
142+
node.arguments[0] &&
143+
node.arguments[0].properties
145144
) {
146145
return node
147-
.init
148146
.arguments[0]
149147
.properties
150148
.filter(property => property.type === 'Property');

tests/lib/rules/sort-styles.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,34 @@ const tests = {
137137
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
138138
}],
139139
},
140+
{
141+
code: `
142+
StyleSheet.create({
143+
myClass: {
144+
y: 2,
145+
x: 1,
146+
z: 3,
147+
},
148+
})
149+
`,
150+
errors: [{
151+
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
152+
}],
153+
},
154+
{
155+
code: `
156+
export default StyleSheet.create({
157+
myClass: {
158+
y: 2,
159+
x: 1,
160+
z: 3,
161+
},
162+
})
163+
`,
164+
errors: [{
165+
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
166+
}],
167+
},
140168
{
141169
code: `
142170
const styles = StyleSheet.create({
@@ -208,4 +236,4 @@ const config = {
208236
tests.valid.forEach(t => Object.assign(t, config));
209237
tests.invalid.forEach(t => Object.assign(t, config));
210238

211-
ruleTester.run('no-unused-styles', rule, tests);
239+
ruleTester.run('sort-styles', rule, tests);

0 commit comments

Comments
 (0)