From 01740901e467bab231b01a30cc95aa5cf93bcaeb Mon Sep 17 00:00:00 2001 From: DianaSuvorova Date: Thu, 2 Jan 2020 13:20:58 -0800 Subject: [PATCH] fix: mapDispatchToProps-prefer-shorthand should ignore prop name (#46) --- lib/rules/mapDispatchToProps-prefer-shorthand.js | 16 +++++++++++----- .../rules/mapDispatchToProps-prefer-shorthand.js | 12 +++++++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/rules/mapDispatchToProps-prefer-shorthand.js b/lib/rules/mapDispatchToProps-prefer-shorthand.js index 83e4d54..2e3d312 100644 --- a/lib/rules/mapDispatchToProps-prefer-shorthand.js +++ b/lib/rules/mapDispatchToProps-prefer-shorthand.js @@ -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; } } diff --git a/tests/lib/rules/mapDispatchToProps-prefer-shorthand.js b/tests/lib/rules/mapDispatchToProps-prefer-shorthand.js index 2cf7e03..037776f 100644 --- a/tests/lib/rules/mapDispatchToProps-prefer-shorthand.js +++ b/tests/lib/rules/mapDispatchToProps-prefer-shorthand.js @@ -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}', ], @@ -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', + }, + ], }], });