eslint-plugin-react-redux
Advanced tools
Comparing version
@@ -17,2 +17,6 @@ # Enforces that all mapDispatchToProps parameters have specific names. (react-redux/mapDispatchToProps-prefer-named-parameters) | ||
```js | ||
connect((state) => state, (anyOtherName) => {})(App) | ||
``` | ||
The following patterns are considered correct: | ||
@@ -31,1 +35,5 @@ | ||
``` | ||
```js | ||
connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App) | ||
``` |
@@ -17,2 +17,6 @@ # Enforces that all mapStateToProps parameters have specific names. (react-redux/mapStateToProps-prefer-named-parameters) | ||
```js | ||
connect(function(anyOtherName) {}, null)(App) | ||
``` | ||
The following patterns are considered correct: | ||
@@ -27,1 +31,5 @@ | ||
``` | ||
```js | ||
connect((state) => state, null)(App) | ||
``` |
@@ -33,4 +33,5 @@ const isReactReduxConnect = require('../isReactReduxConnect'); | ||
const mapDispatchToProps = node.arguments && node.arguments[1]; | ||
if (mapDispatchToProps.type === 'ArrowFunctionExpression' || | ||
mapDispatchToProps.type === 'FunctionExpression' | ||
if (mapDispatchToProps && ( | ||
mapDispatchToProps.type === 'ArrowFunctionExpression' || | ||
mapDispatchToProps.type === 'FunctionExpression') | ||
) { | ||
@@ -37,0 +38,0 @@ report(context, mapDispatchToProps); |
@@ -0,1 +1,3 @@ | ||
const isReactReduxConnect = require('../isReactReduxConnect'); | ||
const argumentNames = [ | ||
@@ -13,12 +15,8 @@ 'dispatch', | ||
const checkDeclaration = function (context, node) { | ||
if (node.id && node.id.name === 'mapDispatchToProps' | ||
&& node.init && node.init.params | ||
) { | ||
node.init.params.forEach((param, i) => { | ||
if (argumentNames[i] && argumentNames[i] !== param.name) { | ||
report(context, param, i); | ||
} | ||
}); | ||
} | ||
const check = function (context, params) { | ||
params.forEach((param, i) => { | ||
if (argumentNames[i] && argumentNames[i] !== param.name) { | ||
report(context, param, i); | ||
} | ||
}); | ||
}; | ||
@@ -29,5 +27,30 @@ | ||
VariableDeclaration(node) { | ||
node.declarations.forEach(decl => checkDeclaration(context, decl)); | ||
node.declarations.forEach((decl) => { | ||
if (decl.id && decl.id.name === 'mapDispatchToProps') { | ||
if (decl.init && ( | ||
decl.init.type === 'ArrowFunctionExpression' || | ||
decl.init.type === 'FunctionExpression' | ||
)) { | ||
check(context, decl.init.params); | ||
} | ||
} | ||
}); | ||
}, | ||
FunctionDeclaration(node) { | ||
if (node.id && node.id.name === 'mapDispatchToProps') { | ||
check(context, node.params); | ||
} | ||
}, | ||
CallExpression(node) { | ||
if (isReactReduxConnect(node)) { | ||
const mapDispatchToProps = node.arguments && node.arguments[1]; | ||
if (mapDispatchToProps && ( | ||
mapDispatchToProps.type === 'ArrowFunctionExpression' || | ||
mapDispatchToProps.type === 'FunctionExpression') | ||
) { | ||
check(context, mapDispatchToProps.params); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
@@ -49,3 +49,3 @@ const utils = require('../utils'); | ||
const mapStateToProps = node.arguments && node.arguments[0]; | ||
if (mapStateToProps.body) { | ||
if (mapStateToProps && mapStateToProps.body) { | ||
checkFunction(context, mapStateToProps.body, getFirstParamName(mapStateToProps)); | ||
@@ -52,0 +52,0 @@ } |
@@ -0,1 +1,3 @@ | ||
const isReactReduxConnect = require('../isReactReduxConnect'); | ||
const argumentNames = [ | ||
@@ -13,12 +15,8 @@ 'state', | ||
const checkDeclaration = function (context, node) { | ||
if (node.id && node.id.name === 'mapStateToProps' | ||
&& node.init && node.init.params | ||
) { | ||
node.init.params.forEach((param, i) => { | ||
if (argumentNames[i] && argumentNames[i] !== param.name) { | ||
report(context, param, i); | ||
} | ||
}); | ||
} | ||
const check = function (context, params) { | ||
params.forEach((param, i) => { | ||
if (argumentNames[i] && argumentNames[i] !== param.name) { | ||
report(context, param, i); | ||
} | ||
}); | ||
}; | ||
@@ -29,5 +27,30 @@ | ||
VariableDeclaration(node) { | ||
node.declarations.forEach(decl => checkDeclaration(context, decl)); | ||
node.declarations.forEach((decl) => { | ||
if (decl.id && decl.id.name === 'mapStateToProps') { | ||
if (decl.init && ( | ||
decl.init.type === 'ArrowFunctionExpression' || | ||
decl.init.type === 'FunctionExpression' | ||
)) { | ||
check(context, decl.init.params); | ||
} | ||
} | ||
}); | ||
}, | ||
FunctionDeclaration(node) { | ||
if (node.id && node.id.name === 'mapStateToProps') { | ||
check(context, node.params); | ||
} | ||
}, | ||
CallExpression(node) { | ||
if (isReactReduxConnect(node)) { | ||
const mapStateToProps = node.arguments && node.arguments[0]; | ||
if (mapStateToProps && ( | ||
mapStateToProps.type === 'ArrowFunctionExpression' || | ||
mapStateToProps.type === 'FunctionExpression') | ||
) { | ||
check(context, mapStateToProps.params); | ||
} | ||
} | ||
}, | ||
}; | ||
}; |
{ | ||
"name": "eslint-plugin-react-redux", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"description": "Enforcing best practices for react-redux", | ||
@@ -16,4 +16,4 @@ "keywords": [ | ||
"test": "npm run lint && mocha tests --recursive", | ||
"test-single": "mocha tests/lib/rules/mapDispatchToProps-prefer-object", | ||
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/mapDispatchToProps-prefer-object", | ||
"test-single": "mocha tests/lib/rules/mapStateToProps-prefer-parameters-names", | ||
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/mapStateToProps-prefer-parameters-names", | ||
"semantic-release": "semantic-release", | ||
@@ -20,0 +20,0 @@ "commitmsg": "npm run test && commitlint -e $GIT_PARAMS" |
@@ -26,2 +26,3 @@ require('babel-eslint'); | ||
`, | ||
'connect(null, null)(App)', | ||
], | ||
@@ -28,0 +29,0 @@ invalid: [{ |
@@ -22,2 +22,7 @@ require('babel-eslint'); | ||
'const mapDispatchToProps = {anAction: anAction}', | ||
'connect((state) => state, {anAction: anAction})(App)', | ||
'connect(null, null)(App)', | ||
'connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App)', | ||
'function mapDispatchToProps(dispatch, ownProps) {}', | ||
], | ||
@@ -40,3 +45,17 @@ invalid: [{ | ||
], | ||
}, { | ||
code: 'function mapDispatchToProps(anyOtherName) {}', | ||
errors: [ | ||
{ | ||
message: 'mapDispatchToProps function parameter #0 should be named dispatch', | ||
}, | ||
], | ||
}, { | ||
code: 'connect((state) => state, (anyOtherName) => {})(App)', | ||
errors: [ | ||
{ | ||
message: 'mapDispatchToProps function parameter #0 should be named dispatch', | ||
}, | ||
], | ||
}], | ||
}); |
@@ -23,2 +23,3 @@ require('babel-eslint'); | ||
'connect((state) => ({isActive: state.isActive}), null)(App)', | ||
'connect(null, null)(App)', | ||
`connect( | ||
@@ -25,0 +26,0 @@ (state) => { |
@@ -21,2 +21,7 @@ require('babel-eslint'); | ||
'const mapStateToProps = (state, ownProps, moreArgs) => {}', | ||
'connect((state) => state, null)(App)', | ||
'function mapStateToProps(state, ownProps) {}', | ||
'connect({state}, null)(App)', | ||
'const mapStateToProps = {}', | ||
'connect(null, null)(App)', | ||
], | ||
@@ -39,3 +44,10 @@ invalid: [{ | ||
], | ||
}, { | ||
code: 'connect(function(anyOtherName) {}, null)(App)', | ||
errors: [ | ||
{ | ||
message: 'mapStateToProps function parameter #0 should be named state', | ||
}, | ||
], | ||
}], | ||
}); |
32970
9.41%798
10.68%