Skip to content

Commit

Permalink
Fix a case where left side of assignment is not MemberExpression
Browse files Browse the repository at this point in the history
This commit also move `isStateMemberExpression` from
`no-direct-mutation-state` rule into `util/Components` so that the logic
could be share with other rules.
  • Loading branch information
lukyth committed Aug 19, 2018
1 parent 3516a81 commit b1024c0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
13 changes: 2 additions & 11 deletions lib/rules/no-direct-mutation-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, {
Expand All @@ -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, {
Expand Down
3 changes: 1 addition & 2 deletions lib/rules/state-in-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
) {
Expand Down
9 changes: 9 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/state-in-constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Foo</div>
}
}
`
}, {
code: `
class Foo extends React.Component {
constructor(props) {
super(props)
foobar = { bar: 0 }
}
render() {
return <div>Foo</div>
}
}
`,
options: ['never']
}],

invalid: [{
Expand Down

0 comments on commit b1024c0

Please sign in to comment.