Skip to content

Commit d3c7b84

Browse files
alvyn279ljharb
authored andcommitted
[eslint config] [patch] Fixed handle and on ordering in sort-comp rule
- fixes #2116
1 parent b30b0e4 commit d3c7b84

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

packages/eslint-config-airbnb/rules/react.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ module.exports = {
244244
'static-methods',
245245
'instance-variables',
246246
'lifecycle',
247+
'/^handle.+$/',
247248
'/^on.+$/',
248249
'getters',
249250
'setters',

packages/eslint-config-airbnb/test/test-react-order.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ${body}}
3333
`;
3434
}
3535

36-
test('validate react prop order', (t) => {
36+
test('validate react methods order', (t) => {
3737
t.test('make sure our eslintrc has React and JSX linting dependencies', (t) => {
3838
t.plan(2);
3939
t.deepEqual(reactRules.plugins, ['react']);
@@ -44,6 +44,8 @@ test('validate react prop order', (t) => {
4444
t.plan(3);
4545
const result = lint(wrapComponent(`
4646
componentDidMount() {}
47+
handleSubmit() {}
48+
onButtonAClick() {}
4749
setFoo() {}
4850
getFoo() {}
4951
setBar() {}
@@ -88,4 +90,47 @@ test('validate react prop order', (t) => {
8890
t.ok(result.errorCount, 'fails');
8991
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
9092
});
93+
94+
t.test('order: when handler method with `handle` prefix after method with `on` prefix', (t) => {
95+
t.plan(2);
96+
const result = lint(wrapComponent(`
97+
componentDidMount() {}
98+
onButtonAClick() {}
99+
handleSubmit() {}
100+
setFoo() {}
101+
getFoo() {}
102+
render() { return <div />; }
103+
`));
104+
105+
t.ok(result.errorCount, 'fails');
106+
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
107+
});
108+
109+
t.test('order: when lifecycle methods after event handler methods', (t) => {
110+
t.plan(2);
111+
const result = lint(wrapComponent(`
112+
handleSubmit() {}
113+
componentDidMount() {}
114+
setFoo() {}
115+
getFoo() {}
116+
render() { return <div />; }
117+
`));
118+
119+
t.ok(result.errorCount, 'fails');
120+
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
121+
});
122+
123+
t.test('order: when event handler methods after getters and setters', (t) => {
124+
t.plan(2);
125+
const result = lint(wrapComponent(`
126+
componentDidMount() {}
127+
setFoo() {}
128+
getFoo() {}
129+
handleSubmit() {}
130+
render() { return <div />; }
131+
`));
132+
133+
t.ok(result.errorCount, 'fails');
134+
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
135+
});
91136
});

react/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,8 @@ We don’t recommend using indexes for keys if the order of items may change.
667667
1. `componentWillUpdate`
668668
1. `componentDidUpdate`
669669
1. `componentWillUnmount`
670-
1. *clickHandlers or eventHandlers* like `onClickSubmit()` or `onChangeDescription()`
670+
1. *event handlers starting with 'handle'* like `handleSubmit()` or `handleChangeDescription()`
671+
1. *event handlers starting with 'on'* like `onClickSubmit()` or `onChangeDescription()`
671672
1. *getter methods for `render`* like `getSelectReason()` or `getFooterContent()`
672673
1. *optional render methods* like `renderNavigation()` or `renderProfilePicture()`
673674
1. `render`

0 commit comments

Comments
 (0)