Skip to content

Commit 44a92d1

Browse files
committed
Revert "BREAKING CHANGE: refactor MDTP-prefer-object to MDTP-returns-object (#17)"
This reverts commit e628781.
1 parent e628781 commit 44a92d1

File tree

6 files changed

+79
-156
lines changed

6 files changed

+79
-156
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Enforces that mapDispatchToProps returns an object. (react-redux/mapDispatchToProps-prefer-object)
2+
3+
In most cases one just needs to pass an object with an actions. And connect wraps it into dispatch function.
4+
5+
[react-redux documentation](https://github.com/reactjs/react-redux/blob/master/docs/api.md#arguments)
6+
> If an object is passed, each function inside it is assumed to be a Redux action creator. An object with the same function names, but with every action creator wrapped into a dispatch call so they may be invoked directly, will be merged into the component’s props.
7+
8+
## Rule details
9+
10+
The following pattern is considered incorrect:
11+
12+
```js
13+
const mapDispatchToProps = (dispatch) => dispatch(action())
14+
```
15+
16+
```js
17+
connect((state) => state, (dispatch) => dispatch(action()))(App)
18+
```
19+
20+
```js
21+
const mapDispatchToProps = () => {}
22+
```
23+
24+
The following patterns are considered correct:
25+
26+
27+
```js
28+
const mapDispatchToProps = {anAction: anAction}
29+
```
30+
31+
```js
32+
connect((state) => state, {anAction}))(App)
33+
```

docs/rules/mapDispatchToProps-returns-object.md

Lines changed: 0 additions & 99 deletions
This file was deleted.

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const rules = {
22
'connect-prefer-minimum-two-arguments': require('./lib/rules/connect-prefer-minimum-two-arguments'),
33
'connect-prefer-named-arguments': require('./lib/rules/connect-prefer-named-arguments'),
4-
'mapDispatchToProps-returns-object': require('./lib/rules/mapDispatchToProps-returns-object'),
4+
'mapDispatchToProps-prefer-object': require('./lib/rules/mapDispatchToProps-prefer-object'),
55
'mapDispatchToProps-prefer-parameters-names': require('./lib/rules/mapDispatchToProps-prefer-parameters-names'),
66
'mapStateToProps-no-store': require('./lib/rules/mapStateToProps-no-store'),
77
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
@@ -27,7 +27,7 @@ module.exports = {
2727
'react-redux/connect-prefer-minimum-two-arguments': 0,
2828
'react-redux/connect-prefer-named-arguments': 2,
2929
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
30-
'react-redux/mapDispatchToProps-returns-object': 2,
30+
'react-redux/mapDispatchToProps-prefer-object': 2,
3131
'react-redux/mapStateToProps-no-store': 2,
3232
'react-redux/mapStateToProps-prefer-parameters-names': 2,
3333
'react-redux/prefer-separate-component-file': 1,

lib/rules/mapDispatchToProps-returns-object.js renamed to lib/rules/mapDispatchToProps-prefer-object.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const isReactReduxConnect = require('../isReactReduxConnect');
2-
const utils = require('../utils');
32

43
const report = function (context, node) {
54
context.report({
@@ -18,20 +17,14 @@ module.exports = function (context) {
1817
decl.init.type === 'ArrowFunctionExpression' ||
1918
decl.init.type === 'FunctionExpression'
2019
)) {
21-
const returnNode = utils.getReturnNode(decl.init);
22-
if (!utils.isObject(returnNode)) {
23-
report(context, node);
24-
}
20+
report(context, decl);
2521
}
2622
}
2723
});
2824
},
2925
FunctionDeclaration(node) {
3026
if (node.id && node.id.name === 'mapDispatchToProps') {
31-
const returnNode = utils.getReturnNode(node.body);
32-
if (!utils.isObject(returnNode)) {
33-
report(context, node);
34-
}
27+
report(context, node.body);
3528
}
3629
},
3730
CallExpression(node) {
@@ -41,10 +34,7 @@ module.exports = function (context) {
4134
mapDispatchToProps.type === 'ArrowFunctionExpression' ||
4235
mapDispatchToProps.type === 'FunctionExpression')
4336
) {
44-
const returnNode = utils.getReturnNode(mapDispatchToProps);
45-
if (!utils.isObject(returnNode)) {
46-
report(context, node);
47-
}
37+
report(context, mapDispatchToProps);
4838
}
4939
}
5040
},

