Skip to content

Commit 75b78ba

Browse files
committed
Teach sort-comp rule about getters and setters.
1 parent ac72383 commit 75b78ba

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

docs/rules/sort-comp.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ The default configuration is:
9090
* `everything-else` is a special group that match all the methods that do not match any of the other groups.
9191
* `render` is referring to the `render` method.
9292
* `type-annotations`. This group is not specified by default, but can be used to enforce flow annotations to be at the top.
93+
* `getters` This group is not specified by default, but can be used to enforce class getters positioning.
94+
* `setters` This group is not specified by default, but can be used to enforce class setters positioning.
9395

9496
You can override this configuration to match your needs.
9597

lib/rules/sort-comp.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ module.exports = {
136136
}
137137
}
138138

139+
if (method.getter) {
140+
for (i = 0, j = methodsOrder.length; i < j; i++) {
141+
if (methodsOrder[i] === 'getters') {
142+
indexes.push(i);
143+
break;
144+
}
145+
}
146+
}
147+
148+
if (method.setter) {
149+
for (i = 0, j = methodsOrder.length; i < j; i++) {
150+
if (methodsOrder[i] === 'setters') {
151+
indexes.push(i);
152+
break;
153+
}
154+
}
155+
}
156+
139157
if (method.typeAnnotation) {
140158
for (i = 0, j = methodsOrder.length; i < j; i++) {
141159
if (methodsOrder[i] === 'type-annotations') {
@@ -363,6 +381,8 @@ module.exports = {
363381
function checkPropsOrder(properties) {
364382
const propertiesInfos = properties.map(node => ({
365383
name: getPropertyName(node),
384+
getter: node.kind === 'get',
385+
setter: node.kind === 'set',
366386
static: node.static,
367387
typeAnnotation: !!node.typeAnnotation && node.value === null
368388
}));

0 commit comments

Comments
 (0)