New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add state-in-constructor rule #1945
Changes from 3 commits
a312b16
2f06677
4706e86
81d0e8b
3516a81
b1024c0
b0e4c84
acc35a1
9415814
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Enforce state initialization style (react/state-in-constructor) | ||
|
||
This rule will enforce the state initialization style to be either in a constructor or with a class property. | ||
|
||
## Rule Options | ||
|
||
```js | ||
... | ||
"react/state-in-constructor": [<enabled>, <mode>] | ||
... | ||
``` | ||
|
||
### `always` mode | ||
|
||
Will enforce the state initialization style to be in a constructor. This is the default mode. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
state = { bar: 0 } | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { bar: 0 } | ||
} | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
### `never` mode | ||
|
||
Will enforce the state initialization style to be with a class property. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
constructor(props) { | ||
super(props) | ||
this.state = { bar: 0 } | ||
} | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
The following patterns are **not** considered warnings: | ||
|
||
```jsx | ||
class Foo extends React.Component { | ||
state = { bar: 0 } | ||
render() { | ||
return <div>Foo</div> | ||
} | ||
} | ||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
When the way a component state is being initialized doesn't matter. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @fileoverview Enforce the state initialization style to be either in a constructor or with a class property | ||
* @author Kanitkorn Sujautra | ||
*/ | ||
'use strict'; | ||
|
||
const Components = require('../util/Components'); | ||
const docsUrl = require('../util/docsUrl'); | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'State initialization in an ES6 class component should be in a constructor', | ||
category: 'Stylistic Issues', | ||
recommended: false, | ||
url: docsUrl('state-in-constructor') | ||
}, | ||
schema: [{ | ||
enum: ['always', 'never'] | ||
}] | ||
}, | ||
|
||
create: Components.detect((context, components, utils) => { | ||
const option = context.options[0] || 'always'; | ||
return { | ||
ClassProperty: function (node) { | ||
if ( | ||
option === 'always' && | ||
!node.static && | ||
node.key.name === 'state' && | ||
utils.isES6Component(node.parent.parent) | ||
) { | ||
context.report({ | ||
node, | ||
message: 'State initialization should be in a constructor' | ||
}); | ||
} | ||
}, | ||
AssignmentExpression(node) { | ||
if ( | ||
option === 'never' && | ||
node.left.object.type === 'ThisExpression' && | ||
node.left.property.name === 'state' && | ||
node.parent.parent.parent.parent.kind === 'constructor' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit brittle: assignment could be nested in other nodes (e.g. if conditions) in which case this sequence will not work. This should either be traversing parents in a loop, or scopes like https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/no-unused-prop-types.js#L264 (although not sure which one is better) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's try to add a test case that would fail here. |
||
utils.isES6Component(node.parent.parent.parent.parent.parent.parent) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, but in this case maybe |
||
) { | ||
context.report({ | ||
node, | ||
message: 'State initialization should be in a class property' | ||
}); | ||
} | ||
} | ||
}; | ||
}) | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines assume that
node.left
is aMemberExpression
which is not always the case in anAssignmentExpression
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try to add a test case that would fail here.