Skip to content

Commit 566d038

Browse files
committed
connect-prefer-named-params: more cases and docs
1 parent 7f1fbd7 commit 566d038

File tree

7 files changed

+49
-55
lines changed

7 files changed

+49
-55
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ module.exports = {
66
"import"
77
],
88
rules: {
9-
"func-names": 0
9+
"func-names": 0,
10+
"global-require": 0
1011
},
1112
"env": {
1213
mocha: true

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ Then configure the rules you want to use under the rules section.
3636
```json
3737
{
3838
"rules": {
39-
"react-redux/rule-name": 2
39+
"react-redux/connect-prefer-named-parameters": 2
4040
}
4141
}
4242
```
4343

4444
## Supported Rules
4545

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

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
rules,
77
configs: {
88
recommended: {
9-
rules,
9+
'react-redux/connect-prefer-named-parameters': 2,
1010
},
1111
all: {
1212
rules,

lib/rules/connect-prefer-named-parameters.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ module.exports = function (context) {
1919
CallExpression(node) {
2020
if (isReactReduxConnect(node)) {
2121
node.arguments.forEach((argument, i) => {
22-
if (!argument.name || argument.name !== argumentNames[i]) {
22+
if (argument.raw && argument.raw !== 'null') {
23+
report(context, node, i);
24+
} else if (!argument.raw && (!argument.name || argument.name !== argumentNames[i])) {
2325
report(context, node, i);
2426
}
2527
});

npm-debug.log

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

tests/lib/rules/connect-prefer-named-parameters.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ const ruleTester = new RuleTester({ parserOptions });
1515

1616
ruleTester.run('connect-prefer-named-parameters', rule, {
1717
valid: [
18+
'connect(null, mapDispatchToProps)(TodoApp)',
1819
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
1920
'connect(mapStateToProps, mapDispatchToProps)(Component)',
21+
'connect()(TodoApp)',
2022
],
2123
invalid: [{
2224
code: 'connect(() => {}, () => {}, mergeProps, options)(Component)',
@@ -36,6 +38,12 @@ ruleTester.run('connect-prefer-named-parameters', rule, {
3638
message: 'Connect function parameter #1 should be named mapDispatchToProps',
3739
},
3840
],
39-
},
40-
],
41+
}, {
42+
code: 'connect(state => state)(TodoApp)',
43+
errors: [
44+
{
45+
message: 'Connect function parameter #0 should be named mapStateToProps',
46+
},
47+
],
48+
}],
4149
});

0 commit comments

Comments
 (0)