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.

Rule Request: setter functions variable name to value

See original GitHub issue

Please describe what the rule should do: Enforce the variable of the set functions to value.

What category of rule is this? (place an “X” next to just one item)

[x] Enforces code style [ ] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

const person = {
  set name(name) {
    this._name = name;
  }
}

const obj = {
  set shape(shape) {
    this._shape = shape;
  }
}

Should pass with:

const person = {
  set name(value) {
    this._name = value;
  }
}

const obj = {
  set shape(value) {
    this._shape = value;
  }
}

Why should this rule be included in ESLint (instead of a plugin)? I think this would be important to have the same name in all setter functions. I look up for this, but found nothing. value is suggested as it is wide used as the setter variable name, but it is actually not enforced, and could be named anything else.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Dec 13, 2017

Thanks for clarifying. Personally, I don’t think this rule would be a good fit for ESLint core, since I think it’s a fairly uncommon preference. However, you could enforce this on your own project using the no-restricted-syntax rule:

{
  "rules": {
    "no-restricted-syntax": [
      "error",
      {
        "selector": ":matches(MethodDefinition, Property)[kind='set'][value.params.0.name!='value']",
        "message": "Setter parameters should be called 'value'."
      }
    ]
  }
}
1reaction
not-an-aardvarkcommented, Dec 13, 2017

Could you clarify why you want the argument of all setter functions to be called value? I’m not sure I understand the purpose of doing that; it seems like some setter functions could benefit from more specific names.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best Practices for Java Getter and Setter - DZone
In Java, getter and setter are two conventional methods that are used for retrieving and updating the value of a variable.
Read more >
Java Getter and Setter Tutorial - from Basics to Best Practices
In Java, getter and setter are two conventional methods that are used for retrieving and updating value of a variable.
Read more >
java - get associated getter/setter of field (member variable)
So in order to get a getter/setter for a Field myField of object myObject , we need to create its corresponding getter/setter method...
Read more >
Getters And Setters In Java: Common Mistakes, And ... - Xperti
A getter method on the other hand is the only possible way to read the value of the variable, outside the class: 1....
Read more >
3.2 Instance Variables, set Methods and get Methods - InformIT
Method setName receives parameter name of type String. Parameters are declared in a parameter list, which is located inside the parentheses that ...
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