-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1945 from lukyth/lukyth/state-in-constructor
[New] Add `state-in-constructor` rule
- Loading branch information
Showing
8 changed files
with
516 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @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.getParentES6Component() | ||
) { | ||
context.report({ | ||
node, | ||
message: 'State initialization should be in a constructor' | ||
}); | ||
} | ||
}, | ||
AssignmentExpression(node) { | ||
if ( | ||
option === 'never' && | ||
utils.isStateMemberExpression(node.left) && | ||
utils.inConstructor() && | ||
utils.getParentES6Component() | ||
) { | ||
context.report({ | ||
node, | ||
message: 'State initialization should be in a class property' | ||
}); | ||
} | ||
} | ||
}; | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.