refactor(kit): processInject#1112
Open
skirtles-code wants to merge 1 commit into
Open
Conversation
✅ Deploy Preview for vue-devtools-docs canceled.
|
@vue/devtools-applet
@vue/devtools-core
@vue/devtools
@vue/devtools-api
@vue/devtools-kit
@vue/devtools-electron
@vue/devtools-shared
@vue/devtools-ui
vite-plugin-vue-devtools
commit: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 were a few things about
processInjectthat caught my eye, which I've attempted to address here.instance.providesusesObject.create(null), so accessinginstance.provides.hasOwnPropertywill throw an error. In practice, it's quite difficult to hit that error, and the error is caught byreturnError, but nevertheless,instance.provides.hasOwnPropertywill never succeed.Due to that error, the
defaultValueis never actually used. However, even if it were used, there are some problems with that value. The logic for settingdefaultValueleads to it being shared between all injections, so if multiple injections attempt to set adefaultit will just use the final value as the value for all injections. It also doesn't account for when thedefaultis a factory function. In practice, we can just remove all the code related todefault. Vue will set the default oninstance.ctx, so we don't need any special handling for it in the devtools.I made this change to ensure
originalKeyis always set:That allows the later check for
originalKey && key !== originalKeyto be simplified to justkey !== originalKey.I've switched a few calls from
x.toString()to useString(x)instead. I think that's generally regarded more idiomatic, handling a wider range of values safely. Vue core uses that pattern extensively. In practice, I don't think it matters in this specific code, butString(x)saves us having to worry about edge cases, especially as the values have typeany.I've also extracted a separate
hasOwnfunction. The code for that function comes from Vue core. Using a separate function like this avoids any of the risks associated withObject.create(null). That function can be moved to a shared location and reused elsewhere, but I thought it'd be better to do that in a separate PR, rather than expanding the scope of this refactoring to other files.Using
hasOwnallows theeslint-disable-next-lineto be removed.I've retained the call to
returnError, as that seems to be a pattern used throughout this file, but in practice I don't think the new code will throw any errors.