Skip to content

Commit

Permalink
feat: mapDispatchToProps-prefer-object
Browse files Browse the repository at this point in the history
feat: mapDispatchToProps-prefer-object
  • Loading branch information
DianaSuvorova authored Jan 14, 2018
1 parent 515ec5d commit a68732f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
package-lock.json
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Then configure the rules you want to use under the rules section.

* [react-redux/connect-prefer-minimum-two-arguments](docs/rules/connect-prefer-minimum-two-arguments.md) Enforces that connect function has at least 2 arguments.
* [react-redux/connect-prefer-named-arguments](docs/rules/connect-prefer-named-arguments.md) Enforces that all connect arguments have specific names.
* [react-redux/mapDispatchToProps-prefer-object](docs/rules/mapDispatchToProps-prefer-object.md) Enforces that all mapDispatchToProps parameters have specific names.
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that mapDispatchToProps returns an object.
* [react-redux/mapStateToProps-prefer-parameters-names](docs/rules/mapStateToProps-prefer-parameters-names.md) Enforces that all mapStateToProps parameters have specific names.
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that all mapDispatchToProps parameters have specific names.
* [react-redux/prefer-separate-component-file](docs/rules/prefer-separate-component-file.md) Enforces that all connected components are defined in a separate file.
30 changes: 30 additions & 0 deletions docs/rules/mapDispatchToProps-prefer-object.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Enforces that mapDispatchToProps returns an object. (react-redux/mapDispatchToProps-prefer-object)

In most cases one just needs to pass an object with an actions. And connect wraps it into dispatch function.

[react-redux documentation](https://github.com/reactjs/react-redux/blob/master/docs/api.md#arguments)
> If an object is passed, each function inside it is assumed to be a Redux action creator. An object with the same function names, but with every action creator wrapped into a dispatch call so they may be invoked directly, will be merged into the component’s props.
## Rule details

The following pattern is considered incorrect:

```js
const mapDispatchToProps = (dispatch) => (
{
requestFilteredItems: (keyword) => {
dispatch(requestFilteredItems(keyword));
}
}
)
```

The following patterns are considered correct:

```js
const mapDispatchToProps = () => {}
```

```js
const mapDispatchToProps = {anAction: anAction}
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const rules = {
'connect-prefer-minimum-two-arguments': require('./lib/rules/connect-prefer-minimum-two-arguments'),
'connect-prefer-named-arguments': require('./lib/rules/connect-prefer-named-arguments'),
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
'mapDispatchToProps-prefer-object': require('./lib/rules/mapDispatchToProps-prefer-object'),
'mapDispatchToProps-prefer-parameters-names': require('./lib/rules/mapDispatchToProps-prefer-parameters-names'),
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
'prefer-separate-component-file': require('./lib/rules/prefer-separate-component-file'),
};

Expand All @@ -24,8 +25,9 @@ module.exports = {
rules: {
'react-redux/connect-prefer-minimum-two-arguments': 0,
'react-redux/connect-prefer-named-arguments': 2,
'react-redux/mapStateToProps-prefer-parameters-names': 2,
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
'react-redux/mapDispatchToProps-prefer-object': 2,
'react-redux/mapStateToProps-prefer-parameters-names': 2,
'react-redux/prefer-separate-component-file': 1,
},
},
Expand Down
28 changes: 28 additions & 0 deletions lib/rules/mapDispatchToProps-prefer-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const report = function (context, node) {
context.report({
message: 'mapDispatchToProps should return object',
node,
});
};

const checkDeclaration = function (context, node) {
const init = (
node.id &&
node.id.name === 'mapDispatchToProps' &&
((node.init && node.init.body) || node.body)
);
if (init && context.getSource(init) !== '{}') {
report(context, node);
}
};

module.exports = function (context) {
return {
VariableDeclaration(node) {
node.declarations.forEach(decl => checkDeclaration(context, decl));
},
FunctionDeclaration(node) {
checkDeclaration(context, node);
},
};
};
36 changes: 36 additions & 0 deletions tests/lib/rules/mapDispatchToProps-prefer-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require('babel-eslint');

const rule = require('../../../lib/rules/mapDispatchToProps-prefer-object');
const RuleTester = require('eslint').RuleTester;

const parserOptions = {
ecmaVersion: 6,
sourceType: 'module',
ecmaFeatures: {
experimentalObjectRestSpread: true,
},
};

const ruleTester = new RuleTester({ parserOptions });

ruleTester.run('mapDispatchToProps-prefer-object', rule, {
valid: [
'const mapDispatchToProps = {anAction: anAction}',
'const mapDispatchToProps = () => {}',
'function mapDispatchToProps () {}',
],
invalid: [{
code: `const mapDispatchToProps = (dispatch) => (
{
requestFilteredItems: (client, keyword) => {
dispatch(requestFilteredItems(client, keyword));
}
}
)`,
errors: [
{
message: 'mapDispatchToProps should return object',
},
],
}],
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
'const mapDispatchToProps = (dispatch, ownProps) => {}',
'const mapDispatchToProps = (dispatch) => {}',
'const mapDispatchToProps = (dispatch, ownProps, moreArgs) => {}',
'const mapDispatchToProps = {anAction: anAction}',
],
invalid: [{
code: 'const mapDispatchToProps = (anyOtherName) => {}',
Expand Down

0 comments on commit a68732f

Please sign in to comment.