From 8921b20c73f551ea3e3c14e627dfbb0b1d3e24ae Mon Sep 17 00:00:00 2001 From: Mats Byrkjeland Date: Thu, 14 Mar 2019 16:40:20 +0100 Subject: [PATCH] Find StyleSheet declarations that are not variables Fixes #214 --- lib/rules/no-color-literals.js | 2 +- lib/rules/no-unused-styles.js | 2 +- lib/rules/sort-styles.js | 2 +- lib/util/stylesheet.js | 42 +++++++++++++++++----------------- tests/lib/rules/sort-styles.js | 30 +++++++++++++++++++++++- 5 files changed, 53 insertions(+), 25 deletions(-) diff --git a/lib/rules/no-color-literals.js b/lib/rules/no-color-literals.js index 4443fea..b79e7e6 100644 --- a/lib/rules/no-color-literals.js +++ b/lib/rules/no-color-literals.js @@ -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); diff --git a/lib/rules/no-unused-styles.js b/lib/rules/no-unused-styles.js index 2592727..e9ca742 100644 --- a/lib/rules/no-unused-styles.js +++ b/lib/rules/no-unused-styles.js @@ -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); diff --git a/lib/rules/sort-styles.js b/lib/rules/sort-styles.js index 44d8279..af0b8a3 100644 --- a/lib/rules/sort-styles.js +++ b/lib/rules/sort-styles.js @@ -107,7 +107,7 @@ module.exports = (context) => { } return { - VariableDeclarator: function (node) { + CallExpression: function (node) { if (!isStyleSheetDeclaration(node, context.settings)) { return; } diff --git a/lib/util/stylesheet.js b/lib/util/stylesheet.js index c9b2b14..0b8a6ef 100644 --- a/lib/util/stylesheet.js +++ b/lib/util/stylesheet.js @@ -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' ); }, @@ -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 []; @@ -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) { diff --git a/tests/lib/rules/sort-styles.js b/tests/lib/rules/sort-styles.js index e687600..12241b5 100644 --- a/tests/lib/rules/sort-styles.js +++ b/tests/lib/rules/sort-styles.js @@ -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\'.', + }], + }, ], }; @@ -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);