Skip to content

Commit

Permalink
Account for UNSAFE_ method in no-will-update-set-state
Browse files Browse the repository at this point in the history
Resolves #1844
  • Loading branch information
alexzherdev committed Jun 24, 2018
1 parent 7869530 commit 40c4ffa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/rules/no-will-update-set-state.js
Expand Up @@ -5,5 +5,9 @@
'use strict';

const makeNoMethodSetStateRule = require('../util/makeNoMethodSetStateRule');
const versionUtil = require('../util/version');

module.exports = makeNoMethodSetStateRule('componentWillUpdate');
module.exports = makeNoMethodSetStateRule(
'componentWillUpdate',
context => versionUtil.testReactVersion(context, '16.3.0')
);
20 changes: 15 additions & 5 deletions lib/util/makeNoMethodSetStateRule.js
Expand Up @@ -10,7 +10,7 @@ const docsUrl = require('./docsUrl');
// Rule Definition
// ------------------------------------------------------------------------------

function makeNoMethodSetStateRule(methodName) {
function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
return {
meta: {
docs: {
Expand All @@ -28,6 +28,15 @@ function makeNoMethodSetStateRule(methodName) {
create: function(context) {
const mode = context.options[0] || 'allow-in-func';

function nameMatches(name) {
let matches = name === methodName;
if (typeof shouldCheckUnsafeCb === 'function' && shouldCheckUnsafeCb(context)) {
matches = matches || name === `UNSAFE_${methodName}`;
}

return matches;
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
Expand All @@ -46,19 +55,20 @@ function makeNoMethodSetStateRule(methodName) {
const ancestors = context.getAncestors(callee).reverse();
let depth = 0;
for (let i = 0, j = ancestors.length; i < j; i++) {
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
const ancestor = ancestors[i];
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
depth++;
}
if (
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
ancestors[i].key.name !== methodName ||
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition') ||
!nameMatches(ancestor.key.name) ||
(mode !== 'disallow-in-func' && depth > 1)
) {
continue;
}
context.report({
node: callee,
message: `Do not use setState in ${methodName}`
message: `Do not use setState in ${ancestor.key.name}`
});
break;
}
Expand Down
41 changes: 41 additions & 0 deletions tests/lib/rules/no-will-update-set-state.js
Expand Up @@ -77,6 +77,18 @@ ruleTester.run('no-will-update-set-state', rule, {
});
`,
parser: 'babel-eslint'
}, {
code: `
class Hello extends React.Component {
UNSAFE_componentWillUpdate() {
this.setState({
data: data
});
}
}
`,
parser: 'babel-eslint',
settings: {react: {version: '16.2.0'}}
}],

invalid: [{
Expand Down Expand Up @@ -225,5 +237,34 @@ ruleTester.run('no-will-update-set-state', rule, {
errors: [{
message: 'Do not use setState in componentWillUpdate'
}]
}, {
code: `
class Hello extends React.Component {
UNSAFE_componentWillUpdate() {
this.setState({
data: data
});
}
}
`,
parser: 'babel-eslint',
settings: {react: {version: '16.3.0'}},
errors: [{
message: 'Do not use setState in UNSAFE_componentWillUpdate'
}]
}, {
code: `
var Hello = createReactClass({
UNSAFE_componentWillUpdate: function() {
this.setState({
data: data
});
}
});
`,
settings: {react: {version: '16.3.0'}},
errors: [{
message: 'Do not use setState in UNSAFE_componentWillUpdate'
}]
}]
});

0 comments on commit 40c4ffa

Please sign in to comment.