Skip to content

Find StyleSheet declarations that are not variables #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/no-color-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = Components.detect((context) => {
}

return {
VariableDeclarator: (node) => {
CallExpression: (node) => {
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
const styles = astHelpers.getStyleDeclarations(node);

Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = Components.detect((context, components) => {
}
},

VariableDeclarator: function (node) {
CallExpression: function (node) {
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
const styleSheetName = astHelpers.getStyleSheetName(node);
const styles = astHelpers.getStyleDeclarations(node);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/sort-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module.exports = (context) => {
}

return {
VariableDeclarator: function (node) {
CallExpression: function (node) {
if (!isStyleSheetDeclaration(node, context.settings)) {
return;
}
Expand Down
42 changes: 21 additions & 21 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,20 @@ const astHelpers = {
containsStyleSheetObject: function (node, objectNames) {
return Boolean(
node &&
node.init &&
node.init.callee &&
node.init.callee.object &&
node.init.callee.object.name &&
objectNames.includes(node.init.callee.object.name)
node.type === 'CallExpression' &&
node.callee &&
node.callee.object &&
node.callee.object.name &&
objectNames.includes(node.callee.object.name)
);
},

containsCreateCall: function (node) {
return Boolean(
node &&
node.init &&
node.init.callee &&
node.init.callee.property &&
node.init.callee.property.name === 'create'
node.callee &&
node.callee.property &&
node.callee.property.name === 'create'
);
},

Expand All @@ -127,20 +126,20 @@ const astHelpers = {
},

getStyleSheetName: function (node) {
if (node && node.id) {
return node.id.name;
if (node && node.parent && node.parent.id) {
return node.parent.id.name;
}
},

getStyleDeclarations: function (node) {
if (
node &&
node.init &&
node.init.arguments &&
node.init.arguments[0] &&
node.init.arguments[0].properties
node.type === 'CallExpression' &&
node.arguments &&
node.arguments[0] &&
node.arguments[0].properties
) {
return node.init.arguments[0].properties.filter(property => property.type === 'Property');
return node.arguments[0].properties.filter(property => property.type === 'Property');
}

return [];
Expand All @@ -149,12 +148,13 @@ const astHelpers = {
getStyleDeclarationsChunks: function (node) {
if (
node &&
node.init &&
node.init.arguments &&
node.init.arguments[0] &&
node.init.arguments[0].properties
node.type === 'CallExpression' &&
node.arguments &&
node.arguments[0] &&
node.arguments[0].properties
) {
const properties = node.init.arguments[0].properties;
const properties = node.arguments[0].properties;

const result = [];
let chunk = [];
for (let i = 0; i < properties.length; i += 1) {
Expand Down
30 changes: 29 additions & 1 deletion tests/lib/rules/sort-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,34 @@ const tests = {
},
],
},
{
code: `
StyleSheet.create({
myClass: {
y: 2,
x: 1,
z: 3,
},
})
`,
errors: [{
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
}],
},
{
code: `
export default StyleSheet.create({
myClass: {
y: 2,
x: 1,
z: 3,
},
})
`,
errors: [{
message: 'Expected style properties to be in ascending order. \'x\' should be before \'y\'.',
}],
},
],
};

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

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