-
Notifications
You must be signed in to change notification settings - Fork 76
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
Add new rule noAccessStateInSetstate #190
Changes from 1 commit
0caf7df
8ddc00f
5e19d46
a81a966
0ce297b
e2b08c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* @license | ||
* Copyright 2018 Palantir Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import * as Lint from "tslint"; | ||
import { isCallExpression } from "tsutils"; | ||
import * as ts from "typescript"; | ||
|
||
export class Rule extends Lint.Rules.AbstractRule { | ||
/* tslint:disable:object-literal-sort-keys */ | ||
public static metadata: Lint.IRuleMetadata = { | ||
ruleName: "no-access-state-in-setstate", | ||
description: "Reports usage of this.state within setState", | ||
rationale: Lint.Utils.dedent | ||
`Usage of this.state might result in errors when two state calls are \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the trailing also the leading backtick should go right after |
||
called in batch and thus referencing old state and not the current state.`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to reference these react docs in this rationale. you can use markdown There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a link to the docs. Is the phrasing alright? (English being not my first language and all) |
||
options: null, | ||
optionsDescription: "", | ||
type: "functionality", | ||
typescriptOnly: false, | ||
}; | ||
/* tslint:enable:object-literal-sort-keys */ | ||
|
||
public static FAILURE_STRING = "Use callback in setState when referencing the previous state."; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithFunction(sourceFile, walk); | ||
} | ||
} | ||
|
||
function walk(ctx: Lint.WalkContext<void>): void { | ||
return ts.forEachChild(ctx.sourceFile, cb); | ||
|
||
function cb(node: ts.Node): void { | ||
if (!isCallExpression(node)) { | ||
return ts.forEachChild(node, cb); | ||
} | ||
if (isStateUsedInSetStateWith(node)) { | ||
ctx.addFailureAtNode(node, Rule.FAILURE_STRING); | ||
} | ||
return; | ||
} | ||
} | ||
|
||
function isStateUsedInSetStateWith(callExpression: ts.CallExpression): boolean { | ||
if (callExpression.expression.getText() !== "this.setState") { | ||
return false; | ||
} | ||
if (callExpression.arguments.length === 0) { | ||
return false; | ||
} | ||
|
||
const firstCallExpressionArgument = callExpression.arguments[0]; | ||
const argumentAsText = firstCallExpressionArgument.getText(); | ||
return argumentAsText.indexOf("this.state.") !== -1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
class SomeReactComponent extends React.Component { | ||
|
||
someClassFunction() { | ||
|
||
this.fooBar({ | ||
foo: this.state.foo | ||
}); | ||
|
||
this.setState({ | ||
foo: "foo", | ||
bar: this.barz | ||
}); | ||
|
||
this.setState( | ||
{ | ||
foo: "foo" | ||
}, | ||
() => this.fooBar(this.state.foo); | ||
); | ||
|
||
this.setState(prevState => ({ | ||
foo: !prevState.foo | ||
})); | ||
|
||
this.setState((prevState, currentProps) => ({ | ||
foo: !prevState.foo, | ||
bar: currentProps.bar | ||
})); | ||
|
||
this.setState({ | ||
~~~~~~~~~~~~~~~ | ||
foo: !this.state.foo | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
}); | ||
~~~~~~~~~~ [0] | ||
|
||
this.setState({ | ||
~~~~~~~~~~~~~~~ | ||
foo: this.fooBar(this.state.foo) | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
}); | ||
~~~~~~~~~~ [0] | ||
|
||
this.setState((prevState, currentProps) => ({ | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
foo: !this.state.foo, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it would of course be much nicer to have the marker only on the occurrence(s) of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sat down again with my code and now I'm pretty sure I found a still very easy and even more reliable way to not only find accesses of this.state but also can now display the error message exactly at the right node. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even better push it right away. Looking forward to your review! |
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
bar: currentProps.bar | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
})); | ||
~~~~~~~~~~~ [0] | ||
|
||
this.setState((prevState, currentProps) => { | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
this.fooBar(this.state.foo); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this case the error message might read a bit strange, since the code is using the callback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good idea, will do that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, unfortunately it might be a little difficult. In my now updated code, you'd have to copy the So for a quick solution I simply made the failure text a bit more generic. |
||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
return { | ||
~~~~~~~~~~~~~~~~~~~~ | ||
bar: !prevState.bar | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
}; | ||
~~~~~~~~~~~~~~ | ||
}); | ||
~~~~~~~~~~ [0] | ||
} | ||
} | ||
|
||
[0]: Use callback in setState when referencing the previous state. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"rules": { | ||
"no-access-state-in-setstate": true | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this did not get syntax highlighted / formatted correctly -- check out the markdown preview