Skip to content
Merged
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
20 changes: 16 additions & 4 deletions lib/isReactReduxConnect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
module.exports = function (node) {
return (
node.callee.name === 'connect'
);
module.exports = function (node, context) {
if (node.callee.type === 'Identifier' && node.callee.name === 'connect') {
const sourceCode = context.getSourceCode();
const scope = sourceCode.getScope(node);
const variable = scope.variables.find(v => v.name === 'connect');
if (variable && variable.defs.length > 0) {
const def = variable.defs[0];
if (
(def.node.type === 'ImportSpecifier' && def.parent.source.value === 'react-redux') ||
(def.node.type === 'VariableDeclarator' && def.node.init && def.node.init.callee && def.node.init.callee.name === 'require' && def.node.init.arguments[0].value === 'react-redux')
) {
return true;
}
}
}
return false;
};
2 changes: 1 addition & 1 deletion lib/rules/connect-prefer-minimum-two-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const create = function (context) {

return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
if (node.arguments.length < 2) {
report(node);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/connect-prefer-named-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const create = function (context) {

return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
node.arguments.forEach((argument, i) => {
if (argument.raw && argument.raw !== 'null') {
report(node, i);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapDispatchToProps = node.arguments && node.arguments[1];
if (mapDispatchToProps && (
mapDispatchToProps.type === 'ArrowFunctionExpression' ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapDispatchToProps = node.arguments && node.arguments[1];
if (mapDispatchToProps && (
mapDispatchToProps.type === 'ArrowFunctionExpression' ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapDispatchToProps = node.arguments && node.arguments[1];
if (mapDispatchToProps && (
mapDispatchToProps.type === 'ArrowFunctionExpression' ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-no-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapStateToProps = node.arguments && node.arguments[0];
if (mapStateToProps && mapStateToProps.body) {
const firstParamName = getFirstParamName(mapStateToProps);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-hoisted.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapStateToProps = node.arguments && node.arguments[0];
if (mapStateToProps && mapStateToProps.body) {
checkFunction(context, mapStateToProps.body);
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapStateToProps = node.arguments && node.arguments[0];
if (mapStateToProps && (
mapStateToProps.type === 'ArrowFunctionExpression' ||
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const create = function (context) {
}
},
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const mapStateToProps = node.arguments && node.arguments[0];
if (mapStateToProps && (
mapStateToProps.type === 'ArrowFunctionExpression' ||
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const belongsToReduxReact = (node, objectName, destrArg) => {
}
}
} else if (node.type === 'CallExpression') {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const check = (mapToProps) => {
if (mapToProps && mapToProps.body) {
const secondArgument = mapToProps.params && mapToProps.params[1];
Expand Down Expand Up @@ -85,4 +85,4 @@ const getPropNameFromReduxRuleMessage = message => message.replace('exclude:', '
module.exports = filterReports([
propsUsedInRedux,
noUnusedPropTypesReact,
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);
2 changes: 1 addition & 1 deletion lib/rules/prefer-separate-component-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
const sourceCode = context.sourceCode ?? context.getSourceCode();
return {
CallExpression(node) {
if (isReactReduxConnect(node)) {
if (isReactReduxConnect(node, context)) {
const component =
node.parent &&
node.parent.arguments &&
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/connect-prefer-minimum-two-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ const ruleTester = new RuleTester( parserOptions );
ruleTester.run('connect-prefer-minimum-two-arguments', rule, {
valid: [
...codeSamples,
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
'connect(mapStateToProps, mapDispatchToProps)(Component)',
'connect({prop1, prop2}, {action1, action2})(Component)',
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)`,
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps)(Component)`,
`import { connect } from 'react-redux'; connect({prop1, prop2}, {action1, action2})(Component)`,
],
invalid: [{
code: 'connect(mapStateToProps)(Component)',
code: `import { connect } from 'react-redux'; connect(mapStateToProps)(Component)`,
errors: [
{
message: 'Connect function should have at least 2 arguments.',
Expand Down
17 changes: 10 additions & 7 deletions tests/lib/rules/connect-prefer-named-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ const ruleTester = new RuleTester( parserOptions );
ruleTester.run('connect-prefer-named-arguments', rule, {
valid: [
...codeSamples,
'export default connect(null, mapDispatchToProps)(TodoApp)',
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
'connect(mapStateToProps, mapDispatchToProps)(Component)',
'connect()(TodoApp)',
`import { connect } from 'react-redux'; export default connect(null, mapDispatchToProps)(TodoApp)`,
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)`,
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps)(Component)`,
`import { connect } from 'react-redux'; connect()(TodoApp)`,
'connect(() => {}, () => {}, mergeProps, options)(Component)',
'connect({}, {})(Component)',
'connect(state => state)(TodoApp)',
],
invalid: [{
code: 'connect(() => {}, () => {}, mergeProps, options)(Component)',
code: `import { connect } from 'react-redux'; connect(() => {}, () => {}, mergeProps, options)(Component)`,
errors: [
{
message: 'Connect function argument #1 should be named mapStateToProps',
Expand All @@ -28,7 +31,7 @@ ruleTester.run('connect-prefer-named-arguments', rule, {
},
],
}, {
code: 'connect({}, {})(Component)',
code: `import { connect } from 'react-redux'; connect({}, {})(Component)`,
errors: [
{
message: 'Connect function argument #1 should be named mapStateToProps',
Expand All @@ -37,7 +40,7 @@ ruleTester.run('connect-prefer-named-arguments', rule, {
},
],
}, {
code: 'connect(state => state)(TodoApp)',
code: `import { connect } from 'react-redux'; connect(state => state)(TodoApp)`,
errors: [
{
message: 'Connect function argument #1 should be named mapStateToProps',
Expand Down
18 changes: 13 additions & 5 deletions tests/lib/rules/mapDispatchToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
'const mapDispatchToProps = (dispatch) => {}',
'const mapDispatchToProps = (dispatch, ownProps, moreArgs) => {}',
'const mapDispatchToProps = {anAction: anAction}',
'connect((state) => state, {anAction: anAction})(App)',
'connect(null, null)(App)',
'connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App)',
`import { connect } from 'react-redux'; connect((state) => state, {anAction: anAction})(App)`,
`import { connect } from 'react-redux'; connect(null, null)(App)`,
`import { connect } from 'react-redux'; connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App)`,
`import { connect } from './path/to/connect.js'; connect('something')`,
`import { connect } from './path/to/connect.js'; connect((state) => state, (anyOtherName) => {})(App)`,
'function mapDispatchToProps(dispatch, ownProps) {}',

],
invalid: [{
code: 'const mapDispatchToProps = (anyOtherName) => {}',
Expand All @@ -48,7 +49,14 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
},
],
}, {
code: 'connect((state) => state, (anyOtherName) => {})(App)',
code: `import { connect } from 'react-redux'; connect((state) => state, (anyOtherName) => {})(App)`,
errors: [
{
message: 'mapDispatchToProps function parameter #0 should be named dispatch',
},
],
}, {
code: `const { connect } = require('react-redux'); connect((state) => state, (anyOtherName) => {})(App)`,
errors: [
{
message: 'mapDispatchToProps function parameter #0 should be named dispatch',
Expand Down
5 changes: 3 additions & 2 deletions tests/lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
'const mapDispatchToProps = actionsMap',
'const mapDispatchToProps = {...actions}',
'const mapDispatchToProps = {anAction: anAction}',
`export default connect(
`import { connect } from 'react-redux';
export default connect(
state => ({
productsList: state.Products.productsList,
}),
{ fetchProducts }
)(Products);
`,
'connect(null, null)(App)',
`import { connect } from 'react-redux'; connect(null, null)(App)`,
'function mapDispatchToProps () {return aThing}',
],
invalid: [{
Expand Down
11 changes: 7 additions & 4 deletions tests/lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
'const mapDispatchToProps = actionsMap',
'const mapDispatchToProps = {...actions}',
'const mapDispatchToProps = {anAction: anAction}',
`export default connect(
`import { connect } from 'react-redux';
export default connect(
state => ({
productsList: state.Products.productsList,
}),
Expand All @@ -42,7 +43,7 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
}
}
}`,
'connect(null, null)(App)',
`import { connect } from 'react-redux'; connect(null, null)(App)`,
'function mapDispatchToProps () {return aThing}',
`function mapDispatchToProps(dispatch) {
return { actions: bindActionCreators(actionCreators, dispatch) }
Expand All @@ -63,7 +64,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
},
],
}, {
code: `export default connect(
code: `import { connect } from 'react-redux';
export default connect(
state => ({
productsList: state.Products.productsList,
}),
Expand All @@ -76,7 +78,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
},
],
}, {
code: `export default connect(
code: `import { connect } from 'react-redux';
export default connect(
state => ({
productsList: state.Products.productsList,
}),
Expand Down
31 changes: 18 additions & 13 deletions tests/lib/rules/mapStateToProps-no-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ ruleTester.run('mapStateToProps-no-store', rule, {
});
`,
'export default function observeStore(store) {return store;}',
'export default connect(() => {})(Alert)',
'export default connect(null, null)(Alert)',
'connect((state) => ({isActive: state.isActive}), null)(App)',
'connect(null, null)(App)',
`connect(
`import { connect } from 'react-redux'; export default connect(() => {})(Alert)`,
`import { connect } from 'react-redux'; export default connect(null, null)(Alert)`,
`import { connect } from 'react-redux'; connect((state) => ({isActive: state.isActive}), null)(App)`,
`import { connect } from 'react-redux'; connect(null, null)(App)`,
`import { connect } from 'react-redux';
connect(
(state) => {
return {
isActive: state.isActive
Expand All @@ -42,7 +43,8 @@ ruleTester.run('mapStateToProps-no-store', rule, {
null
)(App)
`,
`connect(function(state){
`import { connect } from 'react-redux';
connect(function(state){
return {
isActive: state.isActive
}
Expand All @@ -58,14 +60,15 @@ ruleTester.run('mapStateToProps-no-store', rule, {
}`,
'const mapStateToProps = (state, ownProps) => {}',
'const mapStateToProps = (state) => {isActive: state.isActive}',
`const mapStateToProps = (state, ownProps) => {};
`import { connect } from 'react-redux';
const mapStateToProps = (state, ownProps) => {};
connect(mapStateToProps, null)(Alert);`,
`const mapStateToProps = ({ header }) => ({
isLoggedIn: header.user && header.user.isLoggedIn,
}); `,
'const mapStateToProps = ({header}, ownProps) => {header};',
'connect(({header}, ownProps) => {header})(App);',
'connect(({header}, {ownProp1}) => {header, ownProp1})(App);',
`import { connect } from 'react-redux'; connect(({header}, ownProps) => {header})(App);`,
`import { connect } from 'react-redux'; connect(({header}, {ownProp1}) => {header, ownProp1})(App);`,
],
invalid: [{
code: 'const mapStateToProps = (state) => state',
Expand Down Expand Up @@ -93,7 +96,8 @@ ruleTester.run('mapStateToProps-no-store', rule, {
},
],
}, {
code: `export default connect(
code: `import { connect } from 'react-redux';
export default connect(
(state) => {
return {
state: state
Expand All @@ -111,14 +115,15 @@ ruleTester.run('mapStateToProps-no-store', rule, {
},
],
}, {
code: 'connect((state) => state, null)(App)',
code: `import { connect } from 'react-redux'; connect((state) => state, null)(App)`,
errors: [
{
message: 'mapStateToProps should not return complete store object',
},
],
}, {
code: `const mapStateToProps = (state, ownProps) => state;
code: `import { connect } from 'react-redux';
const mapStateToProps = (state, ownProps) => state;
connect(mapStateToProps, null)(Alert);`,
errors: [
{
Expand All @@ -133,7 +138,7 @@ ruleTester.run('mapStateToProps-no-store', rule, {
},
],
}, {
code: 'connect((state) => ({...state}), null)(App)',
code: `import { connect } from 'react-redux'; connect((state) => ({...state}), null)(App)`,
errors: [
{
message: 'mapStateToProps should not return complete store object',
Expand Down
Loading