Bug ESlint with class React, example : 'state' is not defined. (no-undef)
Asked Answered
U

5

9

i'm actually working on a project and when I cloned my project on MacBook, after yarn in shell for install packages, and atom . for open my ATOM. ESLint got a "bug".

For example, for this code :

/*
 * Package Import
 */
 import React, { Component } from 'react';
 import PropTypes from 'prop-types';
 import { Link } from 'react-router-dom';
 import classNames from 'classnames';


/*
 * Code
 */
class UserDropdown extends Component {
  /*
   * PropTypes
   */
   static propTypes = {
     ... // Code 
   }


  /*
   * State
   */
   state = {
     ... // Code
   };

  /*
   * Actions
   */
   onLogOut = () => {
     ... // Code
   };

ESLint tell me :

Error   ESLint  'propTypes' is not defined. (no-undef)  22:10
Error   ESLint  'state' is not defined. (no-undef)  32:3
Error   ESLint  'onLogOut' is not defined. (no-undef)   52:3

I'm still using "babel-plugin-transform-class-properties": "^6.24.1", and i declare it in my brunch-config :

My brunch-config.coffee

  plugins:
    babel:
      presets: ['latest', 'react']
      plugins: [
        'transform-class-properties'
        'transform-object-rest-spread'
      ]

My .eslintrc

{
  "extends": "airbnb",
  "parser": "babel-eslint",
  "env": {
    "browser": true
  },
  "rules": {
    "brace-style": ["error", "stroustrup"],
    "no-param-reassign": ["error", { "props": false }],
    "no-mixed-operators": ["error", { "allowSamePrecedence": true }],
    "jsx-a11y/no-static-element-interactions": "off",
    "react/jsx-filename-extension": "off",
    "react/forbid-prop-types": "off",
    "react/no-unescaped-entities": "off",
    "linebreak-style": "off"
  },
  "settings": {
    "import/resolver": {
      "node": {
        "paths": ["app/"]
      }
    }
  }
}

And my Packages.json

"devDependencies": {
"auto-reload-brunch": "^2.7.1",
"autoprefixer": "^6.7.7",
"babel-brunch": "^6.1.1",
"babel-eslint": "^7.2.3",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"babel-resolver": "^1.1.0",
"brunch": "^2.10.9",
"chai": "^3.5.0",
"enzyme": "^2.8.2",
"eslint": "^3.19.0",
"eslint-config-airbnb": "^14.1.0",
"eslint-import-resolver-node": "^0.3.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-jsx-a11y": "^4.0.0",
"eslint-plugin-react": "^6.10.3",

I'm working on 2 computer, and on Linux, it's OKAY. So, someone have an idea? If you need more code for understand, i can send it. Thanks you dude !

Urinal answered 25/7, 2017 at 19:20 Comment(0)
B
3

I had the same problem and it was because I had ESLint 4 installed. Check your package.json and make sure it’s not something like "eslint": "^3.19.0 || ^4.3.0". I’m using create-react-app, and it’s not compatible with ESLint 4 yet: https://github.com/facebookincubator/create-react-app/issues/2604

Berkie answered 8/8, 2017 at 11:6 Comment(1)
Hello Fredrik ! I was on 3.19, but problem still here. Thanks for feedback and your links @ github. I can see, my problem isn't only for me. So i'm waiting for a new version. Maybe i need completely uninstall eslint on my computer for clean it, and install a proper version of eslint 3.19. Thanks you !Anorthosite
M
2

First you have to install babel-eslint as a dev dependency then in your .eslintrc file add:

"parser": "babel-eslint"
Madra answered 25/7, 2017 at 19:35 Comment(2)
Sorry i forget my packages.json but i have it in devDepAnorthosite
So maybe atom is using the global configuration when runing eslint, try to install babel-eslint globallyMadra
S
1

upgrade eslint

npm i -D eslint@latest
Superdreadnought answered 25/7, 2018 at 9:5 Comment(0)
U
0

i solved my problem by uninstall node, yarn, and npm. I used lastest version of node (8.2.1 actually, same for npm and yarn...)

So after install of Node LTS (6.11.1), and i don't have any problem with ESLint, is nicely working.

Edit : After re-install Yarn (0.27.5), this problem is back here :thinking:

Urinal answered 25/7, 2017 at 23:12 Comment(0)
C
0

I fixed it using this dependencies:

package.json:

"dependencies": {
"react": "16.8.3",
"react-dom": "16.8.3",
"react-scripts": "2.1.8"
}

And i did:

yarn
npm install [email protected]
yarn start

and it worked for me!

Cognoscenti answered 25/3, 2019 at 23:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.