diff --git a/lib/rules/no-color-literals.js b/lib/rules/no-color-literals.js
index a0d68e6..4443fea 100644
--- a/lib/rules/no-color-literals.js
+++ b/lib/rules/no-color-literals.js
@@ -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) {
diff --git a/lib/rules/no-unused-styles.js b/lib/rules/no-unused-styles.js
index 8a33f5c..2592727 100644
--- a/lib/rules/no-unused-styles.js
+++ b/lib/rules/no-unused-styles.js
@@ -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);
diff --git a/lib/util/stylesheet.js b/lib/util/stylesheet.js
index 46dc595..238fdf2 100644
--- a/lib/util/stylesheet.js
+++ b/lib/util/stylesheet.js
@@ -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)
);
},
@@ -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)
);
},
diff --git a/tests/lib/rules/no-unused-styles.js b/tests/lib/rules/no-unused-styles.js
index 15904bf..c99d3e6 100644
--- a/tests/lib/rules/no-unused-styles.js
+++ b/tests/lib/rules/no-unused-styles.js
@@ -204,6 +204,17 @@ const tests = {
}
});
`,
+ }, {
+ code: `
+ const styles = OtherStyleSheet.create({
+ name: {},
+ });
+ const Hello = React.createClass({
+ render: function() {
+ return Hello {this.props.name};
+ }
+ });
+ `,
}],
invalid: [{
@@ -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 ;
+ }
+ }
+ `,
+ errors: [{
+ message: 'Unused style detected: styles.bar',
+ }],
}],
};
@@ -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));