Skip to content

Commit 3cb8d0d

Browse files
committed
Account for UNSAFE_ method in no-will-update-set-state
Resolves jsx-eslint#1844
1 parent 7869530 commit 3cb8d0d

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-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: 15 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 = () => false) {
1414
return {
1515
meta: {
1616
docs: {
@@ -28,6 +28,15 @@ function makeNoMethodSetStateRule(methodName) {
2828
create: function(context) {
2929
const mode = context.options[0] || 'allow-in-func';
3030

31+
function nameMatches(name) {
32+
let matches = name === methodName;
33+
if (shouldCheckUnsafeCb(context)) {
34+
matches = matches || name === `UNSAFE_${methodName}`;
35+
}
36+
37+
return matches;
38+
}
39+
3140
// --------------------------------------------------------------------------
3241
// Public
3342
// --------------------------------------------------------------------------
@@ -46,19 +55,20 @@ function makeNoMethodSetStateRule(methodName) {
4655
const ancestors = context.getAncestors(callee).reverse();
4756
let depth = 0;
4857
for (let i = 0, j = ancestors.length; i < j; i++) {
49-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
58+
const ancestor = ancestors[i];
59+
if (/Function(Expression|Declaration)$/.test(ancestor.type)) {
5060
depth++;
5161
}
5262
if (
53-
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
54-
ancestors[i].key.name !== methodName ||
63+
(ancestor.type !== 'Property' && ancestor.type !== 'MethodDefinition') ||
64+
!nameMatches(ancestor.key.name) ||
5565
(mode !== 'disallow-in-func' && depth > 1)
5666
) {
5767
continue;
5868
}
5969
context.report({
6070
node: callee,
61-
message: `Do not use setState in ${methodName}`
71+
message: `Do not use setState in ${ancestor.key.name}`
6272
});
6373
break;
6474
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ 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+
parser: 'babel-eslint',
91+
settings: {react: {version: '16.2.0'}}
8092
}],
8193

8294
invalid: [{
@@ -225,5 +237,34 @@ ruleTester.run('no-will-update-set-state', rule, {
225237
errors: [{
226238
message: 'Do not use setState in componentWillUpdate'
227239
}]
240+
}, {
241+
code: `
242+
class Hello extends React.Component {
243+
UNSAFE_componentWillUpdate() {
244+
this.setState({
245+
data: data
246+
});
247+
}
248+
}
249+
`,
250+
parser: 'babel-eslint',
251+
settings: {react: {version: '16.3.0'}},
252+
errors: [{
253+
message: 'Do not use setState in UNSAFE_componentWillUpdate'
254+
}]
255+
}, {
256+
code: `
257+
var Hello = createReactClass({
258+
UNSAFE_componentWillUpdate: function() {
259+
this.setState({
260+
data: data
261+
});
262+
}
263+
});
264+
`,
265+
settings: {react: {version: '16.3.0'}},
266+
errors: [{
267+
message: 'Do not use setState in UNSAFE_componentWillUpdate'
268+
}]
228269
}]
229270
});

0 commit comments

Comments
 (0)