Skip to content

Commit 0174090

Browse files
fix: mapDispatchToProps-prefer-shorthand should ignore prop name (#46)
1 parent 8e8d38a commit 0174090

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

lib/rules/mapDispatchToProps-prefer-shorthand.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ const getParamsString = (params, context) =>
1515
const propertyCanUseShortHandButDoesnt = (context, prop, dispatchName) => {
1616
const propName = prop.key && prop.key.name;
1717
const sourceCode = context.getSource(prop.value).replace(/(\r\n|\n|\r|\t| |;)/gm, '');
18-
if (prop.value && (prop.value.type === 'ArrowFunctionExpression' ||
19-
prop.value.type === 'FunctionExpression')) {
18+
if (prop.value && prop.value.type === 'ArrowFunctionExpression') {
2019
const fncDef = prop.value;
2120
const paramString = getParamsString(fncDef.params, context);
22-
if ((sourceCode === `(${paramString})=>${dispatchName}(${propName}(${paramString}))`)
23-
|| (sourceCode === `function(${paramString}){return${dispatchName}(${propName}(${paramString}))}`
24-
)) {
21+
const actionNode = prop.value.body && prop.value.body.arguments && prop.value.body.arguments[0];
22+
const nameFromSourceCode = actionNode && actionNode.callee && actionNode.callee.name;
23+
if (sourceCode === `(${paramString})=>${dispatchName}(${nameFromSourceCode}(${paramString}))`) {
24+
return true;
25+
}
26+
} else if (prop.value && prop.value.type === 'FunctionExpression') {
27+
const fncDef = prop.value;
28+
const paramString = getParamsString(fncDef.params, context);
29+
if (sourceCode === `function(${paramString}){return${dispatchName}(${propName}(${paramString}))}`
30+
) {
2531
return true;
2632
}
2733
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
3636
{ fetchProducts }
3737
)(Products);
3838
`,
39-
`const mapDispatchToProps = dispatch => ({
40-
onDoSomething: () => dispatch(toSomethingElse())
41-
});`,
4239
'connect(null, null)(App)',
4340
'function mapDispatchToProps () {return aThing}',
4441
],
@@ -73,5 +70,14 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
7370
message: 'mapDispatchToProps should use a shorthand dispatch wrapping instead',
7471
},
7572
],
73+
}, {
74+
code: `const mapDispatchToProps = dispatch => ({
75+
onDoSomething: () => dispatch(toSomethingElse()),
76+
});`,
77+
errors: [
78+
{
79+
message: 'mapDispatchToProps should use a shorthand dispatch wrapping instead',
80+
},
81+
],
7682
}],
7783
});

0 commit comments

Comments
 (0)