Skip to content

Added support of other style sheet providers (eg EStyleSheet) #207

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 3 commits into from
Nov 22, 2018
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 @@ -32,7 +32,7 @@ module.exports = Components.detect((context) => {

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

if (styles) {
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 @@ -42,7 +42,7 @@ module.exports = Components.detect((context, components) => {
},

VariableDeclarator: function (node) {
if (astHelpers.isStyleSheetDeclaration(node)) {
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
const styleSheetName = astHelpers.getStyleSheetName(node);
const styles = astHelpers.getStyleDeclarations(node);

Expand Down
14 changes: 10 additions & 4 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,18 @@ const getSourceCode = node => currentContent
.getSourceCode(node)
.getText(node);

const getStyleSheetObjectNames = settings =>
settings['react-native/style-sheet-object-names'] || ['StyleSheet'];

const astHelpers = {
containsStyleSheetObject: function (node) {
containsStyleSheetObject: function (node, objectNames) {
return Boolean(
node &&
node.init &&
node.init.callee &&
node.init.callee.object &&
node.init.callee.object.name === 'StyleSheet'
node.init.callee.object.name &&
objectNames.includes(node.init.callee.object.name)
);
},

Expand All @@ -113,9 +117,11 @@ const astHelpers = {
);
},

isStyleSheetDeclaration: function (node) {
isStyleSheetDeclaration: function (node, settings) {
const objectNames = getStyleSheetObjectNames(settings);

return Boolean(
astHelpers.containsStyleSheetObject(node) &&
astHelpers.containsStyleSheetObject(node, objectNames) &&
astHelpers.containsCreateCall(node)
);
},
Expand Down
29 changes: 29 additions & 0 deletions tests/lib/rules/no-unused-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ const tests = {
}
});
`,
}, {
code: `
const styles = OtherStyleSheet.create({
name: {},
});
const Hello = React.createClass({
render: function() {
return <Text textStyle={styles.name}>Hello {this.props.name}</Text>;
}
});
`,
}],

invalid: [{
Expand Down Expand Up @@ -250,6 +261,21 @@ const tests = {
errors: [{
message: 'Unused style detected: styles.bar',
}],
}, {
code: `
const styles = OtherStyleSheet.create({
foo: {},
bar: {},
})
class Foo extends React.PureComponent {
render() {
return <View style={styles.foo}/>;
}
}
`,
errors: [{
message: 'Unused style detected: styles.bar',
}],
}],
};

Expand All @@ -261,6 +287,9 @@ const config = {
jsx: true,
},
},
settings: {
'react-native/style-sheet-object-names': ['StyleSheet', 'OtherStyleSheet'],
},
};

tests.valid.forEach(t => Object.assign(t, config));
Expand Down