Skip to content

Commit 58519ae

Browse files
author
William Holloway
committed
Factor out no-<method>-set-state rule code to a utility
1 parent 877e6dc commit 58519ae

File tree

4 files changed

+76
-177
lines changed

4 files changed

+76
-177
lines changed

lib/rules/no-did-mount-set-state.js

+2-59
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,6 @@
44
*/
55
'use strict';
66

7-
// ------------------------------------------------------------------------------
8-
// Rule Definition
9-
// ------------------------------------------------------------------------------
7+
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');
108

11-
module.exports = {
12-
meta: {
13-
docs: {
14-
description: 'Prevent usage of setState in componentDidMount',
15-
category: 'Best Practices',
16-
recommended: false
17-
},
18-
19-
schema: [{
20-
enum: ['disallow-in-func']
21-
}]
22-
},
23-
24-
create: function(context) {
25-
26-
var mode = context.options[0] || 'allow-in-func';
27-
28-
// --------------------------------------------------------------------------
29-
// Public
30-
// --------------------------------------------------------------------------
31-
32-
return {
33-
34-
CallExpression: function(node) {
35-
var callee = node.callee;
36-
if (
37-
callee.type !== 'MemberExpression' ||
38-
callee.object.type !== 'ThisExpression' ||
39-
callee.property.name !== 'setState'
40-
) {
41-
return;
42-
}
43-
var ancestors = context.getAncestors(callee).reverse();
44-
var depth = 0;
45-
for (var i = 0, j = ancestors.length; i < j; i++) {
46-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
47-
depth++;
48-
}
49-
if (
50-
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
51-
ancestors[i].key.name !== 'componentDidMount' ||
52-
(mode !== 'disallow-in-func' && depth > 1)
53-
) {
54-
continue;
55-
}
56-
context.report({
57-
node: callee,
58-
message: 'Do not use setState in componentDidMount'
59-
});
60-
break;
61-
}
62-
}
63-
};
64-
65-
}
66-
};
9+
module.exports = makeNoMethodSetStateRule('componentDidMount');

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

+2-59
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,6 @@
44
*/
55
'use strict';
66

7-
// ------------------------------------------------------------------------------
8-
// Rule Definition
9-
// ------------------------------------------------------------------------------
7+
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');
108

11-
module.exports = {
12-
meta: {
13-
docs: {
14-
description: 'Prevent usage of setState in componentDidUpdate',
15-
category: 'Best Practices',
16-
recommended: false
17-
},
18-
19-
schema: [{
20-
enum: ['disallow-in-func']
21-
}]
22-
},
23-
24-
create: function(context) {
25-
26-
var mode = context.options[0] || 'allow-in-func';
27-
28-
// --------------------------------------------------------------------------
29-
// Public
30-
// --------------------------------------------------------------------------
31-
32-
return {
33-
34-
CallExpression: function(node) {
35-
var callee = node.callee;
36-
if (
37-
callee.type !== 'MemberExpression' ||
38-
callee.object.type !== 'ThisExpression' ||
39-
callee.property.name !== 'setState'
40-
) {
41-
return;
42-
}
43-
var ancestors = context.getAncestors(callee).reverse();
44-
var depth = 0;
45-
for (var i = 0, j = ancestors.length; i < j; i++) {
46-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
47-
depth++;
48-
}
49-
if (
50-
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
51-
ancestors[i].key.name !== 'componentDidUpdate' ||
52-
(mode !== 'disallow-in-func' && depth > 1)
53-
) {
54-
continue;
55-
}
56-
context.report({
57-
node: callee,
58-
message: 'Do not use setState in componentDidUpdate'
59-
});
60-
break;
61-
}
62-
}
63-
};
64-
65-
}
66-
};
9+
module.exports = makeNoMethodSetStateRule('componentDidUpdate');

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

+2-59
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,6 @@
44
*/
55
'use strict';
66

7-
// ------------------------------------------------------------------------------
8-
// Rule Definition
9-
// ------------------------------------------------------------------------------
7+
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');
108

11-
module.exports = {
12-
meta: {
13-
docs: {
14-
description: 'Prevent usage of setState in componentWillUpdate',
15-
category: 'Best Practices',
16-
recommended: false
17-
},
18-
19-
schema: [{
20-
enum: ['disallow-in-func']
21-
}]
22-
},
23-
24-
create: function(context) {
25-
26-
var mode = context.options[0] || 'allow-in-func';
27-
28-
// --------------------------------------------------------------------------
29-
// Public
30-
// --------------------------------------------------------------------------
31-
32-
return {
33-
34-
CallExpression: function(node) {
35-
var callee = node.callee;
36-
if (
37-
callee.type !== 'MemberExpression' ||
38-
callee.object.type !== 'ThisExpression' ||
39-
callee.property.name !== 'setState'
40-
) {
41-
return;
42-
}
43-
var ancestors = context.getAncestors(callee).reverse();
44-
var depth = 0;
45-
for (var i = 0, j = ancestors.length; i < j; i++) {
46-
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
47-
depth++;
48-
}
49-
if (
50-
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
51-
ancestors[i].key.name !== 'componentWillUpdate' ||
52-
(mode !== 'disallow-in-func' && depth > 1)
53-
) {
54-
continue;
55-
}
56-
context.report({
57-
node: callee,
58-
message: 'Do not use setState in componentWillUpdate'
59-
});
60-
break;
61-
}
62-
}
63-
};
64-
65-
}
66-
};
9+
module.exports = makeNoMethodSetStateRule('componentWillUpdate');

lib/util/makeNoMethodSetStateRule.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @fileoverview Prevent usage of setState in lifecycle methods
3+
* @author Yannick Croissant
4+
*/
5+
'use strict';
6+
7+
// ------------------------------------------------------------------------------
8+
// Rule Definition
9+
// ------------------------------------------------------------------------------
10+
11+
function makeNoMethodSetStateRule(methodName) {
12+
return {
13+
meta: {
14+
docs: {
15+
description: 'Prevent usage of setState in ' + methodName,
16+
category: 'Best Practices',
17+
recommended: false
18+
},
19+
20+
schema: [{
21+
enum: ['disallow-in-func']
22+
}]
23+
},
24+
25+
create: function(context) {
26+
27+
var mode = context.options[0] || 'allow-in-func';
28+
29+
// --------------------------------------------------------------------------
30+
// Public
31+
// --------------------------------------------------------------------------
32+
33+
return {
34+
35+
CallExpression: function(node) {
36+
var callee = node.callee;
37+
if (
38+
callee.type !== 'MemberExpression' ||
39+
callee.object.type !== 'ThisExpression' ||
40+
callee.property.name !== 'setState'
41+
) {
42+
return;
43+
}
44+
var ancestors = context.getAncestors(callee).reverse();
45+
var depth = 0;
46+
for (var i = 0, j = ancestors.length; i < j; i++) {
47+
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
48+
depth++;
49+
}
50+
if (
51+
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
52+
ancestors[i].key.name !== methodName ||
53+
(mode !== 'disallow-in-func' && depth > 1)
54+
) {
55+
continue;
56+
}
57+
context.report({
58+
node: callee,
59+
message: 'Do not use setState in ' + methodName
60+
});
61+
break;
62+
}
63+
}
64+
};
65+
66+
}
67+
};
68+
}
69+
70+
module.exports = makeNoMethodSetStateRule;

0 commit comments

Comments
 (0)