Skip to content
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

Add EStyleSheet variables support #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -18,7 +18,7 @@ module.exports = Components.detect((context) => {
function reportColorLiterals(colorLiterals) {
if (colorLiterals) {
colorLiterals.forEach((style) => {
if (style) {
if (style && !astHelpers.isEStyleSheetVariable(style.expression)) {
const expression = util.inspect(style.expression);
context.report({
node: style.node,
Expand Down
6 changes: 6 additions & 0 deletions lib/util/stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,12 @@ const astHelpers = {
}
return false;
},

isEStyleSheetVariable: function (expression) {
return Object.values(expression).every(
value => typeof value === 'string' && value.startsWith('$')
);
},
};

module.exports.astHelpers = astHelpers;
Expand Down
28 changes: 28 additions & 0 deletions tests/lib/rules/no-color-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ const tests = {
}
`,
},
{
code: `
const styles = EStyleSheet.create({
$red: 'red',
$blue: 'blue',
style1: {
color: '$red',
},
style2: {
color: '$blue',
}
});
export default class MyComponent extends Component {
render() {
const isDanger = true;
return <View
style={[styles.style1, isDanger ? styles.style1 : styles.style2]}
/>;
}
}
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -187,6 +209,12 @@ const config = {
jsx: true,
},
},
settings: {
'react-native/style-sheet-object-names': [
'StyleSheet',
'EStyleSheet',
],
},
};

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