Skip to content

Commit dbd5d77

Browse files
committed
Added react-native/style-sheet-object-names setting
1 parent 0844805 commit dbd5d77

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

lib/rules/no-color-literals.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = Components.detect((context) => {
3232

3333
return {
3434
VariableDeclarator: (node) => {
35-
if (astHelpers.isStyleSheetDeclaration(node)) {
35+
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
3636
const styles = astHelpers.getStyleDeclarations(node);
3737

3838
if (styles) {

lib/rules/no-unused-styles.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module.exports = Components.detect((context, components) => {
4242
},
4343

4444
VariableDeclarator: function (node) {
45-
if (astHelpers.isStyleSheetDeclaration(node)) {
45+
if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
4646
const styleSheetName = astHelpers.getStyleSheetName(node);
4747
const styles = astHelpers.getStyleDeclarations(node);
4848

lib/util/stylesheet.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,18 @@ const getSourceCode = node => currentContent
9292
.getSourceCode(node)
9393
.getText(node);
9494

95+
const getStyleSheetObjectNames = settings =>
96+
settings['react-native/style-sheet-object-names'] || ['StyleSheet'];
97+
9598
const astHelpers = {
96-
containsStyleSheetObject: function (node) {
99+
containsStyleSheetObject: function (node, objectNames) {
97100
return Boolean(
98101
node &&
99102
node.init &&
100103
node.init.callee &&
101104
node.init.callee.object &&
102105
node.init.callee.object.name &&
103-
node.init.callee.object.name.includes('StyleSheet')
106+
objectNames.includes(node.init.callee.object.name)
104107
);
105108
},
106109

@@ -114,9 +117,11 @@ const astHelpers = {
114117
);
115118
},
116119

117-
isStyleSheetDeclaration: function (node) {
120+
isStyleSheetDeclaration: function (node, settings) {
121+
const objectNames = getStyleSheetObjectNames(settings);
122+
118123
return Boolean(
119-
astHelpers.containsStyleSheetObject(node) &&
124+
astHelpers.containsStyleSheetObject(node, objectNames) &&
120125
astHelpers.containsCreateCall(node)
121126
);
122127
},

tests/lib/rules/no-unused-styles.js

+29
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ const tests = {
204204
}
205205
});
206206
`,
207+
}, {
208+
code: `
209+
const styles = OtherStyleSheet.create({
210+
name: {},
211+
});
212+
const Hello = React.createClass({
213+
render: function() {
214+
return <Text textStyle={styles.name}>Hello {this.props.name}</Text>;
215+
}
216+
});
217+
`,
207218
}],
208219

209220
invalid: [{
@@ -250,6 +261,21 @@ const tests = {
250261
errors: [{
251262
message: 'Unused style detected: styles.bar',
252263
}],
264+
}, {
265+
code: `
266+
const styles = OtherStyleSheet.create({
267+
foo: {},
268+
bar: {},
269+
})
270+
class Foo extends React.PureComponent {
271+
render() {
272+
return <View style={styles.foo}/>;
273+
}
274+
}
275+
`,
276+
errors: [{
277+
message: 'Unused style detected: styles.bar',
278+
}],
253279
}],
254280
};
255281

@@ -261,6 +287,9 @@ const config = {
261287
jsx: true,
262288
},
263289
},
290+
settings: {
291+
'react-native/style-sheet-object-names': ['StyleSheet', 'OtherStyleSheet'],
292+
},
264293
};
265294

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

0 commit comments

Comments
 (0)