Skip to content

Commit 14a428c

Browse files
committed
mapDispatchToProps rule
1 parent 5cc55b1 commit 14a428c

15 files changed

+209
-39
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,12 @@ Then configure the rules you want to use under the rules section.
4343

4444
## Supported Rules
4545

46-
* [react-redux/connect-prefer-named-parameters](docs/rules/connect-prefer-named-parameters.md) Enforces that all connect parameters have specific names.
46+
'react-redux/connect-prefer-minimum-two-arguments': 0,
47+
'react-redux/connect-prefer-named-parameters': 2,
48+
'react-redux/mapStateToProps-prefer-parameters-names': 2,
49+
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
50+
51+
* [react-redux/connect-prefer-minimum-two-arguments](docs/rules/connect-prefer-minimum-two-arguments.md) Enforces that connect function has at least 2 arguments.
52+
* [react-redux/connect-prefer-named-arguments](docs/rules/connect-prefer-named-arguments.md) Enforces that all connect arguments have specific names.
53+
* [react-redux/mapStateToProps-prefer-parameters-names](docs/rules/mapStateToProps-prefer-parameters-names.md) Enforces that all mapStateToProps parameters have specific names.
54+
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that all mapDispatchToProps parameters have specific names.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Enforces that connect function is provided with at least 2 arguments. (react-redux/connect-prefer-minimum-two-arguments)
2+
3+
[react-redux mapStateToProps](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options)
4+
5+
> If you do not supply your own mapDispatchToProps function or object full of action creators, the default mapDispatchToProps implementation just injects dispatch into your component’s props.
6+
7+
This rule enforces that Second argument is provided explicitly.
8+
9+
## Rule details
10+
11+
The following pattern is considered warnings:
12+
13+
```js
14+
connect(mapStateToProps, null, mergeProps)(Component)
15+
```
16+
17+
The following patterns are considered correct:
18+
19+
```js
20+
connect(mapStateToProps, mapDispatchToProps, mergeProps)(Component)
21+
```
22+
23+
```js
24+
connect(mapStateToProps, mapDispatchToProps)(Component)
25+
```

docs/rules/connect-prefer-named-parameters.md renamed to docs/rules/connect-prefer-named-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Enforces that all connect parameters have specific names. (react-redux/connect-prefer-named-parameters)
1+
# Enforces that all connect arguments have specific names. (react-redux/connect-prefer-named-arguments)
22

33
[react-redux connect](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) function has 4 optional arguments:
44
* mapStateToProps
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Enforces that all mapDispatchToProps parameters have specific names. (react-redux/mapDispatchToProps-prefer-named-parameters)
2+
3+
[react-redux mapStateToProps](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) function has 2 optional arguments:
4+
* state
5+
* ownProps
6+
7+
This rule enforces that all of the provided parameters should follow the above naming conventions.
8+
9+
## Rule details
10+
11+
The following pattern is considered warnings:
12+
13+
```js
14+
const mapDispatchToProps = (anyOtherName) => {}
15+
```
16+
17+
The following patterns are considered correct:
18+
19+
```js
20+
const mapDispatchToProps = () => {}
21+
```
22+
23+
```js
24+
const mapDispatchToProps(dispatch, ownProps) => {}
25+
```
26+
27+
```js
28+
const mapDispatchToProps(dispatch) => {}
29+
```

docs/rules/mapStateToProps-prefer-parameters-names.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ This rule enforces that all of the provided parameters should follow the above n
1111
The following pattern is considered warnings:
1212

1313
```js
14-
mapStateToProps(anyOtherName)
14+
const mapStateToProps = (anyOtherName) => {}
1515
```
1616

1717
The following patterns are considered correct:
1818

1919
```js
20-
mapStateToProps(state, ownProps)
20+
const mapStateToProps = (state, ownProps) => {}
2121
```
2222

2323
```js
24-
mapStateToProps(state)
24+
const mapStateToProps = (state) => {}
2525
```

