Skip to content

Commit 786c2da

Browse files
committed
prefer separate component file
1 parent 440c14d commit 786c2da

10 files changed

+79
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ Then configure the rules you want to use under the rules section.
4747
* [react-redux/connect-prefer-named-arguments](docs/rules/connect-prefer-named-arguments.md) Enforces that all connect arguments have specific names.
4848
* [react-redux/mapStateToProps-prefer-parameters-names](docs/rules/mapStateToProps-prefer-parameters-names.md) Enforces that all mapStateToProps parameters have specific names.
4949
* [react-redux/mapDispatchToProps-prefer-parameters-names](docs/rules/mapDispatchToProps-prefer-parameters-names.md) Enforces that all mapDispatchToProps parameters have specific names.
50+
* [react-redux/prefer-separate-component-file](docs/rules/prefer-separate-component-file.md) Enforces that all connected components are defined in a separate file.

docs/rules/connect-prefer-minimum-two-arguments.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This rule enforces that Second argument is provided explicitly.
88

99
## Rule details
1010

11-
The following pattern is considered warnings:
11+
The following pattern is considered incorrect:
1212

1313
```js
1414
connect(mapStateToProps, null, mergeProps)(Component)

docs/rules/connect-prefer-named-arguments.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This rule enforces that all of the provided parameters should follow the above n
1010

1111
## Rule details
1212

13-
The following patterns are considered warnings:
13+
The following patterns are considered incorrect:
1414

1515
```js
1616
connect(mapStateToProps, actionCreators)(TodoApp)
@@ -23,7 +23,7 @@ connect(state => state)(TodoApp)
2323
The following patterns are considered correct:
2424

2525
```js
26-
connect(mapStateToProps, actionCreators, mergeProps)(TodoApp)
26+
connect(mapStateToProps, mapDispatchToProps, mergeProps)(TodoApp)
2727
```
2828

2929
```js

docs/rules/mapDispatchToProps-prefer-parameters-names.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This rule enforces that all of the provided parameters should follow the above n
88

99
## Rule details
1010

11-
The following pattern is considered warnings:
11+
The following pattern is considered incorrect:
1212

1313
```js
1414
const mapDispatchToProps = (anyOtherName) => {}

docs/rules/mapStateToProps-prefer-parameters-names.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This rule enforces that all of the provided parameters should follow the above n
88

99
## Rule details
1010

11-
The following pattern is considered warnings:
11+
The following pattern is considered incorrect:
1212

1313
```js
1414
const mapStateToProps = (anyOtherName) => {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Enforces that all connected components are defined in a separate file (react-redux/prefer-separate-component-file)
2+
3+
And imports it to the container.
4+
5+
## Rule details
6+
7+
The following pattern is considered incorrect:
8+
9+
```js
10+
const Component = () => {};
11+
connect(mapStateToProps, null)(Component)
12+
```
13+
14+
The following patterns are considered correct:
15+
16+
```js
17+
import Component from './component';
18+
connect(mapStateToProps, mapDispatchToProps)(Component)
19+
```
20+
21+
```js
22+
const Component = require('./component')
23+
connect(mapStateToProps, mapDispatchToProps)(Component)
24+
```

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const rules = {
22
'connect-prefer-minimum-two-arguments': require('./lib/rules/connect-prefer-minimum-two-arguments'),
3-
'connect-prefer-named-arguments': require('./lib/rules/connect-prefer-named-parameters'),
3+
'connect-prefer-named-arguments': require('./lib/rules/connect-prefer-named-arguments'),
44
'mapStateToProps-prefer-parameters-names': require('./lib/rules/mapStateToProps-prefer-parameters-names'),
55
'mapDispatchToProps-prefer-parameters-names': require('./lib/rules/mapDispatchToProps-prefer-parameters-names'),
6-
6+
'prefer-separate-component-file': require('./lib/rules/prefer-separate-component-file'),
77
};
88

99
module.exports = {
@@ -14,6 +14,7 @@ module.exports = {
1414
'react-redux/connect-prefer-named-arguments': 2,
1515
'react-redux/mapStateToProps-prefer-parameters-names': 2,
1616
'react-redux/mapDispatchToProps-prefer-parameters-names': 2,
17+
'prefer-separate-component-file/mapDispatchToProps-prefer-parameters-names': 1,
1718
},
1819
all: {
1920
rules,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const isReactReduxConnect = require('../isReactReduxConnect');
2+
3+
const report = function (context, node) {
4+
context.report({
5+
message: 'Connected component should be defined in a separate file.',
6+
node,
7+
});
8+
};
9+
10+
module.exports = function (context) {
11+
return {
12+
CallExpression(node) {
13+
if (isReactReduxConnect(node)) {
14+
const component =
15+
node.parent &&
16+
node.parent.arguments &&
17+
node.parent.arguments[0];
18+
if (component) {
19+
const vars = context.getScope().variables;
20+
vars.forEach((definedVar) => {
21+
if (component.name === definedVar.name) {
22+
definedVar.defs.forEach((def) => {
23+
if (!(def.type === 'ImportBinding' || context.getSource(def.node).includes('require'))) {
24+
report(context, component);
25+
}
26+
});
27+
}
28+
});
29+
}
30+
}
31+
},
32+
};
33+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"scripts": {
1414
"lint": "eslint ./",
1515
"test": "npm run lint && mocha tests --recursive",
16-
"test-single": "mocha tests/lib/rules/connect-prefer-named-arguments",
17-
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/mapDispatchToProps-prefer-parameters-names"
16+
"test-single": "mocha tests/lib/rules/prefer-separate-component-file",
17+
"debug-test": "mocha --debug-brk --inspect tests/lib/rules/prefer-separate-component-file"
1818
},
1919
"repository": {
2020
"type": "git",

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ ruleTester.run('prefer-separate-component-file', rule, {
1919
connect(mapStateToProps, mapDispatchToProps)(Component)`,
2020
`const Component = require('./component')
2121
connect(mapStateToProps, mapDispatchToProps)(Component)`,
22+
`import {Component} from './component';
23+
connect(mapStateToProps, mapDispatchToProps)(Component)`,
2224
],
23-
invalid: [
24-
` const Component = () => {};
25-
connect(mapStateToProps, null)(Component)`,
26-
],
25+
invalid: [{
26+
code: `const Component = () => {};
27+
connect(mapStateToProps, null)(Component)`,
28+
errors: [
29+
{
30+
message: 'Connected component should be defined in a separate file.',
31+
},
32+
],
33+
}],
2734
});

0 commit comments

Comments
 (0)