-
Notifications
You must be signed in to change notification settings - Fork 48.5k
Add eslint rule to error on reads or writes to refs in render #24506
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
239 changes: 239 additions & 0 deletions
239
packages/eslint-plugin-react-hooks/__tests__/ESLintPureRender-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ESLintTester = require('eslint').RuleTester; | ||
const ReactHooksESLintPlugin = require('eslint-plugin-react-hooks'); | ||
const PureRenderESLintRule = ReactHooksESLintPlugin.rules['pure-render']; | ||
|
||
ESLintTester.setDefaultConfig({ | ||
parser: require.resolve('babel-eslint'), | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
}, | ||
}); | ||
|
||
const writeError = { | ||
message: | ||
'Writing to refs during rendering is not allowed. Move this into a useEffect or useLayoutEffect. See https://beta.reactjs.org/apis/useref', | ||
}; | ||
const readError = { | ||
message: | ||
'Reading from refs during rendering is not allowed. See https://beta.reactjs.org/apis/useref', | ||
}; | ||
|
||
const tests = { | ||
valid: [ | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
useLayoutEffect(() => { | ||
ref.current = true; | ||
}); | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
let MyComponent = () => { | ||
let ref = useRef(false); | ||
useLayoutEffect(() => { | ||
ref.current = true; | ||
}); | ||
}; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
let MyComponent = function () { | ||
let ref = useRef(false); | ||
useLayoutEffect(() => { | ||
ref.current = true; | ||
}); | ||
}; | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
let onChange = () => { | ||
ref.current = true; | ||
}; | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(null); | ||
if (ref.current == null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
if (ref.current === false) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(undefined); | ||
if (ref.current === undefined) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(undefined); | ||
if (ref.current == null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(); | ||
if (ref.current == null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(); | ||
if (ref.current == null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = React.useRef(); | ||
if (ref.current == null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
ref.current = true; | ||
} | ||
`, | ||
errors: [writeError], | ||
}, | ||
{ | ||
code: ` | ||
let MyComponent = () => { | ||
let ref = useRef(false); | ||
ref.current = true; | ||
}; | ||
`, | ||
errors: [writeError], | ||
}, | ||
{ | ||
code: ` | ||
let MyComponent = function () { | ||
let ref = useRef(false); | ||
ref.current = true; | ||
}; | ||
`, | ||
errors: [writeError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = React.useRef(false); | ||
ref.current = true; | ||
} | ||
`, | ||
errors: [writeError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
return <h1>{ref.current}</h1>; | ||
} | ||
`, | ||
errors: [readError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(false); | ||
if (ref.current === 'test') { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
errors: [readError, writeError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(null); | ||
if (ref.current === undefined) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
errors: [readError, writeError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(undefined); | ||
if (ref.current === null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
errors: [readError, writeError], | ||
}, | ||
{ | ||
code: ` | ||
function MyComponent() { | ||
let ref = useRef(null); | ||
let someOtherRef = useRef(null); | ||
if (someOtherRef.current === null) { | ||
ref.current = somethingExpensive(); | ||
} | ||
} | ||
`, | ||
errors: [writeError], | ||
}, | ||
], | ||
}; | ||
|
||
const eslintTester = new ESLintTester(); | ||
eslintTester.run('react-hooks', PureRenderESLintRule, tests); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would like some help with these error messages. I'm not sure exactly how much info we'd like to expose in the message vs in the docs link.
Also, would be nice if we could link directly to the "Pitfall" heading on that docs page, but right now it doesn't seem to have an anchor link.
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.
Good job.