Skip to content

Commit

Permalink
Factor out no-<method>-set-state rule code to a utility
Browse files Browse the repository at this point in the history
  • Loading branch information
William Holloway committed Apr 6, 2017
1 parent 877e6dc commit 58519ae
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 177 deletions.
61 changes: 2 additions & 59 deletions lib/rules/no-did-mount-set-state.js
Expand Up @@ -4,63 +4,6 @@
*/
'use strict';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');

module.exports = {
meta: {
docs: {
description: 'Prevent usage of setState in componentDidMount',
category: 'Best Practices',
recommended: false
},

schema: [{
enum: ['disallow-in-func']
}]
},

create: function(context) {

var mode = context.options[0] || 'allow-in-func';

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {

CallExpression: function(node) {
var callee = node.callee;
if (
callee.type !== 'MemberExpression' ||
callee.object.type !== 'ThisExpression' ||
callee.property.name !== 'setState'
) {
return;
}
var ancestors = context.getAncestors(callee).reverse();
var depth = 0;
for (var i = 0, j = ancestors.length; i < j; i++) {
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
depth++;
}
if (
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
ancestors[i].key.name !== 'componentDidMount' ||
(mode !== 'disallow-in-func' && depth > 1)
) {
continue;
}
context.report({
node: callee,
message: 'Do not use setState in componentDidMount'
});
break;
}
}
};

}
};
module.exports = makeNoMethodSetStateRule('componentDidMount');
61 changes: 2 additions & 59 deletions lib/rules/no-did-update-set-state.js
Expand Up @@ -4,63 +4,6 @@
*/
'use strict';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');

module.exports = {
meta: {
docs: {
description: 'Prevent usage of setState in componentDidUpdate',
category: 'Best Practices',
recommended: false
},

schema: [{
enum: ['disallow-in-func']
}]
},

create: function(context) {

var mode = context.options[0] || 'allow-in-func';

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {

CallExpression: function(node) {
var callee = node.callee;
if (
callee.type !== 'MemberExpression' ||
callee.object.type !== 'ThisExpression' ||
callee.property.name !== 'setState'
) {
return;
}
var ancestors = context.getAncestors(callee).reverse();
var depth = 0;
for (var i = 0, j = ancestors.length; i < j; i++) {
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
depth++;
}
if (
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
ancestors[i].key.name !== 'componentDidUpdate' ||
(mode !== 'disallow-in-func' && depth > 1)
) {
continue;
}
context.report({
node: callee,
message: 'Do not use setState in componentDidUpdate'
});
break;
}
}
};

}
};
module.exports = makeNoMethodSetStateRule('componentDidUpdate');
61 changes: 2 additions & 59 deletions lib/rules/no-will-update-set-state.js
Expand Up @@ -4,63 +4,6 @@
*/
'use strict';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
var makeNoMethodSetStateRule = require('../util/makeNoSetStateRule');

module.exports = {
meta: {
docs: {
description: 'Prevent usage of setState in componentWillUpdate',
category: 'Best Practices',
recommended: false
},

schema: [{
enum: ['disallow-in-func']
}]
},

create: function(context) {

var mode = context.options[0] || 'allow-in-func';

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {

CallExpression: function(node) {
var callee = node.callee;
if (
callee.type !== 'MemberExpression' ||
callee.object.type !== 'ThisExpression' ||
callee.property.name !== 'setState'
) {
return;
}
var ancestors = context.getAncestors(callee).reverse();
var depth = 0;
for (var i = 0, j = ancestors.length; i < j; i++) {
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
depth++;
}
if (
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
ancestors[i].key.name !== 'componentWillUpdate' ||
(mode !== 'disallow-in-func' && depth > 1)
) {
continue;
}
context.report({
node: callee,
message: 'Do not use setState in componentWillUpdate'
});
break;
}
}
};

}
};
module.exports = makeNoMethodSetStateRule('componentWillUpdate');
70 changes: 70 additions & 0 deletions lib/util/makeNoMethodSetStateRule.js
@@ -0,0 +1,70 @@
/**
* @fileoverview Prevent usage of setState in lifecycle methods
* @author Yannick Croissant
*/
'use strict';

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

function makeNoMethodSetStateRule(methodName) {
return {
meta: {
docs: {
description: 'Prevent usage of setState in ' + methodName,
category: 'Best Practices',
recommended: false
},

schema: [{
enum: ['disallow-in-func']
}]
},

create: function(context) {

var mode = context.options[0] || 'allow-in-func';

// --------------------------------------------------------------------------
// Public
// --------------------------------------------------------------------------

return {

CallExpression: function(node) {
var callee = node.callee;
if (
callee.type !== 'MemberExpression' ||
callee.object.type !== 'ThisExpression' ||
callee.property.name !== 'setState'
) {
return;
}
var ancestors = context.getAncestors(callee).reverse();
var depth = 0;
for (var i = 0, j = ancestors.length; i < j; i++) {
if (/Function(Expression|Declaration)$/.test(ancestors[i].type)) {
depth++;
}
if (
(ancestors[i].type !== 'Property' && ancestors[i].type !== 'MethodDefinition') ||
ancestors[i].key.name !== methodName ||
(mode !== 'disallow-in-func' && depth > 1)
) {
continue;
}
context.report({
node: callee,
message: 'Do not use setState in ' + methodName
});
break;
}
}
};

}
};
}

module.exports = makeNoMethodSetStateRule;

0 comments on commit 58519ae

Please sign in to comment.