Skip to content

Commit 54bae56

Browse files
authored
Merge pull request Intellicode#223 from draperunner/sort-styles-default-export
Find StyleSheet declarations that are not variables
2 parents 2a280bb + 8921b20 commit 54bae56

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = Components.detect((context, components) => {
4141
}
4242
},
4343

44-
VariableDeclarator: function (node) {
44+
CallExpression: function (node) {
4545
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
4646
const styleSheetName = astHelpers.getStyleSheetName(node);
4747
const styles = astHelpers.getStyleDeclarations(node);

lib/rules/sort-styles.js

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

109109
return {
110-
VariableDeclarator: function (node) {
110+
CallExpression: function (node) {
111111
if (!isStyleSheetDeclaration(node, context.settings)) {
112112
return;
113113
}

lib/util/stylesheet.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,20 @@ 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.callee &&
114+
node.callee.property &&
115+
node.callee.property.name === 'create'
117116
);
118117
},
119118

@@ -127,20 +126,20 @@ const astHelpers = {
127126
},
128127

129128
getStyleSheetName: function (node) {
130-
if (node && node.id) {
131-
return node.id.name;
129+
if (node && node.parent && node.parent.id) {
130+
return node.parent.id.name;
132131
}
133132
},
134133

135134
getStyleDeclarations: function (node) {
136135
if (
137136
node &&
138-
node.init &&
139-
node.init.arguments &&
140-
node.init.arguments[0] &&
141-
node.init.arguments[0].properties
137+
node.type === 'CallExpression' &&
138+
node.arguments &&
139+
node.arguments[0] &&
140+
node.arguments[0].properties
142141
) {
143-
return node.init.arguments[0].properties.filter(property => property.type === 'Property');
142+
return node.arguments[0].properties.filter(property => property.type === 'Property');
144143
}
145144

146145
return [];
@@ -149,12 +148,13 @@ const astHelpers = {
149148
getStyleDeclarationsChunks: function (node) {
150149
if (
151150
node &&
152-
node.init &&
153-
node.init.arguments &&
154-
node.init.arguments[0] &&
155-
node.init.arguments[0].properties
151+
node.type === 'CallExpression' &&
152+
node.arguments &&
153+
node.arguments[0] &&
154+
node.arguments[0].properties
156155
) {
157-
const properties = node.init.arguments[0].properties;
156+
const properties = node.arguments[0].properties;
157+
158158
const result = [];
159159
let chunk = [];
160160
for (let i = 0; i < properties.length; i += 1) {

tests/lib/rules/sort-styles.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,34 @@ const tests = {
597597
},
598598
],
599599
},
600+
{
601+
code: `
602+
StyleSheet.create({
603+
myClass: {
604+
y: 2,
605+
x: 1,
606+
z: 3,
607+
},
608+
})
609+
`,
610+
errors: [{
611+
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
612+
}],
613+
},
614+
{
615+
code: `
616+
export default StyleSheet.create({
617+
myClass: {
618+
y: 2,
619+
x: 1,
620+
z: 3,
621+
},
622+
})
623+
`,
624+
errors: [{
625+
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
626+
}],
627+
},
600628
],
601629
};
602630

@@ -616,4 +644,4 @@ const config = {
616644
tests.valid.forEach(t => Object.assign(t, config));
617645
tests.invalid.forEach(t => Object.assign(t, config));
618646

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

0 commit comments

Comments
 (0)