Skip to content
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
16 changes: 7 additions & 9 deletions lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ module.exports = Components.detect((context, components) => {
}

return {
MemberExpression: function (node) {
const styleRef = astHelpers.getPotentialStyleReferenceFromMemberExpression(node);
if (styleRef) {
styleReferences.add(styleRef);
}
},

VariableDeclarator: function (node) {
if (astHelpers.isStyleSheetDeclaration(node)) {
const styleSheetName = astHelpers.getStyleSheetName(node);
Expand All @@ -41,15 +48,6 @@ module.exports = Components.detect((context, components) => {
}
},

JSXAttribute: function (node) {
if (astHelpers.isStyleAttribute(node)) {
const styles = astHelpers.collectStyleReferences(node.value);
styles.forEach((style) => {
styleReferences.add(style);
});
}
},

'Program:exit': function () {
const list = components.all();
if (Object.keys(list).length > 0) {
Expand Down
37 changes: 15 additions & 22 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,6 @@ const astHelpers = {
);
},

collectStyleReferences: function (node) {
if (astHelpers.hasArrayOfStyleReferences(node)) {
const styleReferenceContainers = node
.expression
.elements;

return astHelpers.collectStyleReferencesFromContainers(
styleReferenceContainers
);
}

return astHelpers.getStyleReferenceFromNode(node.expression);
},

collectStyleObjectExpressions: function (node, context) {
currentContent = context;
if (astHelpers.hasArrayOfStyleReferences(node)) {
Expand Down Expand Up @@ -201,14 +187,6 @@ const astHelpers = {
return astHelpers.getColorLiteralsFromNode(node.expression);
},

collectStyleReferencesFromContainers: function (nodes) {
let styleReferences = [];
nodes.forEach(node => {
styleReferences = styleReferences.concat(astHelpers.getStyleReferenceFromNode(node));
});
return styleReferences;
},

collectStyleObjectExpressionFromContainers: function (nodes) {
let objectExpressions = [];
nodes.forEach(node => {
Expand Down Expand Up @@ -400,6 +378,21 @@ const astHelpers = {
return node.property.name;
}
},

getPotentialStyleReferenceFromMemberExpression: function (node) {
if (
node &&
node.object &&
node.object.type === 'Identifier' &&
node.object.name &&
node.property &&
node.property.type === 'Identifier' &&
node.property.name &&
node.parent.type !== 'MemberExpression'
) {
return [node.object.name, node.property.name].join('.');
}
},
};

module.exports.astHelpers = astHelpers;
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,31 @@ ruleTester.run('no-unused-styles', rule, {
classes: true,
jsx: true,
},
}, {
code: [
'const Hello = React.createClass({',
' getInitialState: function() {',
' return { condition: true }; ',
' },',
' render: function() {',
' const myStyle = this.state.condition ? styles.text : styles.text2;',
' return (',
' <Text style={myStyle}>',
' Hello {this.props.name}',
' </Text>',
' );',
' }',
'});',
'const styles = StyleSheet.create({',
' text: {},',
' text2: {},',
'});',
].join('\n'),
parser: 'babel-eslint',
ecmaFeatures: {
classes: true,
jsx: true,
},
}],

invalid: [{
Expand Down