Skip to content
Open
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
57 changes: 54 additions & 3 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,16 @@ const astHelpers = {
node &&
node.type === 'CallExpression' &&
node.callee &&
node.callee.object &&
node.callee.object.name &&
objectNames.includes(node.callee.object.name)
node.callee.object && ((
node.callee.object.type === 'Identifier' &&
node.callee.object.name &&
objectNames.includes(node.callee.object.name)
) || (
node.callee.object.type === 'MemberExpression' &&
node.callee.object.object.type === 'Identifier' &&
objectNames.includes(node.callee.object.property.name) &&
astHelpers.getImportSource(node.callee.object.object) === 'react-native'
))
);
},

Expand Down Expand Up @@ -463,6 +470,50 @@ const astHelpers = {
}
},

getRoot: function (node) {
let currentNode = node;
while (currentNode && currentNode.parent) {
currentNode = currentNode.parent;
}

return currentNode;
},

getImportVariable: function (node) {
if (node && node.type === 'ImportDeclaration') {
for (let i = 0; i < node.specifiers.length; i += 1) {
const specifier = node.specifiers[i];
if (
specifier && (
specifier.type === 'ImportDefaultSpecifier' ||
specifier.type === 'ImportNamespaceSpecifier'
) &&
specifier.local &&
specifier.local.type === 'Identifier'
) {
return specifier.local.name;
}
}
}
},

getImportSource: function (node) {
if (node && node.type === 'Identifier' && node.name) {
const rootNode = astHelpers.getRoot(node);
if (rootNode && rootNode.body) {
for (let i = 0; i < rootNode.body.length; i += 1) {
const bodyNode = rootNode.body[i];
if (
astHelpers.getImportVariable(bodyNode) === node.name &&
bodyNode.source && bodyNode.source.type === 'Literal'
) {
return bodyNode.source.value;
}
}
}
}
},

getPotentialStyleReferenceFromMemberExpression: function (node) {
if (
node &&
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,31 @@ const tests = {
}
});
`,
}, {
code: `
import RN from 'react-native';
const styles = RN.StyleSheet.create({
name: {}
});
const Hello = React.createClass({
render: function() {
return <Text textStyle={styles.name}>Hello {this.props.name}</Text>;
}
});
`,
}, {
code: `
import React from 'react';
import RN from 'react-native';
const styles = RN.StyleSheet.create({
name: {}
});
const Hello = React.createClass({
render: function() {
return <Text textStyle={styles.name}>Hello {this.props.name}</Text>;
}
});
`,
}],

invalid: [{
Expand Down Expand Up @@ -276,6 +301,22 @@ const tests = {
errors: [{
message: 'Unused style detected: styles.bar',
}],
}, {
code: `
import RN from 'react-native';
const styles = RN.StyleSheet.create({
name: {}
});
const Hello = React.createClass({
render: function() {
return <Text>Hello {this.props.name}</Text>;
}
});
`,
errors: [{
message: 'Unused style detected: styles.name',
}],

}],
};

Expand Down