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.

class-methods-use-this rule doesn't consider existence of getters and setters

See original GitHub issue

Currently class-methods-use-this rule is ignoring existence of getters and setters in javascript.

Let’s consider an approach to have proper getter and setter for an object.

// originally bar was a storage engine
let bar;

class Foo {
  get bar() {
    return bar;
  }
  set bar(value) {
    bar = value;
  }
}

After that, eslint was starting to complain that get bar() should use this.

I read the manual which says if method doesn’t use this, it should be a static method. Which doesn’t make much sense for getters, since static getter won’t be executed for instantiated class.

Then, to comply to eslint rule, I’ll need to not use getter or setter and stick to the setBar(), getBar() methods which can be fine, but I think eslint should be wise enough to detect getters special type of functions.

I checked the rule code, and it seems like the isInstanceMethod() function should be changed.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
nzakascommented, Jan 12, 2017

I think the rule is working as intended, as this is an edge case. I’d say the only possibility here is add an option that allows you to opt out of checking accessor properties.

1reaction
BridgeARcommented, May 15, 2017

When extending Errors you want to add a getter for the name and maybe also add some attributes that you do not want to be allowed to be changed at all. So you wouldn’t add a setter for these entries.

class CustomError extends Error {
  get name() {
    return 'CustomError'; // Could also be written as: this.constructor.name
  }
  get code() {
    return 400; // Static value that should not be changed
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

python classes getters and setters static method and class ...
A static method can't access # any of the objects attributes, it's really only included for grouping # related functions to their classes....
Read more >
Can datatype redefine getter/setter?
Currently I'm generating Getter and Setter in class generation code but that expands class definition. Is there a way to specify custom datatype...
Read more >
Guide to Java Reflection - Baeldung
In this tutorial, we will explore Java reflection, which allows us to inspect and/or modify runtime attributes of classes, interfaces, ...
Read more >
11. Methods and @property - Advance Python Tutorials
mehodsEx.py # below x will be used by static method # if we do not define it, ... python methodEx.py method: 14 classmethod:...
Read more >
Effective Dart: Design
Use the same name for the same thing, throughout your code. If a precedent already exists outside your API that users are likely...
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