Skip to content

New rule proposal: compact-accessor #12277

@mdjermanovic

Description

@mdjermanovic

Please 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 and no-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.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions