mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-10 19:48:05 -05:00
[eslint config] [patch] Fixed handle and on ordering in sort-comp rule
- fixes #2116
This commit is contained in:
1
packages/eslint-config-airbnb/rules/react.js
vendored
1
packages/eslint-config-airbnb/rules/react.js
vendored
@@ -244,6 +244,7 @@ module.exports = {
|
||||
'static-methods',
|
||||
'instance-variables',
|
||||
'lifecycle',
|
||||
'/^handle.+$/',
|
||||
'/^on.+$/',
|
||||
'getters',
|
||||
'setters',
|
||||
|
||||
@@ -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']);
|
||||
@@ -44,6 +44,8 @@ test('validate react prop order', (t) => {
|
||||
t.plan(3);
|
||||
const result = lint(wrapComponent(`
|
||||
componentDidMount() {}
|
||||
handleSubmit() {}
|
||||
onButtonAClick() {}
|
||||
setFoo() {}
|
||||
getFoo() {}
|
||||
setBar() {}
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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`
|
||||
|
||||
Reference in New Issue
Block a user