Skip to content

Commit 118da84

Browse files
fix: support destructuring assignment (#14)
* fix: mapStateToProps-no-store support destructuring assignment * fix: allowing destructuring args in prefer-names * fix: correcting test case * chore: adding test case for ownProps destr assignment
1 parent 2d347ef commit 118da84

10 files changed

+41
-10
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const mapDispatchToProps = () => {}
2828
const mapDispatchToProps(dispatch, ownProps) => {}
2929
```
3030

31+
```js
32+
const mapDispatchToProps(dispatch, {prop1, prop2}) => {}
33+
```
34+
3135
```js
3236
const mapDispatchToProps(dispatch) => {}
3337
```

docs/rules/mapStateToProps-no-store.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ const mapStateToProps = () => {}
3535
const mapStateToProps = (state) => {isActive: state.isActive}
3636
```
3737

38+
```js
39+
const mapStateToProps = ({isActive}) => {isActive}
40+
```
41+
3842
```js
3943
connect((state) => ({isActive: state.isActive}), null)(App)
4044
```

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const mapStateToProps = (state, ownProps) => {}
2828
const mapStateToProps = (state) => {}
2929
```
3030

31+
```js
32+
const mapStateToProps = ({isActive}) => {isActive}
33+
```
34+
3135
```js
3236
connect((state) => state, null)(App)
3337
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const report = function (context, node, i) {
1414

1515
const check = function (context, params) {
1616
params.forEach((param, i) => {
17-
if (argumentNames[i] && argumentNames[i] !== param.name) {
17+
if (argumentNames[i] && param.type !== 'ObjectPattern' && argumentNames[i] !== param.name) {
1818
report(context, param, i);
1919
}
2020
});

lib/rules/mapStateToProps-no-store.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ const report = function (context, node) {
88
});
99
};
1010

11-
const getFirstParamName = node =>
12-
node.params && node.params[0] && node.params[0].name;
11+
// first param name or false for destructuring assignment;
12+
const getFirstParamName = (node) => {
13+
const firstParam = node.params && node.params[0];
14+
return firstParam && firstParam.type === 'Identifier' && firstParam.name;
15+
};
1316

1417
const propertyIsStore = (prop, storeName) => {
1518
if (prop.type === 'Property' && prop.value && prop.value.name === storeName) {
@@ -47,23 +50,29 @@ module.exports = function (context) {
4750
node.declarations.forEach((decl) => {
4851
if (decl.id && decl.id.name === 'mapStateToProps') {
4952
const body = decl.init.body;
50-
const firstParamName = decl.init.params &&
51-
decl.init.params[0] &&
52-
decl.init.params[0].name;
53-
checkFunction(context, body, firstParamName);
53+
const firstParamName = getFirstParamName(decl.init);
54+
if (firstParamName) {
55+
checkFunction(context, body, firstParamName);
56+
}
5457
}
5558
});
5659
},
5760
FunctionDeclaration(node) {
5861
if (node.id && node.id.name === 'mapStateToProps') {
59-
checkFunction(context, node.body, getFirstParamName(node));
62+
const firstParamName = getFirstParamName(node);
63+
if (firstParamName) {
64+
checkFunction(context, node.body, firstParamName);
65+
}
6066
}
6167
},
6268
CallExpression(node) {
6369
if (isReactReduxConnect(node)) {
6470
const mapStateToProps = node.arguments && node.arguments[0];
6571
if (mapStateToProps && mapStateToProps.body) {
66-
checkFunction(context, mapStateToProps.body, getFirstParamName(mapStateToProps));
72+
const firstParamName = getFirstParamName(mapStateToProps);
73+
if (firstParamName) {
74+
checkFunction(context, mapStateToProps.body, firstParamName);
75+
}
6776
}
6877
}
6978
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const report = function (context, node, i) {
1414

1515
const check = function (context, params) {
1616
params.forEach((param, i) => {
17-
if (argumentNames[i] && argumentNames[i] !== param.name) {
17+
if (argumentNames[i] && param.type !== 'ObjectPattern' && argumentNames[i] !== param.name) {
1818
report(context, param, i);
1919
}
2020
});

tests/lib/rules/connect-prefer-minimum-two-arguments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ruleTester.run('connect-prefer-minimum-two-arguments', rule, {
1717
valid: [
1818
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
1919
'connect(mapStateToProps, mapDispatchToProps)(Component)',
20+
'connect({prop1, prop2}, {action1, action2})(Component)',
2021
],
2122
invalid: [{
2223
code: 'connect(mapStateToProps)(Component)',

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const ruleTester = new RuleTester({ parserOptions });
1616
ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
1717
valid: [
1818
'const mapDispatchToProps = (dispatch, ownProps) => {}',
19+
'const mapDispatchToProps = (dispatch, {prop1, prop2}) => {}',
1920
'const mapDispatchToProps = (dispatch) => {}',
2021
'const mapDispatchToProps = (dispatch, ownProps, moreArgs) => {}',
2122
'const mapDispatchToProps = {anAction: anAction}',

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ ruleTester.run('mapStateToProps-no-store', rule, {
5757
'const mapStateToProps = (state) => {isActive: state.isActive}',
5858
`const mapStateToProps = (state, ownProps) => {};
5959
connect(mapStateToProps, null)(Alert);`,
60+
`const mapStateToProps = ({ header }) => ({
61+
isLoggedIn: header.user && header.user.isLoggedIn,
62+
}); `,
63+
'const mapStateToProps = ({header}, ownProps) => {header};',
64+
'connect(({header}, ownProps) => {header})(App);',
65+
'connect(({header}, {ownProp1}) => {header, ownProp1})(App);',
6066
],
6167
invalid: [{
6268
code: 'const mapStateToProps = (state) => state',

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

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

1616
ruleTester.run('mapStateToProps-prefer-parameters-names', rule, {
1717
valid: [
18+
'const mapStateToProps = ({prop1, prop2}, {ownProp1, ownProp2}) => {}',
1819
'const mapStateToProps = (state, ownProps) => {}',
1920
'const mapStateToProps = (state) => {}',
2021
'const mapStateToProps = (state, ownProps, moreArgs) => {}',
@@ -23,6 +24,7 @@ ruleTester.run('mapStateToProps-prefer-parameters-names', rule, {
2324
'connect({state}, null)(App)',
2425
'const mapStateToProps = {}',
2526
'connect(null, null)(App)',
27+
'const mapStateToProps = ({prop1, prop2}, ownProps) => {}',
2628
],
2729
invalid: [{
2830
code: 'const mapStateToProps = (anyOtherName) => {}',

0 commit comments

Comments
 (0)