-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ESLint Plugin: Add rule react-no-unsafe-timeout #14650
Conversation
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.
Nice one, I'm starting to get comfortable with reviewing those ESLint rules. I like that all recent efforts are focused on preventing common mistakes rather than code style polishing 💯
code: `class MyComponent extends Component { componentDidMount() { this.props.setTimeout(); } }`, | ||
}, | ||
{ | ||
code: `class MyComponent extends Component { componentDidMount() { this.timeoutId = setTimeout(); } }`, |
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.
It still can be invalid (not canceled when the component gets unmount) but I think it's fine to assume good intents and keep implementation reasonable :)
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.
It still can be invalid (not canceled when the component gets unmount) but I think it's fine to assume good intents and keep implementation reasonable :)
Right, the thinking was: You'd only ever care about the value produced by setTimeout
if you intended to use it in clearTimeout
(the return value is described as such). There's no guarantee here that this actually occurs, or that it occurs specifically in componentWillUnmount
. But it's also the only option for considering "safe" in a generic sense where someone may want to use this rule, but wouldn't use withSafeTimeout
from @wordpress/compose
.
@aduth - I'm curious, why is there no |
@nerrad Oversight. I'll open a pull request shortly. Thanks for noting it. |
I figured :) |
This pull request seeks to introduce a new ESLint rule
@wordpress/react-no-unsafe-timeout
. In brief, it is meant to enforce the anti-pattern ofsetTimeout
use in components whichwithSafeTimeout
exists to address.From the included rule documentation:
In introducing the rule, it also resolves a few existing offending occurrences in code:
packages/block-editor/src/components/rich-text/index.js
packages/block-editor/src/components/url-input/index.js
packages/components/src/form-token-field/suggestions-list.js
Testing Instructions:
Verify there are no errors when running
npm run lint-js
.Verify unit tests pass running
npm run test-unit