Skip to content

Commit a68732f

Browse files
feat: mapDispatchToProps-prefer-object
feat: mapDispatchToProps-prefer-object
1 parent 515ec5d commit a68732f

File tree

7 files changed

+102
-3
lines changed

7 files changed

+102
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
package-lock.json

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Then configure the rules you want to use under the rules section.
4545

4646
* [react-redux/connect-prefer-minimum-two-arguments](docs/rules/connect-prefer-minimum-two-arguments.md) Enforces that connect function has at least 2 arguments.
4747
* [react-redux/connect-prefer-named-arguments](docs/rules/connect-prefer-named-arguments.md) Enforces that all connect arguments have specific names.
48+
* [react-redux/mapDispatchToProps-prefer-object](docs/rules/mapDispatchToProps-prefer-object.md) Enforces that all mapDispatchToProps parameters have specific names.
49+
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that mapDispatchToProps returns an object.
4850
* [react-redux/mapStateToProps-prefer-parameters-names](docs/rules/mapStateToProps-prefer-parameters-names.md) Enforces that all mapStateToProps parameters have specific names.
49-
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that all mapDispatchToProps parameters have specific names.
5051
* [react-redux/prefer-separate-component-file](docs/rules/prefer-separate-component-file.md) Enforces that all connected components are defined in a separate file.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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) => (
14+
{
15+
requestFilteredItems: (keyword) => {
16+
dispatch(requestFilteredItems(keyword));
17+
}
18+
}
19+
)
20+
```
21+
22+
The following patterns are considered correct:
23+
24+
```js
25+
const mapDispatchToProps = () => {}
26+
```
27+
28+
```js
29+
const mapDispatchToProps = {anAction: anAction}
30+
```

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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-
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
4+
'mapDispatchToProps-prefer-object': require('./lib/rules/mapDispatchToProps-prefer-object'),
55
'mapDispatchToProps-prefer-parameters-names': require('./lib/rules/mapDispatchToProps-prefer-parameters-names'),
6+
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
67
'prefer-separate-component-file': require('./lib/rules/prefer-separate-component-file'),
78
};
89

@@ -24,8 +25,9 @@ module.exports = {
2425
rules: {
2526
'react-redux/connect-prefer-minimum-two-arguments': 0,
2627
'react-redux/connect-prefer-named-arguments': 2,
27-
'react-redux/mapStateToProps-prefer-parameters-names': 2,
2828
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
29+
'react-redux/mapDispatchToProps-prefer-object': 2,
30+
'react-redux/mapStateToProps-prefer-parameters-names': 2,
2931
'react-redux/prefer-separate-component-file': 1,
3032
},
3133
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const report = function (context, node) {
2+
context.report({
3+
message: 'mapDispatchToProps should return object',
4+
node,
5+
});
6+
};
7+
8+
const checkDeclaration = function (context, node) {
9+
const init = (
10+
node.id &&
11+
node.id.name === 'mapDispatchToProps' &&
12+
((node.init && node.init.body) || node.body)
13+
);
14+
if (init && context.getSource(init) !== '{}') {
15+
report(context, node);
16+
}
17+
};
18+
19+
module.exports = function (context) {
20+
return {
21+
VariableDeclaration(node) {
22+
node.declarations.forEach(decl => checkDeclaration(context, decl));
23+
},
24+
FunctionDeclaration(node) {
25+
checkDeclaration(context, node);
26+
},
27+
};
28+
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require('babel-eslint');
2+
3+
const rule = require('../../../lib/rules/mapDispatchToProps-prefer-object');
4+
const RuleTester = require('eslint').RuleTester;
5+
6+
const parserOptions = {
7+
ecmaVersion: 6,
8+
sourceType: 'module',
9+
ecmaFeatures: {
10+
experimentalObjectRestSpread: true,
11+
},
12+
};
13+
14+
const ruleTester = new RuleTester({ parserOptions });
15+
16+
ruleTester.run('mapDispatchToProps-prefer-object', rule, {
17+
valid: [
18+
'const mapDispatchToProps = {anAction: anAction}',
19+
'const mapDispatchToProps = () => {}',
20+
'function mapDispatchToProps () {}',
21+
],
22+
invalid: [{
23+
code: `const mapDispatchToProps = (dispatch) => (
24+
{
25+
requestFilteredItems: (client, keyword) => {
26+
dispatch(requestFilteredItems(client, keyword));
27+
}
28+
}
29+
)`,
30+
errors: [
31+
{
32+
message: 'mapDispatchToProps should return object',
33+
},
34+
],
35+
}],
36+
});

tests/lib/rules/mapDispatchToProps-prefer-parameters-names.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
1818
'const mapDispatchToProps = (dispatch, ownProps) => {}',
1919
'const mapDispatchToProps = (dispatch) => {}',
2020
'const mapDispatchToProps = (dispatch, ownProps, moreArgs) => {}',
21+
'const mapDispatchToProps = {anAction: anAction}',
2122
],
2223
invalid: [{
2324
code: 'const mapDispatchToProps = (anyOtherName) => {}',

0 commit comments

Comments
 (0)