Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude references to this.state in setState callback #1610

Merged
merged 2 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/rules/no-access-state-in-setstate.js
Expand Up @@ -26,6 +26,16 @@ module.exports = {
node.callee.object.type === 'ThisExpression';
}

function isFirstArgumentInSetStateCall(current, node) {
if (!isSetStateCall(current)) {
return false;
}
while (node && node.parent !== current) {
node = node.parent;
}
return current.arguments[0] === node;
}

// The methods array contains all methods or functions that are using this.state
// or that are calling another method or function using this.state
const methods = [];
Expand Down Expand Up @@ -55,7 +65,7 @@ module.exports = {
// to further check if they contains this.state
let current = node.parent;
while (current.type !== 'Program') {
if (isSetStateCall(current)) {
if (isFirstArgumentInSetStateCall(current, node)) {
const methodName = node.callee.name;
methods.map(method => {
if (method.methodName === methodName) {
Expand All @@ -80,7 +90,7 @@ module.exports = {
let current = node;
while (current.type !== 'Program') {
// Reporting if this.state is directly within this.setState
if (isSetStateCall(current)) {
if (isFirstArgumentInSetStateCall(current, node)) {
context.report(
node,
'Use callback in setState when referencing the previous state.'
Expand Down Expand Up @@ -129,7 +139,7 @@ module.exports = {
current.parent.object === current
) {
while (current.type !== 'Program') {
if (isSetStateCall(current)) {
if (isFirstArgumentInSetStateCall(current, node)) {
vars
.filter(v => v.scope === context.getScope() && v.variableName === node.name)
.map(v => context.report(
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-access-state-in-setstate.js
Expand Up @@ -64,6 +64,25 @@ ruleTester.run('no-access-state-in-setstate', rule, {
});
`,
parserOptions: parserOptions
}, {
// issue 1604: allow this.state in callback
code: `
var Hello = React.createClass({
onClick: function() {
this.setState({}, () => console.log(this.state));
}
});
`,
parserOptions: parserOptions
}, {
code: `
var Hello = React.createClass({
onClick: function() {
this.setState({}, () => 1 + 1);
}
});
`,
parserOptions: parserOptions
}, {
code: [
'var Hello = React.createClass({',
Expand Down Expand Up @@ -142,6 +161,30 @@ ruleTester.run('no-access-state-in-setstate', rule, {
errors: [{
message: 'Use callback in setState when referencing the previous state.'
}]
}, {
code: `
var Hello = React.createClass({
onClick: function() {
this.setState(this.state, () => 1 + 1);
}
});
`,
parserOptions: parserOptions,
errors: [{
message: 'Use callback in setState when referencing the previous state.'
}]
}, {
code: `
var Hello = React.createClass({
onClick: function() {
this.setState(this.state, () => console.log(this.state));
}
});
`,
parserOptions: parserOptions,
errors: [{
message: 'Use callback in setState when referencing the previous state.'
}]
}, {
code: [
'var Hello = React.createClass({',
Expand Down