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

magic numbers inside stylesheet

Open enapupe opened this issue 9 years ago • 9 comments

Hello, I was wondering how to properly setup this plugin in order I stop getting warnings like 9:15 warning No magic number: 20 no-magic-numbers within styles block such as:

const styles = StyleSheet.create({
  container: {
    flex: 1
    , justifyContent: 'center'
    , alignItems: 'center'
    , backgroundColor: '#F5FCFF'
  }
  , welcome: {
    fontSize: 20
    , textAlign: 'center'
    , margin: 10
  }
  , instructions: {
    textAlign: 'center'
    , color: '#333333'
    , marginBottom: 5
  }
})

This plugin offers any solution to this matter?

enapupe avatar Jun 28 '16 14:06 enapupe

The warnings are caused by a rule that is not part of this plugin, you can disable that particular rule for each stylesheet block with inline eslint-disable comments or you can disable this rule in your .eslintrc.

If a rule like this would be defined in this plugin it could perform similar checks except in white listed scenario's such as certain style rules.

Intellicode avatar Jun 28 '16 20:06 Intellicode

So you are saying the only options are adding comments OR disabling the magic number verification completely? I don't have much knowledge of eslint env..

enapupe avatar Jun 28 '16 20:06 enapupe

The issue is, using no-magic-numbers is painful with react-native stylesheets. Do you think this plugin could address this issue?

enapupe avatar Jun 29 '16 20:06 enapupe

It might be fruitful to create a no-magic-numbers in this repo, like intellicode said.

tomauty avatar Jun 29 '16 21:06 tomauty

I don't know very much about eslint stuff and hierarchy.. @tomauty do you see any way to accomplish this kind of validation? Could you point me some directions? Ty!

enapupe avatar Jun 29 '16 21:06 enapupe

@enapupe it takes a bit of practice to get the hang of writing lint rules, but you can piggy-back on someone else's work by trying to modify an existing rule. The first thing is to see where the no-magic-numbers rule comes from and see if you can see a place in the code you can modify it to ignore StyleSheet rules. You can also look in this repo to see how @Intellicode determines a node is part of a StyleSheet.

Essentially you'll need a special version of the no-magic-numbers rule that makes the exceptions you want, and use that instead of the original.

aarongreenwald avatar Jun 30 '16 11:06 aarongreenwald

@aarongreenwald thank you very much for such explanation. So I'd probably develop a small plugin which extends no-magic-numbers.. I like the approach, thanks for sharing !

enapupe avatar Jun 30 '16 14:06 enapupe

@enapupe Actually, if you don't mind sharing it I'm up for including it in the plugin.

Intellicode avatar Jun 30 '16 20:06 Intellicode

I wouldn't mind sharing, but right now I already have too much new software to study, so, I'm not gonna do this anytime soon..

enapupe avatar Jun 30 '16 20:06 enapupe