index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
const rules = {
2-
'connect-prefer-named-parameters': require('./lib/rules/connect-prefer-named-parameters'),
2+
'connect-prefer-minimum-two-arguments': require('./lib/rules/connect-prefer-minimum-two-arguments'),
3+
'connect-prefer-named-arguments': require('./lib/rules/connect-prefer-named-parameters'),
34
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
5+
'mapDispatchToProps-prefer-parameters-names': require('./lib/rules/mapDispatchToProps-prefer-parameters-names'),
6+
47
};
58

69
module.exports = {
710
rules,
811
configs: {
912
recommended: {
10-
'react-redux/connect-prefer-named-parameters': 2,
13+
'react-redux/connect-prefer-minimum-two-arguments': 0,
14+
'react-redux/connect-prefer-named-arguments': 2,
1115
'react-redux/mapStateToProps-prefer-parameters-names': 2,
16+
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
1217
},
1318
all: {
1419
rules,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const isReactReduxConnect = require('../isReactReduxConnect');
2+
3+
const report = function (context, node) {
4+
context.report({
5+
message: 'Connect function should have 2 arguments',
6+
node,
7+
});
8+
};
9+
10+
module.exports = function (context) {
11+
return {
12+
CallExpression(node) {
13+
if (isReactReduxConnect(node)) {
14+
if (node.arguments.length < 2) {
15+
report(context, node);
16+
}
17+
}
18+
},
19+
};
20+
};

lib/rules/connect-prefer-named-parameters.js renamed to lib/rules/connect-prefer-named-arguments.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const argumentNames = [
99

1010
const report = function (context, node, i) {
1111
context.report({
12-
message: `Connect function parameter #${i} should be named ${argumentNames[i]}`,
12+
message: `Connect function argument #${i} should be named ${argumentNames[i]}`,
1313
node,
1414
});
1515
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const argumentNames = [
2+
'dispatch',
3+
'ownProps',
4+
];
5+
6+
const report = function (context, node, i) {
7+
context.report({
8+
message: `mapDispatchToProps function parameter #${i} should be named ${argumentNames[i]}`,
9+
node,
10+
});
11+
};
12+
13+
const checkDeclaration = function (context, node) {
14+
if (node.id && node.id.name === 'mapDispatchToProps'
15+
&& node.init && node.init.params
16+
) {
17+
node.init.params.forEach((param, i) => {
18+
if (argumentNames[i] && argumentNames[i] !== param.name) {
19+
report(context, param, i);
20+
}
21+
});
22+
}
23+
};
24+
25+
module.exports = function (context) {
26+
return {
27+
VariableDeclaration(node) {
28+
node.declarations.forEach(decl => checkDeclaration(context, decl));
29+
},
30+
};
31+
};

lib/rules/mapStateToProps-prefer-parameters-names.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ const report = function (context, node, i) {
1010
});
1111
};
1212

13+
const checkDeclaration = function (context, node) {
14+
if (node.id && node.id.name === 'mapStateToProps'
15+
&& node.init && node.init.params
16+
) {
17+
node.init.params.forEach((param, i) => {
18+
if (argumentNames[i] && argumentNames[i] !== param.name) {
19+
report(context, param, i);
20+
}
21+
});
22+
}
23+
};
24+
1325
module.exports = function (context) {
1426
return {
15-
CallExpression(node) {
16-
if (node.callee.name === 'mapStateToProps') {
17-
node.arguments.forEach((argument, i) => {
18-
if (argument.raw && argument.raw !== 'null') {
19-
report(context, node, i);
20-
} else if (
21-
!argument.raw
22-
&& argumentNames[i]
23-
&& (!argument.name || argument.name !== argumentNames[i])) {
24-
report(context, node, i);
25-
}
26-
});
27-
}
27+
VariableDeclaration(node) {
28+
node.declarations.forEach(decl => checkDeclaration(context, decl));
2829
},
2930
};
3031
};

0 commit comments

Comments
 (0)