Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to allow retun value of bindActionCreators call #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# EditorConfig helps developers define and maintain consistent
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️❤️❤️ Thank you!!!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add https://npmjs.com/eccheck to CI to verify this, so it doesn't deviate

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, link above triggers 404 for me, is it some private package?

Copy link
Collaborator

@ljharb ljharb Dec 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops sorry, https://www.npmjs.com/package/eclint :-)

specifically, eclint check $(git ls-files), added in a prelint or postlint script.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's quite a nice package, have not been aware of this, thanks!
I will play around and see if I can add it in the same PR

# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2

[*.{diff,md}]
trim_trailing_whitespace = false
24 changes: 24 additions & 0 deletions docs/rules/mapDispatchToProps-returns-object.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ const mapDispatchToProps = {anAction: anAction}
const mapDispatchToProps = (dispatch) => ({anAction: dispatch(anAction())})
```

## Options

### allowReturnBindFn
Rule example
```js
{
'react-redux/mapDispatchToProps-returns-object': ['error', { allowReturnBindFn: true }],
}
```

If this option is set to true, return the result of `bindActionCreators` considered to be valid:

```js
const mapDispatchToProps = (dispatch) => {
return bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
);
}
```

## Not supported use cases.

#### mapDispatchToProps is a function but actions are not bound to dispatch
Expand Down
12 changes: 9 additions & 3 deletions lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const report = function (context, node) {


module.exports = function (context) {
const config = context.options[0] || {};

const shouldAllowBindActionCreators = returnNode =>
config.allowReturnBindFn && utils.isReturnNodeBindActionCreatorsCall(returnNode);

return {
VariableDeclaration(node) {
node.declarations.forEach((decl) => {
Expand All @@ -19,7 +24,7 @@ module.exports = function (context) {
decl.init.type === 'FunctionExpression'
)) {
const returnNode = utils.getReturnNode(decl.init);
if (!utils.isObject(returnNode)) {
if (!utils.isObject(returnNode) && !shouldAllowBindActionCreators(returnNode)) {
report(context, node);
}
}
Expand All @@ -29,7 +34,8 @@ module.exports = function (context) {
FunctionDeclaration(node) {
if (node.id && node.id.name === 'mapDispatchToProps') {
const returnNode = utils.getReturnNode(node.body);
if (!utils.isObject(returnNode)) {

if (!utils.isObject(returnNode) && !shouldAllowBindActionCreators(returnNode)) {
report(context, node);
}
}
Expand All @@ -42,7 +48,7 @@ module.exports = function (context) {
mapDispatchToProps.type === 'FunctionExpression')
) {
const returnNode = utils.getReturnNode(mapDispatchToProps);
if (!utils.isObject(returnNode)) {
if (!utils.isObject(returnNode) && !shouldAllowBindActionCreators(returnNode)) {
report(context, node);
}
}
Expand Down
13 changes: 12 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
'use strict';

const BIND_ACTION_CREATORS_FN_NAME = 'bindActionCreators';

const isObject = node => node && (
node.type === 'ObjectExpression' || node.type === 'Identifier'
);

const isReturnNodeBindActionCreatorsCall = (node) => {
if (node.type === 'CallExpression') {
return node.callee.type === 'Identifier' && node.callee.name === BIND_ACTION_CREATORS_FN_NAME;
}

return false;
};

const getReturnNode = (node) => {
const body = node && node.body;
if (!body) {
return node;
} else if (isObject(body)) {
} else if (isObject(body) || body.type === 'CallExpression') {
return body;
} else if (body.type === 'BlockStatement') {
return getReturnNode(body);
Expand All @@ -28,4 +38,5 @@ const getReturnNode = (node) => {
module.exports = {
getReturnNode,
isObject,
isReturnNodeBindActionCreatorsCall,
};
92 changes: 92 additions & 0 deletions tests/lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,95 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
],
}],
});

ruleTester.run('mapDispatchToProps-returns-object-allowReturnBindFn', rule, {
valid: [
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this. Would you please add some more negative cases.
For example when the option is set to true but name of the function is not bindActionCreators and when the option is set to false.

{
options: [{ allowReturnBindFn: true }],
code: `function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
);
}`,
},
{
options: [{ allowReturnBindFn: true }],
code: `const mapDispatchToProps = (dispatch) => {
return bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
);
}`,
},
{
options: [{ allowReturnBindFn: true }],
code: `const mapDispatchToProps = (dispatch) =>
bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
);
`,
},
{
options: [{ allowReturnBindFn: true }],
code: `export default connect(
state => ({
productsList: state.Products.productsList,
}),
function (dispatch) {
return bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
)
}
)(Products);`,
},
{
options: [{ allowReturnBindFn: true }],
code: `export default connect(
state => ({
productsList: state.Products.productsList,
}),
(dispatch) => {
return bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
)
}
)(Products);`,
},
{
options: [{ allowReturnBindFn: true }],
code: `export default connect(
state => ({
productsList: state.Products.productsList,
}),
(dispatch) =>
bindActionCreators(
{
requestFilteredItems,
showAlert: showAlertAction,
},
dispatch
)
)(Products);`,
},
],
invalid: [],
});