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.

conflict when use class-style component syntax and airbnb eslint rule

See original GitHub issue

Version

3.0.0-beta.6

Reproduction link

https://github.com/BGPSekai/T.Vey-frontend/blob/d09c83f82def85ace3f2739982648ee2629b1bb0/src/views/About.vue

Steps to reproduce

I create a project which select these options: TypeScript Progressive Web App (PWA) Support Router Vuex CSS Pre-processors(SCSS) Linter / Formatter(ESLint Airbnb config)(on save and fix on commit), use class-style component syntax, Babel alongside TypeScript for auto-detected polyfills, then install it with Yarn.

Then I add lifecycle methods(like created) which code like this:

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';

@Component
export default class About extends Vue {
  created() {
    // do something
  }
}
</script>

Then I run yarn serve(or lint), it occurred an error with eslint, it said: error: Expected ‘this’ to be used by class method ‘created’ (class-methods-use-this)

What is expected?

I think it will be OK

What is actually happening?

It just couldn’t pass the lint rule


Actually, it occurs when I use 3.0.0-beta.7, but I can’t select this version in issue helper.

BTW, add this in .eslintrc can ignore this lint rule:

  "rules": {
    "class-methods-use-this": 0
  }

but I think this shouldn’t occur after I build the project with vue-cli.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
davarscommented, Jan 8, 2019

Also, for the DYI approach, I just modified my project’s .eslintrc.js as so:

...
rules: {
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'class-methods-use-this': ['error', {
      exceptMethods: [
        // react lifecycle methods, from the airbnb rule
        'render',
        'getInitialState',
        'getDefaultProps',
        'getChildContext',
        'componentWillMount',
        'componentDidMount',
        'componentWillReceiveProps',
        'shouldComponentUpdate',
        'componentWillUpdate',
        'componentDidUpdate',
        'componentWillUnmount',

        // vue lifecycle methods
        'beforeCreate',
        'created',
        'beforeMount',
        'mounted',
        'beforeUpdate',
        'updated',
        'activated',
        'deactivated',
        'beforeDestroy',
        'destroyed',
        'errorCaptured',
      ],
    }],
...
3reactions
tommyocommented, Jun 26, 2020

For those still wrestling with this: adding the following to .eslintrc.js appears to solve the issue. Could this be added as a default?

  overrides: [
    {
      files: [ '*.vue'],
      rules: {
        'class-methods-use-this': ['error', {
          exceptMethods: [
            // vue lifecycle methods
            'beforeCreate',
            'created',
            'beforeMount',
            'mounted',
            'beforeUpdate',
            'updated',
            'activated',
            'deactivated',
            'beforeDestroy',
            'destroyed',
            'errorCaptured',
          ],
        }],
      },
    },
  ],
Read more comments on GitHub >

github_iconTop Results From Across the Web

Airbnb JavaScript Style Guide()
Airbnb JavaScript Style Guide() { Note: this guide assumes you are using Babel, and requires that you use babel-preset-airbnb or the equivalent. It...
Read more >
Airbnb, ESLint, Prettier conflict over Switch and Case indentation
2 Answers 2 · First off you need to decide what you want your styles tab-width to be. (Most Style-Guides set there tab-width...
Read more >
Setup ESLint + Prettier + AirBnB Style with Create React App
ESLint is a linter which will analyze your code and find common issues, while also identifying styles inconsistent with AirBnB's style guide if ......
Read more >
Setting up efficient workflows with ESLint, Prettier and ...
In this article I would like to start very easily and go into more depth from topic to topic. In the first step...
Read more >
Migrate Angular app to ESLint with Prettier, AirBnB Styleguide ...
How to migrate Angular TypeScript app from TSLint to ESLint, add AirBnB Styleguide, add Prettier, configure Git hooks and VS Code.
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