Skip to content

Commit 1d18f84

Browse files
authored
Merge pull request #207 from xxsnakerxx/EStyleSheet-support
Added support of other style sheet providers (eg EStyleSheet)
2 parents e351e4f + dbd5d77 commit 1d18f84

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-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

+10-4
Original file line numberDiff line numberDiff line change
@@ -92,14 +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 &&
102-
node.init.callee.object.name === 'StyleSheet'
105+
node.init.callee.object.name &&
106+
objectNames.includes(node.init.callee.object.name)
103107
);
104108
},
105109

@@ -113,9 +117,11 @@ const astHelpers = {
113117
);
114118
},
115119

116-
isStyleSheetDeclaration: function (node) {
120+
isStyleSheetDeclaration: function (node, settings) {
121+
const objectNames = getStyleSheetObjectNames(settings);
122+
117123
return Boolean(
118-
astHelpers.containsStyleSheetObject(node) &&
124+
astHelpers.containsStyleSheetObject(node, objectNames) &&
119125
astHelpers.containsCreateCall(node)
120126
);
121127
},

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)