dx(computed): warn incorrect use of inject or lifecycle hooks inside computed #10038
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.
This is a follow up to 324e817. That added a warning when
getCurrentInstance
is called from acomputed
getter.That warning does cover several real cases, including
useI18n
and the Oruga problem in #10001. But in #9974 I mistakenly suggested that it would also cover misuse ofinject
or lifecycle hooks.While those APIs do use the current instance, they don't access it via
getCurrentInstance
.I've added similar warnings for both
inject
and lifecycle hooks. There are various other functions where warnings could also be added, but for this PR I stopped at these two. In practice, I suspect that'll cover most composables that aren't safe to use in this way.One advantage of not using
getCurrentInstance
is that it allows the warnings to be a bit more specific. A warning that explicitly mentionsinject
is probably more helpful than a warning about the undocumentedgetCurrentInstance
.Note that for
inject
I also check for!currentApp
. While I don't have a specific use case in mind, it seemed like it could be legitimate to callinject
ifrunWithContext
is wrapped around it, as that would guarantee the existence of a suitable injection context.