-
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.
Showing
8 changed files
with
208 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
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,36 @@ | ||
# Enforces that mapStateToProps does not bind complete store to a component. (react-redux/mapStateToProps-no-store) | ||
|
||
Passing whole state to a component is a bd practice. Triggering unnecessary re-renders. | ||
Instead one should provide specific properties used by a component. | ||
|
||
## Rule details | ||
|
||
The following patterns are considered incorrect: | ||
|
||
```js | ||
const mapStateToProps = (state) => state | ||
``` | ||
|
||
```js | ||
const mapStateToProps = state => { | ||
return {state: state} | ||
} | ||
``` | ||
|
||
```js | ||
connect((state) => state, null)(App) | ||
``` | ||
|
||
The following patterns are correct: | ||
|
||
```js | ||
const mapStateToProps = () => {} | ||
``` | ||
|
||
```js | ||
const mapStateToProps = (state) => {isActive: state.isActive} | ||
``` | ||
|
||
```js | ||
connect((state) => ({isActive: state.isActive}), null)(App) | ||
``` |
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,51 @@ | ||
const utils = require('../utils'); | ||
const isReactReduxConnect = require('../isReactReduxConnect'); | ||
|
||
const report = function (context, node) { | ||
context.report({ | ||
message: 'mapStateToProps should not return complete store object', | ||
node, | ||
}); | ||
}; | ||
|
||
const getFirstParamName = node => | ||
node.params && node.params[0] && node.params[0].name; | ||
|
||
const checkFunction = function (context, body, firstParamName) { | ||
const returnNode = utils.getReturnNode(body); | ||
// return state; | ||
if (returnNode && returnNode.type === 'Identifier' && returnNode.name === firstParamName) { | ||
report(context, body); | ||
} | ||
// return {store: state}; | ||
if (returnNode && returnNode.type === 'ObjectExpression' && | ||
returnNode.properties.reduce((acc, cv) => | ||
acc || (cv.value.name === firstParamName), false)) { | ||
report(context, body); | ||
} | ||
}; | ||
|
||
module.exports = function (context) { | ||
return { | ||
VariableDeclaration(node) { | ||
node.declarations.forEach((decl) => { | ||
if (decl.id && decl.id.name === 'mapStateToProps') { | ||
const body = decl.init.body; | ||
const firxtParamName = decl.init.params && | ||
decl.init.params[0] && | ||
decl.init.params[0].name; | ||
checkFunction(context, body, firxtParamName); | ||
} | ||
}); | ||
}, | ||
FunctionDeclaration(node) { | ||
checkFunction(context, node.body, getFirstParamName(node)); | ||
}, | ||
CallExpression(node) { | ||
if (isReactReduxConnect(node)) { | ||
const mapStateToProps = node.arguments && node.arguments[0]; | ||
checkFunction(context, mapStateToProps.body, getFirstParamName(mapStateToProps)); | ||
} | ||
}, | ||
}; | ||
}; |
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,18 @@ | ||
'use strict'; | ||
|
||
const getReturnNode = (node) => { | ||
const body = node.body; | ||
if (!body || !body.length) { | ||
return node; | ||
} | ||
for (let i = body.length - 1; i >= 0; i -= 1) { | ||
if (body[i].type === 'ReturnStatement') { | ||
return body[i].argument; | ||
} | ||
} | ||
return null; | ||
}; | ||
|
||
module.exports = { | ||
getReturnNode, | ||
}; |
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,96 @@ | ||
require('babel-eslint'); | ||
|
||
const rule = require('../../../lib/rules/mapStateToProps-no-store'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const parserOptions = { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
}, | ||
}; | ||
|
||
const ruleTester = new RuleTester({ parserOptions }); | ||
|
||
ruleTester.run('mapStateToProps-no-store', rule, { | ||
valid: [ | ||
'connect((state) => ({isActive: state.isActive}), null)(App)', | ||
`connect( | ||
(state) => { | ||
return { | ||
isActive: state.isActive | ||
} | ||
}, | ||
null | ||
)(App) | ||
`, | ||
`connect(function(state){ | ||
return { | ||
isActive: state.isActive | ||
} | ||
}, | ||
null | ||
)(App) | ||
`, | ||
`function mapStateToProps(state) { | ||
return {}; | ||
}`, | ||
`const mapStateToProps = function(state) { | ||
return state.isActive; | ||
}`, | ||
'const mapStateToProps = (state, ownProps) => {}', | ||
'const mapStateToProps = (state) => {isActive: state.isActive}', | ||
], | ||
invalid: [{ | ||
code: 'const mapStateToProps = (state) => state', | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps should not return complete store object', | ||
}, | ||
], | ||
}, { | ||
code: `const mapStateToProps = state => { | ||
return {state: state} | ||
}`, | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps should not return complete store object', | ||
}, | ||
], | ||
}, { | ||
code: `function mapStateToProps(state) { | ||
return state; | ||
}`, | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps should not return complete store object', | ||
}, | ||
], | ||
}, { | ||
code: `export default connect( | ||
(state) => { | ||
return { | ||
state: state | ||
} | ||
}, | ||
(dispatch) => { | ||
return { | ||
actions: bindActionCreators(actions, dispatch) | ||
} | ||
} | ||
)(App)`, | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps should not return complete store object', | ||
}, | ||
], | ||
}, { | ||
code: 'connect((state) => state, null)(App)', | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps should not return complete store object', | ||
}, | ||
], | ||
}], | ||
}); |