Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for UNSAFE_ method in no-will-update-set-state #1845

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
);
23 changes: 18 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,18 @@ function makeNoMethodSetStateRule(methodName) {
create: function(context) {
const mode = context.options[0] || 'allow-in-func';

function nameMatches(name) {
if (name === methodName) {
return true;
}

if (typeof shouldCheckUnsafeCb === 'function' && shouldCheckUnsafeCb(context)) {
return name === `UNSAFE_${methodName}`;
}

return false;
}

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------
Expand All @@ -46,19 +58,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
39 changes: 39 additions & 0 deletions tests/lib/rules/no-will-update-set-state.js
Expand Up @@ -77,6 +77,17 @@ ruleTester.run('no-will-update-set-state', rule, {
});
`,
parser: 'babel-eslint'
}, {
code: `
class Hello extends React.Component {
UNSAFE_componentWillUpdate() {
this.setState({
data: data
});
}
}
`,
settings: {react: {version: '16.2.0'}}
}],

invalid: [{
Expand Down Expand Up @@ -225,5 +236,33 @@ 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
});
}
}
`,
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'
}]
}]
});