Skip to content
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Finally, enable all of the rules that you would like to use. For example:
"more/no-filter-instead-of-find": 2,
"more/force-native-methods": 2,
"more/no-duplicated-chains": 2,
"more/classbody-starts-with-newline": [2, 'never']
"more/classbody-starts-with-newline": [2, 'never'],
"more/no-constructor-in-react-component": 2
}
```

Expand All @@ -60,6 +61,7 @@ Finally, enable all of the rules that you would like to use. For example:
* [no-numeric-endings-for-variables](docs/no-numeric-endings-for-variables.md): - Prohibits the use of variables that end in numerics.
* [no-duplicated-chains](docs/no-duplicated-chains.md): - Prohibits the duplication of long chains like `this.props.user.name`
* [classbody-starts-with-newline](docs/classbody-starts-with-newline.md) - Prohibits an empty line at the beggining of a class body
* [more/no-constructor-in-react-component](docs/more/no-constructor-in-react-component) - Do not allow constructor in React components

## Author
WebbyLab (https://webbylab.com)
24 changes: 24 additions & 0 deletions docs/no-constructor-in-react-component.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Do not allow constructor in React components (more/no-constructor-in-react-component)

In most cases, we don't need a constructor in React Components instead we can use a static propTypes property to pass props in a component. This rule does not allow you to use a constructor in components.

## Rule Details

Will enforce not to use a constructor for React Components.

The following patterns are considered bad:
```jsx
class OneComponent extends Component {
constructor(props) {
super(props);
}
}
```

The following patterns are **not** considered bad:
```jsx
class OneComponent extends Component {
static propTypes = {}
state = {}
}
```
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ module.exports = {
'force-native-methods': require('./lib/rules/force-native-methods'),
'no-duplicated-chains': require('./lib/rules/no-duplicated-chains'),
'classbody-starts-with-newline': require('./lib/rules/classbody-starts-with-newline'),
'no-filter-instead-of-find': require('./lib/rules/no-filter-instead-of-find')
'no-filter-instead-of-find': require('./lib/rules/no-filter-instead-of-find'),
'no-constructor-in-react-component': require('./lib/rules/no-constructor-in-react-component')
},
configs: {
recommended: {
Expand All @@ -25,7 +26,8 @@ module.exports = {
'more/force-native-methods': 2,
'more/no-duplicated-chains': 2,
'more/classbody-starts-with-newline': [2, 'never'],
'more/no-filter-instead-of-find': 2
'more/no-filter-instead-of-find': 2,
'more/no-constructor-in-react-component': 2
}
}
}
Expand Down
49 changes: 49 additions & 0 deletions lib/rules/no-constructor-in-react-component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = {
meta: {
docs: {
description: 'Prevent using constructor in React components, insted use static propTypes properties',
category: 'Best Practices'
}
},

create: (context) => {
const componentsList = [];

function reportConstructorInComponent(node) {
context.report({
node,
message: 'Do not use constructor in React components'
});
}

function detectComponent(node) {
if (node.superClass &&
((node.superClass.name === 'Component') ||
(node.superClass.object && node.superClass.object.name === 'React'))) {
componentsList.push(node);
}
}

function exit() {
let constructors = [];

componentsList.forEach(component => {
const methods = component.body.body.filter(node =>
node.type === 'MethodDefinition' &&
node.kind === 'constructor');

constructors = constructors.concat(methods);
});

constructors.forEach(node => {
reportConstructorInComponent(node);
});
}

return {
ClassExpression : detectComponent,
ClassDeclaration: detectComponent,
'Program:exit' : exit
};
}
};
Loading