From b1024c08c6ac39404a4713a05d01fb316ffc90ba Mon Sep 17 00:00:00 2001 From: Kanitkorn S Date: Sun, 19 Aug 2018 11:18:35 +0700 Subject: [PATCH] Fix a case where left side of assignment is not MemberExpression This commit also move `isStateMemberExpression` from `no-direct-mutation-state` rule into `util/Components` so that the logic could be share with other rules. --- lib/rules/no-direct-mutation-state.js | 13 ++----------- lib/rules/state-in-constructor.js | 3 +-- lib/util/Components.js | 9 +++++++++ tests/lib/rules/state-in-constructor.js | 25 +++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/lib/rules/no-direct-mutation-state.js b/lib/rules/no-direct-mutation-state.js index a4daed44ec..bbb9c62a12 100644 --- a/lib/rules/no-direct-mutation-state.js +++ b/lib/rules/no-direct-mutation-state.js @@ -59,15 +59,6 @@ module.exports = { return node; } - /** - * Determine if this MemberExpression is for `this.state` - * @param {Object} node The node to process - * @returns {Boolean} - */ - function isStateMemberExpression(node) { - return node.object.type === 'ThisExpression' && node.property.name === 'state'; - } - /** * Determine if we should currently ignore assignments in this component. * @param {?Object} component The component to process @@ -101,7 +92,7 @@ module.exports = { return; } const item = getOuterMemberExpression(node.left); - if (isStateMemberExpression(item)) { + if (utils.isStateMemberExpression(item)) { const mutations = (component && component.mutations) || []; mutations.push(node.left.object); components.set(node, { @@ -117,7 +108,7 @@ module.exports = { return; } const item = getOuterMemberExpression(node.argument); - if (isStateMemberExpression(item)) { + if (utils.isStateMemberExpression(item)) { const mutations = (component && component.mutations) || []; mutations.push(item); components.set(node, { diff --git a/lib/rules/state-in-constructor.js b/lib/rules/state-in-constructor.js index f416ffde8c..ebfeb65605 100644 --- a/lib/rules/state-in-constructor.js +++ b/lib/rules/state-in-constructor.js @@ -43,8 +43,7 @@ module.exports = { AssignmentExpression(node) { if ( option === 'never' && - node.left.object.type === 'ThisExpression' && - node.left.property.name === 'state' && + utils.isStateMemberExpression(node.left) && utils.inConstructor() && utils.getParentES6Component() ) { diff --git a/lib/util/Components.js b/lib/util/Components.js index 28e834d665..d4d42a0c6b 100644 --- a/lib/util/Components.js +++ b/lib/util/Components.js @@ -321,6 +321,15 @@ function componentRule(rule, context) { return false; }, + /** + * Determine if the node is MemberExpression of `this.state` + * @param {Object} node The node to process + * @returns {Boolean} + */ + isStateMemberExpression: function(node) { + return node.type === 'MemberExpression' && node.object.type === 'ThisExpression' && node.property.name === 'state'; + }, + getReturnPropertyAndNode(ASTnode) { let property; let node = ASTnode; diff --git a/tests/lib/rules/state-in-constructor.js b/tests/lib/rules/state-in-constructor.js index fe4804b65d..8324b56cf8 100644 --- a/tests/lib/rules/state-in-constructor.js +++ b/tests/lib/rules/state-in-constructor.js @@ -198,6 +198,31 @@ ruleTester.run('state-in-constructor', rule, { } } ` + }, { + code: ` + class Foo extends React.Component { + constructor(props) { + super(props) + foobar = { bar: 0 } + } + render() { + return
Foo
+ } + } + ` + }, { + code: ` + class Foo extends React.Component { + constructor(props) { + super(props) + foobar = { bar: 0 } + } + render() { + return
Foo
+ } + } + `, + options: ['never'] }], invalid: [{