Skip to content

Commit

Permalink
fix: mapDispatchToProps-prefer-shorthand should ignore prop name (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
DianaSuvorova authored Jan 2, 2020
1 parent 8e8d38a commit 0174090
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 11 additions & 5 deletions lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ const getParamsString = (params, context) =>
const propertyCanUseShortHandButDoesnt = (context, prop, dispatchName) => {
const propName = prop.key && prop.key.name;
const sourceCode = context.getSource(prop.value).replace(/(\r\n|\n|\r|\t| |;)/gm, '');
if (prop.value && (prop.value.type === 'ArrowFunctionExpression' ||
prop.value.type === 'FunctionExpression')) {
if (prop.value && prop.value.type === 'ArrowFunctionExpression') {
const fncDef = prop.value;
const paramString = getParamsString(fncDef.params, context);
if ((sourceCode === `(${paramString})=>${dispatchName}(${propName}(${paramString}))`)
|| (sourceCode === `function(${paramString}){return${dispatchName}(${propName}(${paramString}))}`
)) {
const actionNode = prop.value.body && prop.value.body.arguments && prop.value.body.arguments[0];
const nameFromSourceCode = actionNode && actionNode.callee && actionNode.callee.name;
if (sourceCode === `(${paramString})=>${dispatchName}(${nameFromSourceCode}(${paramString}))`) {
return true;
}
} else if (prop.value && prop.value.type === 'FunctionExpression') {
const fncDef = prop.value;
const paramString = getParamsString(fncDef.params, context);
if (sourceCode === `function(${paramString}){return${dispatchName}(${propName}(${paramString}))}`
) {
return true;
}
}
Expand Down
12 changes: 9 additions & 3 deletions tests/lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
{ fetchProducts }
)(Products);
`,
`const mapDispatchToProps = dispatch => ({
onDoSomething: () => dispatch(toSomethingElse())
});`,
'connect(null, null)(App)',
'function mapDispatchToProps () {return aThing}',
],
Expand Down Expand Up @@ -73,5 +70,14 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
message: 'mapDispatchToProps should use a shorthand dispatch wrapping instead',
},
],
}, {
code: `const mapDispatchToProps = dispatch => ({
onDoSomething: () => dispatch(toSomethingElse()),
});`,
errors: [
{
message: 'mapDispatchToProps should use a shorthand dispatch wrapping instead',
},
],
}],
});

0 comments on commit 0174090

Please sign in to comment.