-
Notifications
You must be signed in to change notification settings - Fork 1.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
Add lint plugin to check for ref access during render #3835
Closed
Closed
Changes from all commits
Commits
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
This file contains 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
This file contains 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,189 @@ | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you 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 REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
// Copied from https://github.com/facebook/react/pull/24506 | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforces component render purity', | ||
recommended: true, | ||
url: 'https://beta.reactjs.org/learn/keeping-components-pure' | ||
} | ||
}, | ||
create(context) { | ||
return { | ||
MemberExpression(member) { | ||
// Look for member expressions that look like refs (i.e. `ref.current`). | ||
if ( | ||
member.object.type !== 'Identifier' || | ||
member.computed || | ||
member.property.type !== 'Identifier' || | ||
member.property.name !== 'current' | ||
) { | ||
return; | ||
} | ||
|
||
// Find the parent function of this node, as well as any if statement matching against the ref value | ||
// (i.e. lazy init pattern shown in React docs). | ||
let node = member; | ||
let fn; | ||
let conditional; | ||
while (node) { | ||
if ( | ||
node.type === 'FunctionDeclaration' || | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' | ||
) { | ||
fn = node; | ||
break; | ||
} | ||
|
||
if ( | ||
node.type === 'IfStatement' && | ||
node.test.type === 'BinaryExpression' && | ||
(node.test.operator === '==' || node.test.operator === '===') && | ||
isMemberExpressionEqual(node.test.left, member) | ||
) { | ||
conditional = node.test; | ||
} | ||
|
||
node = node.parent; | ||
} | ||
|
||
if (!fn) { | ||
return; | ||
} | ||
|
||
// Find the variable definition for the object. | ||
const variable = getVariable(context.getScope(), member.object.name); | ||
if (!variable) { | ||
return; | ||
} | ||
|
||
// Find the initialization of the variable and see if it's a call to useRef. | ||
const refDefinition = variable.defs.find(def => { | ||
const init = def.node.init; | ||
if (!init) { | ||
return false; | ||
} | ||
|
||
return ( | ||
init.type === 'CallExpression' && | ||
((init.callee.type === 'Identifier' && | ||
init.callee.name === 'useRef') || | ||
(init.callee.type === 'MemberExpression' && | ||
init.callee.object.type === 'Identifier' && | ||
init.callee.object.name === 'React' && | ||
init.callee.property.type === 'Identifier' && | ||
init.callee.property.name === 'useRef')) && | ||
parentFunction(def.node) === fn | ||
); | ||
}); | ||
|
||
if (refDefinition) { | ||
// If within an if statement, check if comparing with the initial value passed to useRef. | ||
// This indicates the lazy init pattern, which is allowed. | ||
if (conditional) { | ||
const init = refDefinition.node.init.arguments[0] || { | ||
type: 'Identifier', | ||
name: 'undefined' | ||
}; | ||
if (isLiteralEqual(conditional.operator, init, conditional.right)) { | ||
return; | ||
} | ||
} | ||
|
||
// Otherwise, report an error for either writing or reading to this ref based on parent expression. | ||
context.report({ | ||
node: member, | ||
message: | ||
member.parent.type === 'AssignmentExpression' && | ||
member.parent.left === member | ||
? 'Writing to refs during rendering is not allowed. Move this into a useEffect or useLayoutEffect. See https://beta.reactjs.org/apis/useref' | ||
: 'Reading from refs during rendering is not allowed. See https://beta.reactjs.org/apis/useref' | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
function getVariable(scope, name) { | ||
while (scope) { | ||
const variable = scope.set.get(name); | ||
if (variable) { | ||
return variable; | ||
} | ||
|
||
scope = scope.upper; | ||
} | ||
} | ||
|
||
function parentFunction(node) { | ||
while (node) { | ||
if ( | ||
node.type === 'FunctionDeclaration' || | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' | ||
) { | ||
return node; | ||
} | ||
|
||
node = node.parent; | ||
} | ||
} | ||
|
||
function isMemberExpressionEqual(a, b) { | ||
if (a === b) { | ||
return true; | ||
} | ||
|
||
return ( | ||
a.type === 'MemberExpression' && | ||
b.type === 'MemberExpression' && | ||
a.object.type === 'Identifier' && | ||
b.object.type === 'Identifier' && | ||
a.object.name === b.object.name && | ||
a.property.type === 'Identifier' && | ||
b.property.type === 'Identifier' && | ||
a.property.name === b.property.name | ||
); | ||
} | ||
|
||
function isLiteralEqual(operator, a, b) { | ||
let aValue, bValue; | ||
if (a.type === 'Identifier' && a.name === 'undefined') { | ||
aValue = undefined; | ||
} else if (a.type === 'Literal') { | ||
aValue = a.value; | ||
} else { | ||
return; | ||
} | ||
|
||
if (b.type === 'Identifier' && b.name === 'undefined') { | ||
bValue = undefined; | ||
} else if (b.type === 'Literal') { | ||
bValue = b.value; | ||
} else { | ||
return; | ||
} | ||
|
||
if (operator === '===') { | ||
return aValue === bValue; | ||
} else if (operator === '==') { | ||
// eslint-disable-next-line | ||
return aValue == bValue; | ||
} | ||
|
||
return false; | ||
} |
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 think we're going to need to look for other things as well, such as
virtualizer.delegate
orvirtualizer.anything
really https://github.com/adobe/react-spectrum/blob/main/packages/%40react-stately/virtualizer/src/useVirtualizerState.ts#L55we'll also probably want to check if current is being written to or read from inside a
useMemo
since that's equivalent to render time? https://github.com/adobe/react-spectrum/blob/main/packages/%40react-stately/collections/src/useCollection.ts#L25There 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.
probably things like our weak maps hook contexts as well
which are hard because we can't just put the writes and reads into useLayoutEffect since the child ones would run before the parent... so they'd always run on stale data
https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/calendar/src/useCalendarBase.ts#L66
https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/calendar/src/useCalendarGrid.ts#L125