Skip to content

Commit

Permalink
[eslint config] [patch] Fixed handle and on ordering in `sort-com…
Browse files Browse the repository at this point in the history
…p` rule

- fixes #2116
  • Loading branch information
alvyn279 authored and ljharb committed Sep 23, 2020
1 parent b30b0e4 commit d3c7b84
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/eslint-config-airbnb/rules/react.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ module.exports = {
'static-methods',
'instance-variables',
'lifecycle',
'/^handle.+$/',
'/^on.+$/',
'getters',
'setters',
Expand Down
47 changes: 46 additions & 1 deletion packages/eslint-config-airbnb/test/test-react-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ${body}}
`;
}

test('validate react prop order', (t) => {
test('validate react methods order', (t) => {
t.test('make sure our eslintrc has React and JSX linting dependencies', (t) => {
t.plan(2);
t.deepEqual(reactRules.plugins, ['react']);
Expand All @@ -44,6 +44,8 @@ test('validate react prop order', (t) => {
t.plan(3);
const result = lint(wrapComponent(`
componentDidMount() {}
handleSubmit() {}
onButtonAClick() {}
setFoo() {}
getFoo() {}
setBar() {}
Expand Down Expand Up @@ -88,4 +90,47 @@ test('validate react prop order', (t) => {
t.ok(result.errorCount, 'fails');
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
});

t.test('order: when handler method with `handle` prefix after method with `on` prefix', (t) => {
t.plan(2);
const result = lint(wrapComponent(`
componentDidMount() {}
onButtonAClick() {}
handleSubmit() {}
setFoo() {}
getFoo() {}
render() { return <div />; }
`));

t.ok(result.errorCount, 'fails');
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
});

t.test('order: when lifecycle methods after event handler methods', (t) => {
t.plan(2);
const result = lint(wrapComponent(`
handleSubmit() {}
componentDidMount() {}
setFoo() {}
getFoo() {}
render() { return <div />; }
`));

t.ok(result.errorCount, 'fails');
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
});

t.test('order: when event handler methods after getters and setters', (t) => {
t.plan(2);
const result = lint(wrapComponent(`
componentDidMount() {}
setFoo() {}
getFoo() {}
handleSubmit() {}
render() { return <div />; }
`));

t.ok(result.errorCount, 'fails');
t.deepEqual(result.messages.map((msg) => msg.ruleId), ['react/sort-comp'], 'fails due to sort');
});
});
3 changes: 2 additions & 1 deletion react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ We don’t recommend using indexes for keys if the order of items may change.
1. `componentWillUpdate`
1. `componentDidUpdate`
1. `componentWillUnmount`
1. *clickHandlers or eventHandlers* like `onClickSubmit()` or `onChangeDescription()`
1. *event handlers starting with 'handle'* like `handleSubmit()` or `handleChangeDescription()`
1. *event handlers starting with 'on'* like `onClickSubmit()` or `onChangeDescription()`
1. *getter methods for `render`* like `getSelectReason()` or `getFooterContent()`
1. *optional render methods* like `renderNavigation()` or `renderProfilePicture()`
1. `render`
Expand Down

0 comments on commit d3c7b84

Please sign in to comment.