Skip to content

Add no-unused-styles default import support #228

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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' &&
astHelpers.getImportSource(node.callee.object.object) === 'react-native' &&
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can potentially be heavy operation. I wonder if it should be cached in some way. Also, maybe it should be moved as the last check?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did bump into some performance issues with unnecessary import checks. Decided to move it as the last check to improve performance.

objectNames.includes(node.callee.object.property.name)
))
);
},

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; node.specifiers; 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; 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