Skip to content

Commit 5cc55b1

Browse files
committed
mapStateToProps names
1 parent 566d038 commit 5cc55b1

File tree

6 files changed

+87
-9
lines changed

6 files changed

+87
-9
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Enforces that all mapStateToProps parameters have specific names. (react-redux/mapStateToProps-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+
mapStateToProps(anyOtherName)
15+
```
16+
17+
The following patterns are considered correct:
18+
19+
```js
20+
mapStateToProps(state, ownProps)
21+
```
22+
23+
```js
24+
mapStateToProps(state)
25+
```

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const rules = {
22
'connect-prefer-named-parameters': require('./lib/rules/connect-prefer-named-parameters'),
3+
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
34
};
45

56
module.exports = {
67
rules,
78
configs: {
89
recommended: {
910
'react-redux/connect-prefer-named-parameters': 2,
11+
'react-redux/mapStateToProps-prefer-parameters-names': 2,
1012
},
1113
all: {
1214
rules,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module.exports = function (context) {
2121
node.arguments.forEach((argument, i) => {
2222
if (argument.raw && argument.raw !== 'null') {
2323
report(context, node, i);
24-
} else if (!argument.raw && (!argument.name || argument.name !== argumentNames[i])) {
24+
} else if (
25+
!argument.raw
26+
&& argumentNames[i]
27+
&& (!argument.name || argument.name !== argumentNames[i])) {
2528
report(context, node, i);
2629
}
2730
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const argumentNames = [
2+
'state',
3+
'ownProps',
4+
];
5+
6+
const report = function (context, node, i) {
7+
context.report({
8+
message: `mapStateToProps function parameter #${i} should be named ${argumentNames[i]}`,
9+
node,
10+
});
11+
};
12+
13+
module.exports = function (context) {
14+
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+
}
28+
},
29+
};
30+
};

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313
"scripts": {
1414
"lint": "eslint ./",
1515
"test": "npm run lint && mocha tests --recursive",
16-
"test-single": "mocha tests/lib/rules/connect-prefer-named-parameters",
17-
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/connect-prefer-named-parameters"
16+
"test-single": "mocha tests/lib/rules/mapStateToProps-prefer-parameters-names",
17+
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/mapStateToProps-prefer-parameters-names"
1818
},
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/diana.suvorova/eslint-plugin-react-redux"
22+
},
1923
"devDependencies": {
2024
"babel-eslint": "^8.1.0",
2125
"eslint": "^4.14.0",

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

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

1616
ruleTester.run('mapStateToProps-prefer-parameters-names', rule, {
1717
valid: [
18-
'mapStateToProps(state, ownProps)(Component)',
19-
'mapStateToProps(state)(Component)',
20-
],
21-
invalid: [
22-
'mapStateToProps(anyOtherName)(Component)',
23-
'mapStateToProps(anyOtherName, anyOtherName)(Component)',
18+
'mapStateToProps(state, ownProps)',
19+
'mapStateToProps(state)',
20+
'mapStateToProps(state, ownProps, moreArgs)',
2421
],
22+
invalid: [{
23+
code: 'mapStateToProps(anyOtherName)',
24+
errors: [
25+
{
26+
message: 'mapStateToProps function parameter #0 should be named state',
27+
},
28+
],
29+
}, {
30+
code: 'mapStateToProps(anyOtherName, anyOtherName)',
31+
errors: [
32+
{
33+
message: 'mapStateToProps function parameter #0 should be named state',
34+
}, {
35+
message: 'mapStateToProps function parameter #1 should be named ownProps',
36+
},
37+
],
38+
}],
2539
});

0 commit comments

Comments
 (0)