Skip to content

Commit 55b9b31

Browse files
authored
fix: don't lint connect() of not redux (DianaSuvorova#103)
* fix: don't lint connect() of not redux Signed-off-by: koooge <[email protected]> * fix: Lint connect() for cjs Signed-off-by: koooge <[email protected]> --------- Signed-off-by: koooge <[email protected]>
1 parent ea56e11 commit 55b9b31

23 files changed

+134
-82
lines changed

lib/isReactReduxConnect.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1-
module.exports = function (node) {
2-
return (
3-
node.callee.name === 'connect'
4-
);
1+
module.exports = function (node, context) {
2+
if (node.callee.type === 'Identifier' && node.callee.name === 'connect') {
3+
const sourceCode = context.getSourceCode();
4+
const scope = sourceCode.getScope(node);
5+
const variable = scope.variables.find(v => v.name === 'connect');
6+
if (variable && variable.defs.length > 0) {
7+
const def = variable.defs[0];
8+
if (
9+
(def.node.type === 'ImportSpecifier' && def.parent.source.value === 'react-redux') ||
10+
(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')
11+
) {
12+
return true;
13+
}
14+
}
15+
}
16+
return false;
517
};

lib/rules/connect-prefer-minimum-two-arguments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const create = function (context) {
1010

1111
return {
1212
CallExpression(node) {
13-
if (isReactReduxConnect(node)) {
13+
if (isReactReduxConnect(node, context)) {
1414
if (node.arguments.length < 2) {
1515
report(node);
1616
}

lib/rules/connect-prefer-named-arguments.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const create = function (context) {
1717

1818
return {
1919
CallExpression(node) {
20-
if (isReactReduxConnect(node)) {
20+
if (isReactReduxConnect(node, context)) {
2121
node.arguments.forEach((argument, i) => {
2222
if (argument.raw && argument.raw !== 'null') {
2323
report(node, i);

lib/rules/mapDispatchToProps-prefer-parameters-names.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const create = function (context) {
4040
}
4141
},
4242
CallExpression(node) {
43-
if (isReactReduxConnect(node)) {
43+
if (isReactReduxConnect(node, context)) {
4444
const mapDispatchToProps = node.arguments && node.arguments[1];
4545
if (mapDispatchToProps && (
4646
mapDispatchToProps.type === 'ArrowFunctionExpression' ||

lib/rules/mapDispatchToProps-prefer-shorthand.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const create = function (context) {
7171
}
7272
},
7373
CallExpression(node) {
74-
if (isReactReduxConnect(node)) {
74+
if (isReactReduxConnect(node, context)) {
7575
const mapDispatchToProps = node.arguments && node.arguments[1];
7676
if (mapDispatchToProps && (
7777
mapDispatchToProps.type === 'ArrowFunctionExpression' ||

lib/rules/mapDispatchToProps-returns-object.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const create = function (context) {
3434
}
3535
},
3636
CallExpression(node) {
37-
if (isReactReduxConnect(node)) {
37+
if (isReactReduxConnect(node, context)) {
3838
const mapDispatchToProps = node.arguments && node.arguments[1];
3939
if (mapDispatchToProps && (
4040
mapDispatchToProps.type === 'ArrowFunctionExpression' ||

lib/rules/mapStateToProps-no-store.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const create = function (context) {
6666
}
6767
},
6868
CallExpression(node) {
69-
if (isReactReduxConnect(node)) {
69+
if (isReactReduxConnect(node, context)) {
7070
const mapStateToProps = node.arguments && node.arguments[0];
7171
if (mapStateToProps && mapStateToProps.body) {
7272
const firstParamName = getFirstParamName(mapStateToProps);

lib/rules/mapStateToProps-prefer-hoisted.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const create = function (context) {
5252
}
5353
},
5454
CallExpression(node) {
55-
if (isReactReduxConnect(node)) {
55+
if (isReactReduxConnect(node, context)) {
5656
const mapStateToProps = node.arguments && node.arguments[0];
5757
if (mapStateToProps && mapStateToProps.body) {
5858
checkFunction(context, mapStateToProps.body);

lib/rules/mapStateToProps-prefer-parameters-names.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const create = function (context) {
4040
}
4141
},
4242
CallExpression(node) {
43-
if (isReactReduxConnect(node)) {
43+
if (isReactReduxConnect(node, context)) {
4444
const mapStateToProps = node.arguments && node.arguments[0];
4545
if (mapStateToProps && (
4646
mapStateToProps.type === 'ArrowFunctionExpression' ||

lib/rules/mapStateToProps-prefer-selectors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const create = function (context) {
8383
}
8484
},
8585
CallExpression(node) {
86-
if (isReactReduxConnect(node)) {
86+
if (isReactReduxConnect(node, context)) {
8787
const mapStateToProps = node.arguments && node.arguments[0];
8888
if (mapStateToProps && (
8989
mapStateToProps.type === 'ArrowFunctionExpression' ||

lib/rules/no-unused-prop-types.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const belongsToReduxReact = (node, objectName, destrArg) => {
3333
}
3434
}
3535
} else if (node.type === 'CallExpression') {
36-
if (isReactReduxConnect(node)) {
36+
if (isReactReduxConnect(node, context)) {
3737
const check = (mapToProps) => {
3838
if (mapToProps && mapToProps.body) {
3939
const secondArgument = mapToProps.params && mapToProps.params[1];
@@ -85,4 +85,4 @@ const getPropNameFromReduxRuleMessage = message => message.replace('exclude:', '
8585
module.exports = filterReports([
8686
propsUsedInRedux,
8787
noUnusedPropTypesReact,
88-
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);
88+
], getPropNameFromReactRuleMessage, getPropNameFromReduxRuleMessage);

lib/rules/prefer-separate-component-file.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = {
1212
const sourceCode = context.sourceCode ?? context.getSourceCode();
1313
return {
1414
CallExpression(node) {
15-
if (isReactReduxConnect(node)) {
15+
if (isReactReduxConnect(node, context)) {
1616
const component =
1717
node.parent &&
1818
node.parent.arguments &&

tests/lib/rules/connect-prefer-minimum-two-arguments.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ const ruleTester = new RuleTester( parserOptions );
1313
ruleTester.run('connect-prefer-minimum-two-arguments', rule, {
1414
valid: [
1515
...codeSamples,
16-
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
17-
'connect(mapStateToProps, mapDispatchToProps)(Component)',
18-
'connect({prop1, prop2}, {action1, action2})(Component)',
16+
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)`,
17+
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps)(Component)`,
18+
`import { connect } from 'react-redux'; connect({prop1, prop2}, {action1, action2})(Component)`,
1919
],
2020
invalid: [{
21-
code: 'connect(mapStateToProps)(Component)',
21+
code: `import { connect } from 'react-redux'; connect(mapStateToProps)(Component)`,
2222
errors: [
2323
{
2424
message: 'Connect function should have at least 2 arguments.',

tests/lib/rules/connect-prefer-named-arguments.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ const ruleTester = new RuleTester( parserOptions );
1313
ruleTester.run('connect-prefer-named-arguments', rule, {
1414
valid: [
1515
...codeSamples,
16-
'export default connect(null, mapDispatchToProps)(TodoApp)',
17-
'connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)',
18-
'connect(mapStateToProps, mapDispatchToProps)(Component)',
19-
'connect()(TodoApp)',
16+
`import { connect } from 'react-redux'; export default connect(null, mapDispatchToProps)(TodoApp)`,
17+
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps, mergeProps, options)(Component)`,
18+
`import { connect } from 'react-redux'; connect(mapStateToProps, mapDispatchToProps)(Component)`,
19+
`import { connect } from 'react-redux'; connect()(TodoApp)`,
20+
'connect(() => {}, () => {}, mergeProps, options)(Component)',
21+
'connect({}, {})(Component)',
22+
'connect(state => state)(TodoApp)',
2023
],
2124
invalid: [{
22-
code: 'connect(() => {}, () => {}, mergeProps, options)(Component)',
25+
code: `import { connect } from 'react-redux'; connect(() => {}, () => {}, mergeProps, options)(Component)`,
2326
errors: [
2427
{
2528
message: 'Connect function argument #1 should be named mapStateToProps',
@@ -28,7 +31,7 @@ ruleTester.run('connect-prefer-named-arguments', rule, {
2831
},
2932
],
3033
}, {
31-
code: 'connect({}, {})(Component)',
34+
code: `import { connect } from 'react-redux'; connect({}, {})(Component)`,
3235
errors: [
3336
{
3437
message: 'Connect function argument #1 should be named mapStateToProps',
@@ -37,7 +40,7 @@ ruleTester.run('connect-prefer-named-arguments', rule, {
3740
},
3841
],
3942
}, {
40-
code: 'connect(state => state)(TodoApp)',
43+
code: `import { connect } from 'react-redux'; connect(state => state)(TodoApp)`,
4144
errors: [
4245
{
4346
message: 'Connect function argument #1 should be named mapStateToProps',

tests/lib/rules/mapDispatchToProps-prefer-parameters-names.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
1818
'const mapDispatchToProps = (dispatch) => {}',
1919
'const mapDispatchToProps = (dispatch, ownProps, moreArgs) => {}',
2020
'const mapDispatchToProps = {anAction: anAction}',
21-
'connect((state) => state, {anAction: anAction})(App)',
22-
'connect(null, null)(App)',
23-
'connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App)',
21+
`import { connect } from 'react-redux'; connect((state) => state, {anAction: anAction})(App)`,
22+
`import { connect } from 'react-redux'; connect(null, null)(App)`,
23+
`import { connect } from 'react-redux'; connect((state) => state, (dispatch, ownProps, moreArgs) => {})(App)`,
24+
`import { connect } from './path/to/connect.js'; connect('something')`,
25+
`import { connect } from './path/to/connect.js'; connect((state) => state, (anyOtherName) => {})(App)`,
2426
'function mapDispatchToProps(dispatch, ownProps) {}',
25-
2627
],
2728
invalid: [{
2829
code: 'const mapDispatchToProps = (anyOtherName) => {}',
@@ -48,7 +49,14 @@ ruleTester.run('mapDispatchToProps-prefer-parameters-names', rule, {
4849
},
4950
],
5051
}, {
51-
code: 'connect((state) => state, (anyOtherName) => {})(App)',
52+
code: `import { connect } from 'react-redux'; connect((state) => state, (anyOtherName) => {})(App)`,
53+
errors: [
54+
{
55+
message: 'mapDispatchToProps function parameter #0 should be named dispatch',
56+
},
57+
],
58+
}, {
59+
code: `const { connect } = require('react-redux'); connect((state) => state, (anyOtherName) => {})(App)`,
5260
errors: [
5361
{
5462
message: 'mapDispatchToProps function parameter #0 should be named dispatch',

tests/lib/rules/mapDispatchToProps-prefer-shorthand.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ ruleTester.run('mapDispatchToProps-prefer-shorthand', rule, {
2727
'const mapDispatchToProps = actionsMap',
2828
'const mapDispatchToProps = {...actions}',
2929
'const mapDispatchToProps = {anAction: anAction}',
30-
`export default connect(
30+
`import { connect } from 'react-redux';
31+
export default connect(
3132
state => ({
3233
productsList: state.Products.productsList,
3334
}),
3435
{ fetchProducts }
3536
)(Products);
3637
`,
37-
'connect(null, null)(App)',
38+
`import { connect } from 'react-redux'; connect(null, null)(App)`,
3839
'function mapDispatchToProps () {return aThing}',
3940
],
4041
invalid: [{

tests/lib/rules/mapDispatchToProps-returns-object.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
1717
'const mapDispatchToProps = actionsMap',
1818
'const mapDispatchToProps = {...actions}',
1919
'const mapDispatchToProps = {anAction: anAction}',
20-
`export default connect(
20+
`import { connect } from 'react-redux';
21+
export default connect(
2122
state => ({
2223
productsList: state.Products.productsList,
2324
}),
@@ -42,7 +43,7 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
4243
}
4344
}
4445
}`,
45-
'connect(null, null)(App)',
46+
`import { connect } from 'react-redux'; connect(null, null)(App)`,
4647
'function mapDispatchToProps () {return aThing}',
4748
`function mapDispatchToProps(dispatch) {
4849
return { actions: bindActionCreators(actionCreators, dispatch) }
@@ -63,7 +64,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
6364
},
6465
],
6566
}, {
66-
code: `export default connect(
67+
code: `import { connect } from 'react-redux';
68+
export default connect(
6769
state => ({
6870
productsList: state.Products.productsList,
6971
}),
@@ -76,7 +78,8 @@ ruleTester.run('mapDispatchToProps-returns-object', rule, {
7678
},
7779
],
7880
}, {
79-
code: `export default connect(
81+
code: `import { connect } from 'react-redux';
82+
export default connect(
8083
state => ({
8184
productsList: state.Products.productsList,
8285
}),

tests/lib/rules/mapStateToProps-no-store.js

+18-13
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ ruleTester.run('mapStateToProps-no-store', rule, {
2929
});
3030
`,
3131
'export default function observeStore(store) {return store;}',
32-
'export default connect(() => {})(Alert)',
33-
'export default connect(null, null)(Alert)',
34-
'connect((state) => ({isActive: state.isActive}), null)(App)',
35-
'connect(null, null)(App)',
36-
`connect(
32+
`import { connect } from 'react-redux'; export default connect(() => {})(Alert)`,
33+
`import { connect } from 'react-redux'; export default connect(null, null)(Alert)`,
34+
`import { connect } from 'react-redux'; connect((state) => ({isActive: state.isActive}), null)(App)`,
35+
`import { connect } from 'react-redux'; connect(null, null)(App)`,
36+
`import { connect } from 'react-redux';
37+
connect(
3738
(state) => {
3839
return {
3940
isActive: state.isActive
@@ -42,7 +43,8 @@ ruleTester.run('mapStateToProps-no-store', rule, {
4243
null
4344
)(App)
4445
`,
45-
`connect(function(state){
46+
`import { connect } from 'react-redux';
47+
connect(function(state){
4648
return {
4749
isActive: state.isActive
4850
}
@@ -58,14 +60,15 @@ ruleTester.run('mapStateToProps-no-store', rule, {
5860
}`,
5961
'const mapStateToProps = (state, ownProps) => {}',
6062
'const mapStateToProps = (state) => {isActive: state.isActive}',
61-
`const mapStateToProps = (state, ownProps) => {};
63+
`import { connect } from 'react-redux';
64+
const mapStateToProps = (state, ownProps) => {};
6265
connect(mapStateToProps, null)(Alert);`,
6366
`const mapStateToProps = ({ header }) => ({
6467
isLoggedIn: header.user && header.user.isLoggedIn,
6568
}); `,
6669
'const mapStateToProps = ({header}, ownProps) => {header};',
67-
'connect(({header}, ownProps) => {header})(App);',
68-
'connect(({header}, {ownProp1}) => {header, ownProp1})(App);',
70+
`import { connect } from 'react-redux'; connect(({header}, ownProps) => {header})(App);`,
71+
`import { connect } from 'react-redux'; connect(({header}, {ownProp1}) => {header, ownProp1})(App);`,
6972
],
7073
invalid: [{
7174
code: 'const mapStateToProps = (state) => state',
@@ -93,7 +96,8 @@ ruleTester.run('mapStateToProps-no-store', rule, {
9396
},
9497
],
9598
}, {
96-
code: `export default connect(
99+
code: `import { connect } from 'react-redux';
100+
export default connect(
97101
(state) => {
98102
return {
99103
state: state
@@ -111,14 +115,15 @@ ruleTester.run('mapStateToProps-no-store', rule, {
111115
},
112116
],
113117
}, {
114-
code: 'connect((state) => state, null)(App)',
118+
code: `import { connect } from 'react-redux'; connect((state) => state, null)(App)`,
115119
errors: [
116120
{
117121
message: 'mapStateToProps should not return complete store object',
118122
},
119123
],
120124
}, {
121-
code: `const mapStateToProps = (state, ownProps) => state;
125+
code: `import { connect } from 'react-redux';
126+
const mapStateToProps = (state, ownProps) => state;
122127
connect(mapStateToProps, null)(Alert);`,
123128
errors: [
124129
{
@@ -133,7 +138,7 @@ ruleTester.run('mapStateToProps-no-store', rule, {
133138
},
134139
],
135140
}, {
136-
code: 'connect((state) => ({...state}), null)(App)',
141+
code: `import { connect } from 'react-redux'; connect((state) => ({...state}), null)(App)`,
137142
errors: [
138143
{
139144
message: 'mapStateToProps should not return complete store object',

0 commit comments

Comments
 (0)