Install ESLint either locally or globally.
$ npm install eslintIf you installed ESLint globally, you have to install React plugin globally too. Otherwise, install it locally.
$ npm install eslint-plugin-jsx-control-statementsAdd plugins section and specify ESLint-plugin-JSX-Control-Statements as a plugin and plugin:jsx-control-statements/recommended
to "extends"
{
"plugins": [
"jsx-control-statements"
],
"extends": ["plugin:jsx-control-statements/recommended"]
}If it is not already the case you must also configure ESLint to support JSX.
{
"ecmaFeatures": {
"jsx": true
}
}The plugin comes with a number of rules and an environment that sets the control statements (<If>, <For> etc) as global variables:
{
"rules": {
"jsx-control-statements/jsx-choose-not-empty": 1,
"jsx-control-statements/jsx-for-require-each": 1,
"jsx-control-statements/jsx-for-require-of": 1,
"jsx-control-statements/jsx-if-require-condition": 1,
"jsx-control-statements/jsx-otherwise-once-last": 1,
"jsx-control-statements/jsx-use-if-tag": 1,
"jsx-control-statements/jsx-when-require-condition": 1,
"jsx-control-statements/jsx-jcs-no-undef": 1,
"no-undef": 0 // Replace this with jsx-jcs-no-undef
},
env: {
"jsx-control-statements/jsx-control-statements": true
}
}- jsx-choose-not-empty: Warn when
Choosetag is empty or does not have at least oneWhentag as child. - jsx-for-require-each: Warn if
Fortag is missingeachattribute. And also marks the variable as defined. - jsx-for-require-of: Warn if
Fortag is missingofattribute. - jsx-if-require-condition: Warn if
Iftag is missingconditionattribute. - jsx-otherwise-once-last: Warn when
Otherwisetag is used more than once insideChooseand is not last child. - jsx-use-if-tag: Use
Iftag instead of ternary operator. - jsx-when-require-condition: Warn if
Whentag is missingconditionattribute. - jsx-jcs-no-undef: Replaces the built-in
no-undefrule with one that is tolerant of variables expressed in<For>(eachandindexattributes). Note that to stop getting errors fromno-undefyou have to turn it off and this on.
Thanks to @yannickcr for his awesome eslint-plugin-react.
MIT License. Copyright(c) Vivek Kumar Bansal