New rule proposal: compact-accessor
See original GitHub issuePlease describe what the rule should do:
If an accessor property has both getter and setter functions, this rule requires their definitions to be one after another in object literals/class definitions.
Optionally, the rule can require their order (get before set, or set before get).
What category of rule is this? (place an “X” next to just one item)
[X] Enforces code style (layout)
Provide 2-3 code examples that this rule will warn about:
/* eslint compact-accessor:error*/
var foo = {
get a(){},
bar: 5,
set a(val){}
};
var foo = {
get a(){},
...bar,
set a(val){}
}
class foo {
get a(){}
bar(){}
set a(val){}
}
/* eslint compact-accessor:["error", "getBeforeSet"]*/
var foo = {
set a(val){},
get a(){}
}
class foo {
set a(val){}
get a(){}
}
Why should this rule be included in ESLint (instead of a plugin)?
While it is allowed, I don’t think there is a real use case for non-consecutive definitions of accessor functions for the same property, and it should be a best practice to avoid this feature. Also, the additional option can enforce a consistent style (e.g., get
before set
).
I’m not sure about the rule’s name, there could be a better word than compact
for this.
Notes:
- The rule does not enforce the existence of the pair for a getter/setter (that’s
accessor-pairs
). - The rule does not require grouping of all accessor properties at the same place, it works with each individually.
- If a property has a duplicate getter or setter, such property will be ignored by this rule and caught by other rules (
no-dupe-keys
andno-dupe-class-members
)
Problem: This rule is overlapping with sort-keys
. But, one might want to enforce this without sorting. Also, sort-keys
doesn’t work with classes and doesn’t enforce get/set order.
Are you willing to submit a pull request to implement this rule?
Yes.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:8 (8 by maintainers)
Top GitHub Comments
grouped-accessor-pairs?
I’ll work on this over the weekend, should be ready for review soon.