Skip to content

Commit

Permalink
[fix] no-did-mount-set-state, no-did-update-set-state: Catch setS…
Browse files Browse the repository at this point in the history
…tate in cDU and cDM when defined as ClassProperties
  • Loading branch information
jaaberg authored and ljharb committed Dec 16, 2017
1 parent a131363 commit f7a9fb5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/util/makeNoMethodSetStateRule.js
Expand Up @@ -76,7 +76,7 @@ function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
depth++;
}
if (
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition') ||
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition' && ancestor.type !== 'ClassProperty') ||
!nameMatches(ancestor.key.name) ||
(mode !== 'disallow-in-func' && depth > 1)
) {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-did-mount-set-state.js
Expand Up @@ -107,6 +107,20 @@ ruleTester.run('no-did-mount-set-state', rule, {
errors: [{
message: 'Do not use setState in componentDidMount'
}]
}, {
code: `
class Hello extends React.Component {
componentDidMount = () => {
this.setState({
data: data
});
}
}
`,
parser: 'babel-eslint',
errors: [{
message: 'Do not use setState in componentDidMount'
}]
}, {
code: `
var Hello = createReactClass({
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-did-update-set-state.js
Expand Up @@ -107,6 +107,20 @@ ruleTester.run('no-did-update-set-state', rule, {
errors: [{
message: 'Do not use setState in componentDidUpdate'
}]
}, {
code: `
class Hello extends React.Component {
componentDidUpdate = () => {
this.setState({
data: data
});
}
}
`,
parser: 'babel-eslint',
errors: [{
message: 'Do not use setState in componentDidUpdate'
}]
}, {
code: `
var Hello = createReactClass({
Expand Down

0 comments on commit f7a9fb5

Please sign in to comment.