fix(compiler): avoid false-positive for destructured hook identifiers that are only called #35335
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.
Summary:
Reproduced a false-positive where destructuring a hook-named identifier from an object and then calling it is flagged:
const { useValue } = hooks;
useValue(); // should be allowed
Root cause: The compiler treated hook-like names as values too eagerly without confirming how the binding is used.
Fix: For each Place in the HIR, compute whether it is only used as the callee of calls. If so, skip the "Hooks may not be referenced as normal values" error for KnownHook kinds. Otherwise preserve existing behavior.
Repro (Playground):
https://playground.react.dev/#N4Igzg9grgTgxgUxALhHCA7MAXABFMBANQEMAbKBXAXlwAoBKGgPn0IFkEBbCOxl3ABYATABpcAbQC6DANwAdDOix4AFhAgBrMDVzA2xcpVwBfBRkXKcuAGIkAlmR21+1VsEW5cVvPoKGKKmRcKhNddS0wcy8fXAA3Iypaf1JAxmjcGARsWAxcAB4wAAcSDGZgBMCTfIB6YtLmRRNFS0xrAHUIGG1dV3dPbzbfA1TKYLjTcI1tDNjK41o49IGsnJg8wpKyisTquq3GjGaMEFE0TAAzewBzFBB7LiKuvGwATyKqfQAFCmv7DAA8kVsPY2pMLjAIFxcAByABGJDhCDIAFoir9-iisiQ4NgUehHo4EDAagATew4GHmRR0DxHXBgEggsBXBA6H5QP6A4GgrByU7gdQAdwAkhhsMSMOQwCgLtKECYgA
Notes: