-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mapDispatchToProps-prefer-object
feat: mapDispatchToProps-prefer-object
- Loading branch information
1 parent
515ec5d
commit a68732f
Showing
7 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters