Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: DianaSuvorova/eslint-plugin-react-redux
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.2.0
Choose a base ref
...
head repository: DianaSuvorova/eslint-plugin-react-redux
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.2.1
Choose a head ref
  • 1 commit
  • 23 files changed
  • 1 contributor

Commits on Mar 16, 2025

  1. fix: don't lint connect() of not redux (#103)

    * fix: don't lint connect() of not redux
    
    Signed-off-by: koooge <koooooge@gmail.com>
    
    * fix: Lint connect() for cjs
    
    Signed-off-by: koooge <koooooge@gmail.com>
    
    ---------
    
    Signed-off-by: koooge <koooooge@gmail.com>
    koooge authored Mar 16, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    55b9b31 View commit details
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
@@ -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);
}
2 changes: 1 addition & 1 deletion lib/rules/connect-prefer-named-arguments.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
@@ -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' ||
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
@@ -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' ||
2 changes: 1 addition & 1 deletion lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
@@ -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' ||
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-no-store.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-hoisted.js
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
@@ -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' ||
2 changes: 1 addition & 1 deletion lib/rules/mapStateToProps-prefer-selectors.js
Original file line number Diff line number Diff line change
@@ -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' ||
4 changes: 2 additions & 2 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
@@ -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];
@@ -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
@@ -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 &&
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
@@ -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.',
17 changes: 10 additions & 7 deletions tests/lib/rules/connect-prefer-named-arguments.js
Original file line number Diff line number Diff line change
@@ -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',
@@ -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',
@@ -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',
18 changes: 13 additions & 5 deletions tests/lib/rules/mapDispatchToProps-prefer-parameters-names.js
Original file line number Diff line number Diff line change
@@ -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) => {}',
@@ -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',
5 changes: 3 additions & 2 deletions tests/lib/rules/mapDispatchToProps-prefer-shorthand.js
Original file line number Diff line number Diff line change
@@ -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: [{
11 changes: 7 additions & 4 deletions tests/lib/rules/mapDispatchToProps-returns-object.js
Original file line number Diff line number Diff line change
@@ -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,
}),
@@ -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) }
@@ -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,
}),
@@ -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,
}),
31 changes: 18 additions & 13 deletions tests/lib/rules/mapStateToProps-no-store.js
Original file line number Diff line number Diff line change
@@ -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
@@ -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
}
@@ -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',
@@ -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
@@ -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: [
{
@@ -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',
Loading