Skip to content

Commit 8122c8b

Browse files
alexzherdevljharb
authored andcommitted
Account for UNSAFE_ method in no-will-update-set-state
Resolves jsx-eslint#1844
1 parent c6cc4ab commit 8122c8b

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

lib/rules/no-will-update-set-state.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
'use strict';
66

77
const makeNoMethodSetStateRule = require('../util/makeNoMethodSetStateRule');
8+
const versionUtil = require('../util/version');
89

9-
module.exports = makeNoMethodSetStateRule('componentWillUpdate');
10+
module.exports = makeNoMethodSetStateRule(
11+
'componentWillUpdate',
12+
context => versionUtil.testReactVersion(context, '16.3.0')
13+
);

lib/util/makeNoMethodSetStateRule.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const docsUrl = require('./docsUrl');
1010
// Rule Definition
1111
// ------------------------------------------------------------------------------
1212

13-
function makeNoMethodSetStateRule(methodName) {
13+
function makeNoMethodSetStateRule(methodName, shouldCheckUnsafeCb) {
1414
return {
1515
meta: {
1616
docs: {
@@ -28,6 +28,18 @@ function makeNoMethodSetStateRule(methodName) {
2828
create: function(context) {
2929
const mode = context.options[0] || 'allow-in-func';
3030

31+
function nameMatches(name) {
32+
if (name === methodName) {
33+
return true;
34+
}
35+
36+
if (typeof shouldCheckUnsafeCb === 'function' && shouldCheckUnsafeCb(context)) {
37+
return name === `UNSAFE_${methodName}`;
38+
}
39+
40+
return false;
41+
}
42+
3143
// --------------------------------------------------------------------------
3244
// Public
3345
// --------------------------------------------------------------------------
@@ -46,19 +58,20 @@ function makeNoMethodSetStateRule(methodName) {
4658
const ancestors = context.getAncestors(callee).reverse();
4759
let depth = 0;
4860
for (let i = 0, j = ancestors.length; i < j; i++) {
49-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
61+
const ancestor = ancestors[i];
62+
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
5063
depth++;
5164
}
5265
if (
53-
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
54-
ancestors[i].key.name !== methodName ||
66+
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition') ||
67+
!nameMatches(ancestor.key.name) ||
5568
(mode !== 'disallow-in-func' && depth > 1)
5669
) {
5770
continue;
5871
}
5972
context.report({
6073
node: callee,
61-
message: `Do not use setState in ${methodName}`
74+
message: `Do not use setState in ${ancestor.key.name}`
6275
});
6376
break;
6477
}

tests/lib/rules/no-will-update-set-state.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ ruleTester.run('no-will-update-set-state', rule, {
7777
});
7878
`,
7979
parser: 'babel-eslint'
80+
}, {
81+
code: `
82+
class Hello extends React.Component {
83+
UNSAFE_componentWillUpdate() {
84+
this.setState({
85+
data: data
86+
});
87+
}
88+
}
89+
`,
90+
settings: {react: {version: '16.2.0'}}
8091
}],
8192

8293
invalid: [{
@@ -225,5 +236,33 @@ ruleTester.run('no-will-update-set-state', rule, {
225236
errors: [{
226237
message: 'Do not use setState in componentWillUpdate'
227238
}]
239+
}, {
240+
code: `
241+
class Hello extends React.Component {
242+
UNSAFE_componentWillUpdate() {
243+
this.setState({
244+
data: data
245+
});
246+
}
247+
}
248+
`,
249+
settings: {react: {version: '16.3.0'}},
250+
errors: [{
251+
message: 'Do not use setState in UNSAFE_componentWillUpdate'
252+
}]
253+
}, {
254+
code: `
255+
var Hello = createReactClass({
256+
UNSAFE_componentWillUpdate: function() {
257+
this.setState({
258+
data: data
259+
});
260+
}
261+
});
262+
`,
263+
settings: {react: {version: '16.3.0'}},
264+
errors: [{
265+
message: 'Do not use setState in UNSAFE_componentWillUpdate'
266+
}]
228267
}]
229268
});

0 commit comments

Comments
 (0)