question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

eslint throws error on arrow function class methods

See original GitHub issue

I think I’ve found a bug. According to the es6 spec, class methods are free to use arrow function declarations. However, eslint throws a Parsing error: unexpected token = when it encounters these methods.

Try the following:

    class App{
        classMethod = () => { console.log('hi world');}
    }

I’ve tried configuring my .eslintrc to cover all the obvious features without success.

    {
      "ecmaFeatures": {
        "modules":true,
        "arrowFunctions":true,
        "classes":true
      },
      "env": {
        "es6": true
      },
    }

Edit: I’m using v1.10.3

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

97reactions
dekelbcommented, Mar 4, 2017

Following @mysticatea’s comment, if you are using babel and you still want eslint to work based on that, you can add

"parser": "babel-eslint",

to your eslintrc file.

14reactions
michaelaflorescommented, Oct 13, 2017

For somer reason this no longer works for me. Eslint is now throwing an error that a class method as arrow function was used before defined. Anyone else with a similar setup facing this issue?

I also tried changing parserOptions to specify newer ECMA versions, to no avail.

Not sure if it makes a difference but the project was created with create-react-app, which I know does have some babel settings that may have an affect but that should be overridden via my .babelrc I think.

I’ve also noticed that a fellow dev on the same project does not see this issue in his VS Code, which is odd because ESLint should be running an identical version from the project’s node modules with the same settings. Any clues there?

.eslintrc

{
  "extends": "eslint-config-airbnb",
  "env": {
    "browser": true,
    "node": true,
    "mocha": true
  },
  "rules": {
    "camelcase": 0,
    "comma-dangle": 0,
    "guard-for-in": 1,
    "import/default": 0,
    "import/extensions": 0,
    "import/no-duplicates": 0,
    "import/no-extraneous-dependencies": 0,
    "import/named": 0,
    "import/namespace": 0,
    "import/no-unresolved": 0,
    "import/no-named-as-default": 2,
    "indent": [2, 2, {
      "SwitchCase": 1
    }],
    "jsx-a11y/interactive-supports-focus": 1,
    "max-len": 0,
    "new-cap": [2, {
      "capIsNewExceptions": ["List", "Map", "Set"]
    }],
    "no-alert": 1,
    "no-console": 2,
    "no-unused-vars": 2,
    "no-use-before-define": ["error", {
      "functions": false
    }],
    "react/jsx-boolean-value": 0,
    "react/jsx-filename-extension": 0,
    "react/no-multi-comp": 0,
    "global-require": 0
  },
  "parser": "babel-eslint",
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "plugins": [
    "react", "import"
  ],
  "settings": {
    "import/parser": "babel-eslint",
    "import/resolve": {
      "moduleDirectory": ["node_modules", "src"]
    }
  },
  "globals": {
    "__DEVELOPMENT__": true,
    "__CLIENT__": true,
    "__SERVER__": true,
    "__DISABLE_SSR__": true,
    "__DEVTOOLS__": true,
    "webpackIsomorphicTools": true
  }
}

Eslint version

"eslint": "^3.19.0",
    "eslint-config-airbnb": "^15.0.1",
    "eslint-plugin-import": "2.2.0",
    "eslint-plugin-jsx-a11y": "5.0.1",
    "eslint-plugin-react": "7.0.1",

.babelrc

    "presets": [
      "react",
      "es2015",
      "stage-1"
    ]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I configure ESLint to allow fat arrow class methods
What configuration parameter am I missing to enable fat arrow class methods in eslint?
Read more >
class-methods-use-this - ESLint - Pluggable JavaScript Linter
This rule is aimed to flag class methods that do not use this . Examples of incorrect code for this rule: /*eslint class-methods-use-this:...
Read more >
explicit-function-return-type | typescript-eslint
Require explicit return types on functions and class methods. ... Whether to allow arrow functions that start with the `void` keyword.
Read more >
Fixing eslint no-invalid-this error for fat arrow class methods
Fixing eslint no-invalid-this error for fat arrow class methods · npm install these 2 guys: babel-eslint and eslint-plugin-babel · in .eslintrc add plugins:...
Read more >
Require space before/after arrow function's arrow (arrow ...
This rule normalize style of spacing before/after an arrow function's arrow( => ). /*eslint-env es6*/ // { "before": true, "after": true } (a) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found