26

I want configure ESLint for check my JSX files but my configuration doesn't work. What is the correct way?

.eslintrc.js:

module.exports = {
  extends: 'airbnb',
  parserOptions: {
    ecmaFeatures: {
      jsx: true
    }
  },
  plugins: [
    'react',
  ],
  parser: 'babel-eslint'
};
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
SaroVin
  • 1,583
  • 3
  • 23
  • 46

3 Answers3

34

In order to lint JSX files configuration alone is not enough. Your configuration looks fine (although you probably don't need babel-eslint, unless you are using features that are lower than stage 4 proposal). By default ESLint will only process .js files. You have to tell it that you want to process .jsx files as well by using --ext flag on command line: eslint --ext .jsx .

Ilya Volodin
  • 10,929
  • 2
  • 45
  • 48
13

I found an alternative to the original answer so that you don't have to specify the arguments when running eslint command.

Option 1

Use the command eslint . --ext .js --ext .jsx

Option 2

Specify overrides in your eslint config...

//.eslintrc
{
  ...
  "overrides": [
    {
      "files": ["*.jsx", "*.js"]
    }
  ],
  ...
}
user2220967
  • 133
  • 1
  • 3
  • 3
    Option 2 only works if all of the rules are placed within this override object, which is not useful if you're extending other configurations. – Chase Sandmann Mar 24 '21 at 00:11
5

I would recommend you to use also the plugin for eslint https://github.com/yannickcr/eslint-plugin-react

You could do this example config:

'use strict';

module.exports = {
  'extends': [ 'plugin:react/recommended' ],
  'parserOptions': {
    'ecmaFeatures': {
      'jsx': true
    }
  },
  'plugins': [ 'react' ],
  'rules': {

    // React
    'react/forbid-prop-types': 'error',
    'react/no-multi-comp': [ 'error', { 'ignoreStateless': true }],
    'react/no-set-state': 'error',
    'react/no-string-refs': 'error',
    'react/prefer-es6-class': 'error',
    'react/prefer-stateless-function': 'error',
    'react/require-extension': 'error',
    'react/require-render-return': 'error',
    'react/self-closing-comp': 'error',
    'react/sort-comp': 'error',
    'react/sort-prop-types': 'error',
    'react/wrap-multilines': 'error',

    // JSX
    'react/jsx-boolean-value': 'error',
    'react/jsx-closing-bracket-location': 'error',
    'react/jsx-curly-spacing': [ 'error', 'always' ],
    'react/jsx-equals-spacing': 'error',
    'react/jsx-first-prop-new-line': 'error',
    'react/jsx-handler-names': 'error',
    'react/jsx-indent-props': [ 'error', 2 ],
    'react/jsx-indent': [ 'error', 2 ],
    'react/jsx-key': 'error',
    'react/jsx-max-props-per-line': [ 'error', { 'maximum': 3 }],
    'react/jsx-no-bind': 'error',
    'react/jsx-no-literals': 'off',
    'react/jsx-no-target-blank': 'error',
    'react/jsx-pascal-case': 'error',
    'react/jsx-sort-props': 'error',
    'react/jsx-space-before-closing': 'error'
  }
};
fernandopasik
  • 9,565
  • 7
  • 48
  • 55
  • Is this `eslint` config file enough good for a little project with 3 developers along side ? – AmerllicA Jul 06 '18 at 04:27
  • 1
    Yes it could be suitable, or you could use a config like https://www.npmjs.com/package/eslint-config-airbnb which comes with a lot of good stuff already out of the box – fernandopasik Jul 19 '18 at 13:15
  • But I saw some articles about `airbnb`, In these articles were wrote `airbnb` eslint is annoying and make development hard and slow. Is it right? – AmerllicA Jul 19 '18 at 13:47
  • 3
    I think the point of eslint is to put HARD restrictions on how the code is written. Airbnb config is industry standard, if your developers complain it might be because they don't use estrict configs. I personally think that is very good to have a very tight eslint configuration that doesn't allow lazy developers do what they think is easy. – fernandopasik Jul 19 '18 at 21:57
  • 2
    It's not just about being strict on code style, airbnb has good rules that detect potential bugs, performance issues, and other things – fernandopasik Jul 19 '18 at 22:09
  • Thanks a lot, Definitely I use it in my next project. Undoubtedly I will do it. But first I should R&D about it and study how can I settle it in my case. – AmerllicA Jul 19 '18 at 22:18