Skip to content

Commit 2d347ef

Browse files
fix: adding spread support (#13)
* fix: adding spread support * chore: package scripts clean up
1 parent 4c8dee4 commit 2d347ef

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

docs/rules/mapStateToProps-no-store.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ const mapStateToProps = state => {
1717
}
1818
```
1919

20+
```js
21+
const mapStateToProps = state => ({...state});
22+
```
23+
2024
```js
2125
connect((state) => state, null)(App)
2226
```
@@ -34,3 +38,14 @@ const mapStateToProps = (state) => {isActive: state.isActive}
3438
```js
3539
connect((state) => ({isActive: state.isActive}), null)(App)
3640
```
41+
42+
## Not supported use cases.
43+
44+
Please note that the following use case although common is not supported due to the nature of static code analysis.
45+
46+
The following would not warn:
47+
48+
```js
49+
const getProps = (state) => state;
50+
const mapStateToProps = (state) => getProps(state);
51+
```

lib/rules/mapStateToProps-no-store.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ const report = function (context, node) {
1111
const getFirstParamName = node =>
1212
node.params && node.params[0] && node.params[0].name;
1313

14+
const propertyIsStore = (prop, storeName) => {
15+
if (prop.type === 'Property' && prop.value && prop.value.name === storeName) {
16+
// state
17+
return true;
18+
} else if (
19+
// ...state
20+
prop.type === 'ExperimentalSpreadProperty'
21+
&& prop.argument && prop.argument.type === 'Identifier'
22+
&& prop.argument.name === storeName
23+
) {
24+
return true;
25+
}
26+
return false;
27+
};
28+
1429
const checkFunction = function (context, body, firstParamName) {
1530
const returnNode = utils.getReturnNode(body);
1631
// return state;
@@ -20,7 +35,8 @@ const checkFunction = function (context, body, firstParamName) {
2035
// return {store: state};
2136
if (returnNode && returnNode.type === 'ObjectExpression' &&
2237
returnNode.properties.reduce((acc, cv) =>
23-
acc || (cv.value.name === firstParamName), false)) {
38+
(acc || propertyIsStore(cv, firstParamName)), false)
39+
) {
2440
report(context, body);
2541
}
2642
};

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
"scripts": {
1414
"lint": "eslint ./",
1515
"test": "npm run lint && mocha tests --recursive",
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",
1816
"semantic-release": "semantic-release",
1917
"commitmsg": "npm run test && commitlint -e $GIT_PARAMS"
2018
},

tests/lib/rules/mapDispatchToProps-prefer-object.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const ruleTester = new RuleTester({ parserOptions });
1515

1616
ruleTester.run('mapDispatchToProps-prefer-object', rule, {
1717
valid: [
18+
'const mapDispatchToProps = {...actions}',
1819
'const mapDispatchToProps = {anAction: anAction}',
1920
`export default connect(
2021
state => ({

tests/lib/rules/mapStateToProps-no-store.js

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

1616
ruleTester.run('mapStateToProps-no-store', rule, {
1717
valid: [
18+
` const mapStateToProps = state => ({
19+
...getSomeStateFromASelector(state),
20+
showDefaultHeader: showDefaultHeader(state),
21+
});
22+
`,
23+
` const mapStateToProps = state => ({
24+
aField: getSomeStateFromASelector(state),
25+
});
26+
`,
1827
'export default function observeStore(store) {return store;}',
1928
'export default connect(() => {})(Alert)',
2029
'export default connect(() => {})(Alert)',
@@ -107,5 +116,19 @@ ruleTester.run('mapStateToProps-no-store', rule, {
107116
message: 'mapStateToProps should not return complete store object',
108117
},
109118
],
119+
}, {
120+
code: 'const mapStateToProps = state => ({...state});',
121+
errors: [
122+
{
123+
message: 'mapStateToProps should not return complete store object',
124+
},
125+
],
126+
}, {
127+
code: 'connect((state) => ({...state}), null)(App)',
128+
errors: [
129+
{
130+
message: 'mapStateToProps should not return complete store object',
131+
},
132+
],
110133
}],
111134
});

0 commit comments

Comments
 (0)