Skip to content

Commit 55b9b31

Browse files
authored
fix: don't lint connect() of not redux (#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

Lines changed: 16 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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' ||

0 commit comments

Comments
 (0)