fix(vite): use '' key for root scope in variable collector (#22679)

This commit is contained in:
anhao 2023-08-17 21:35:28 +08:00 committed by GitHub
parent 08b1950ffb
commit 4e538a03e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 6 deletions

View File

@ -141,14 +141,26 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio
}
})
/*
* track scopes with unique keys. for example
* ```js
* // root scope, marked as ''
* function a () { // '0'
* function b () {} // '0-0'
* function c () {} // '0-1'
* }
* function d () {} // '1'
* // ''
* ```
* */
class ScopeTracker {
// the top of the stack is not a part of current key, it is used for next level
scopeIndexStack: number[]
curScopeKey: string
constructor () {
// top level
this.scopeIndexStack = [0]
this.curScopeKey = '0'
this.curScopeKey = ''
}
getKey () {
@ -173,8 +185,7 @@ class ScopedVarsCollector {
constructor () {
this.all = new Map()
// top level
this.curScopeKey = '0'
this.curScopeKey = ''
}
refresh (scopeKey: string) {
@ -192,7 +203,7 @@ class ScopedVarsCollector {
hasVar (scopeKey: string, name: string) {
const indices = scopeKey.split('-').map(Number)
for (let i = indices.length; i > 0; i--) {
for (let i = indices.length; i >= 0; i--) {
if (this.all.get(indices.slice(0, i).join('-'))?.has(name)) {
return true
}

View File

@ -0,0 +1,5 @@
function useAsyncData (s?: any) { return s }
export const ShouldNotBeKeyed = (() => {
return useAsyncData()
})()

View File

@ -1,4 +1,6 @@
<script setup lang="ts">
import { ShouldNotBeKeyed } from '~/other-composables-folder/local'
function localScopedComposables () {
const _assert = (key?: string) => key ?? 'was not keyed'
@ -39,11 +41,15 @@ function localScopedComposables () {
})()]
}
function fromNonComponentFile () {
return [_assert(ShouldNotBeKeyed)]
}
function useCustomKeyedComposable (arg?: string) {
return _assert(arg)
}
return [...basic(), ...hoisting(), ...complex(), ...deeperScope(), useCustomKeyedComposable()]
return [...basic(), ...hoisting(), ...complex(), ...deeperScope(), ...fromNonComponentFile(), useCustomKeyedComposable()]
}
const skippedLocalScopedComposables = localScopedComposables().every(res => res === 'was not keyed')
</script>