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

ESLint Plugin: react-no-unsafe-timeout: Consider variable assignment as valid #16292

Merged
merged 2 commits into from
Jun 26, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions packages/components/src/snackbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ function Snackbar( {
onRemove = noop,
}, ref ) {
useEffect( () => {
// This rule doesn't account yet for React Hooks
// eslint-disable-next-line @wordpress/react-no-unsafe-timeout
const timeoutHandle = setTimeout( () => {
onRemove();
}, NOTICE_TIMEOUT );
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed custom regular expression for the `no-restricted-syntax` rule enforcing translate function arguments. [#15839](https://github.com/WordPress/gutenberg/pull/15839).
- Fixed arguments checking of `_nx` for the `no-restricted-syntax` rule enforcing translate function arguments. [#15839](https://github.com/WordPress/gutenberg/pull/15839).
- Fixed false positive with `react-no-unsafe-timeout` which would wrongly flag errors when assigning `setTimeout` result to a variable (for example, in a `useEffect` hook).

## 2.2.0 (2019-05-21)

Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/rules/__tests__/react-no-unsafe-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ ruleTester.run( 'react-no-unsafe-timeout', rule, {
{
code: `class MyComponent extends Component { componentDidMount() { this.timeoutId = setTimeout(); } }`,
},
{
code: `
function MyComponent() {
useEffect( () => {
const timeoutHandle = setTimeout( () => {} );

return () => clearTimeout( timeoutHandle );
}, [] );

return null;
}`,
},
],
invalid: [
{
Expand Down
5 changes: 4 additions & 1 deletion packages/eslint-plugin/rules/react-no-unsafe-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ module.exports = {

// If the result of a `setTimeout` call is assigned to a
// variable, assume the timer ID is handled by a cancellation.
const hasAssignment = node.parent.type === 'AssignmentExpression';
const hasAssignment = (
node.parent.type === 'AssignmentExpression' ||
node.parent.type === 'VariableDeclarator'
);
if ( hasAssignment ) {
return;
}
Expand Down