mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-21 21:25:11 +00:00
fix(vite): use ''
key for root scope in variable collector (#22679)
This commit is contained in:
parent
08b1950ffb
commit
4e538a03e2
@ -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
|
||||
}
|
||||
|
5
test/fixtures/basic/other-composables-folder/local.ts
vendored
Normal file
5
test/fixtures/basic/other-composables-folder/local.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
function useAsyncData (s?: any) { return s }
|
||||
|
||||
export const ShouldNotBeKeyed = (() => {
|
||||
return useAsyncData()
|
||||
})()
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user