eslint-plugin-react-native icon indicating copy to clipboard operation
eslint-plugin-react-native copied to clipboard

Rules related to styles object don't work for all definition styles

Open prithsharma opened this issue 6 years ago • 3 comments

Version: 3.2.1

I have noticed that rules like no-unused-styles and no-color-literals don't work if styles are defined as

let styles;
export default SomeComponent() {
  ...
}

styles = StyleSheet.create({
  ...
});

They start working if the definition is made as

export default SomeComponent() {
  ...
}

const styles = StyleSheet.create({
  ...
});

I want to prefer keeping the style definitions at the bottom of the file(as others also expressed in other issues) but not at the expense of disabling the no-use-before-define rule for variables(which is mentioned as a workaround in another issue).

Is this something that can be fixed? I would like to contribute, but might need some hints.

prithsharma avatar Jun 22 '18 21:06 prithsharma

Hi @prithsharma,

This is possible, I guess you could modify the rule to handle variable assignments a bit more flexible

Intellicode avatar Jul 02 '18 21:07 Intellicode

Another example where this happens is if you define them as a static variable:

class MyComponent extends Component {
  static styles = StyleSheet.create({...});
}

this won't be checked also

mloureiro avatar Mar 15 '19 13:03 mloureiro

Another example is if you have it defined on a class, that uses props to determine styles

class MyComponent extends Component {
  styles = StyleSheet.create({
    something: {
       awesome: this.props.isAwesome ? 'orange' : 'black',
    }
  });
}

Screen Shot 2019-10-01 at 12 27 08 PM

tjbenton avatar Oct 01 '19 16:10 tjbenton