lib/utils.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
'use strict';
22

3-
const isObject = node => node && (
4-
node.type === 'ObjectExpression' || node.type === 'Identifier'
5-
);
6-
73
const getReturnNode = (node) => {
84
const body = node.body;
9-
if (!body) {
5+
if (!body || !body.length) {
106
return node;
11-
} else if (isObject(body)) {
12-
return body;
13-
} else if (body.type === 'BlockStatement') {
14-
return getReturnNode(body);
157
}
168
for (let i = body.length - 1; i >= 0; i -= 1) {
179
if (body[i].type === 'ReturnStatement') {
18-
const arg = body[i].argument;
19-
if (arg.type === 'ArrowFunctionExpression' || arg.type === 'FunctionExpression') {
20-
return getReturnNode(arg);
21-
}
22-
return arg;
10+
return body[i].argument;
2311
}
2412
}
2513
return null;
2614
};
2715

2816
module.exports = {
2917
getReturnNode,
30-
isObject,
3118
};

tests/lib/rules/mapDispatchToProps-returns-object.js renamed to tests/lib/rules/mapDispatchToProps-prefer-object.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require('babel-eslint');
22

3-
const rule = require('../../../lib/rules/mapDispatchToProps-returns-object');
3+
const rule = require('../../../lib/rules/mapDispatchToProps-prefer-object');
44
const RuleTester = require('eslint').RuleTester;
55

66
const parserOptions = {
@@ -13,11 +13,8 @@ const parserOptions = {
1313

1414
const ruleTester = new RuleTester({ parserOptions });
1515

16-
ruleTester.run('mapDispatchToProps-returns-object', rule, {
16+
ruleTester.run('mapDispatchToProps-prefer-object', rule, {
1717
valid: [
18-
'const mapDispatchToProps = {}',
19-
'const mapDispatchToProps = null',
20-
'const mapDispatchToProps = actionsMap',
2118
'const mapDispatchToProps = {...actions}',
2219
'const mapDispatchToProps = {anAction: anAction}',
2320
`export default connect(
@@ -27,29 +24,7 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
2724
{ fetchProducts }
2825
)(Products);
2926
`,
30-
'function mapDispatchToProps () {return {action}}',
31-
`const mapDispatchToProps = (dispatch) => (
32-
{
33-
requestFilteredItems: (client, keyword) => {
34-
dispatch(requestFilteredItems(client, keyword));
35-
}
36-
}
37-
)
38-
`,
39-
`const mapDispatchToProps = dispatch => ({
40-
onDoSomething: () => dispatch(toSomethingElse())
41-
});`,
42-
`const mapDispatchToProps = function(dispatch) {
43-
return { requestFilteredItems: (client, keyword) => {
44-
dispatch(requestFilteredItems(client, keyword));
45-
}
46-
}
47-
}`,
4827
'connect(null, null)(App)',
49-
'function mapDispatchToProps () {return aThing}',
50-
`function mapDispatchToProps(dispatch) {
51-
return { actions: bindActionCreators(actionCreators, dispatch) }
52-
}`,
5328
],
5429
invalid: [{
5530
code: 'function mapDispatchToProps () {}',
@@ -65,6 +40,43 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
6540
message: 'mapDispatchToProps should return object',
6641
},
6742
],
43+
}, {
44+
code: `const mapDispatchToProps = (dispatch) => (
45+
{
46+
requestFilteredItems: (client, keyword) => {
47+
dispatch(requestFilteredItems(client, keyword));
48+
}
49+
}
50+
)`,
51+
errors: [
52+
{
53+
message: 'mapDispatchToProps should return object',
54+
},
55+
],
56+
}, {
57+
code: `function mapDispatchToProps(dispatch) {
58+
return { requestFilteredItems: (client, keyword) => {
59+
dispatch(requestFilteredItems(client, keyword));
60+
}
61+
}
62+
}`,
63+
errors: [
64+
{
65+
message: 'mapDispatchToProps should return object',
66+
},
67+
],
68+
}, {
69+
code: `const mapDispatchToProps = function(dispatch) {
70+
return { requestFilteredItems: (client, keyword) => {
71+
dispatch(requestFilteredItems(client, keyword));
72+
}
73+
}
74+
}`,
75+
errors: [
76+
{
77+
message: 'mapDispatchToProps should return object',
78+
},
79+
],
6880
}, {
6981
code: `export default connect(
7082
state => ({

0 commit comments

Comments
 (0)