mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +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 {
|
class ScopeTracker {
|
||||||
|
// the top of the stack is not a part of current key, it is used for next level
|
||||||
scopeIndexStack: number[]
|
scopeIndexStack: number[]
|
||||||
curScopeKey: string
|
curScopeKey: string
|
||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
// top level
|
|
||||||
this.scopeIndexStack = [0]
|
this.scopeIndexStack = [0]
|
||||||
this.curScopeKey = '0'
|
this.curScopeKey = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
getKey () {
|
getKey () {
|
||||||
@ -173,8 +185,7 @@ class ScopedVarsCollector {
|
|||||||
|
|
||||||
constructor () {
|
constructor () {
|
||||||
this.all = new Map()
|
this.all = new Map()
|
||||||
// top level
|
this.curScopeKey = ''
|
||||||
this.curScopeKey = '0'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
refresh (scopeKey: string) {
|
refresh (scopeKey: string) {
|
||||||
@ -192,7 +203,7 @@ class ScopedVarsCollector {
|
|||||||
|
|
||||||
hasVar (scopeKey: string, name: string) {
|
hasVar (scopeKey: string, name: string) {
|
||||||
const indices = scopeKey.split('-').map(Number)
|
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)) {
|
if (this.all.get(indices.slice(0, i).join('-'))?.has(name)) {
|
||||||
return true
|
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">
|
<script setup lang="ts">
|
||||||
|
import { ShouldNotBeKeyed } from '~/other-composables-folder/local'
|
||||||
|
|
||||||
function localScopedComposables () {
|
function localScopedComposables () {
|
||||||
const _assert = (key?: string) => key ?? 'was not keyed'
|
const _assert = (key?: string) => key ?? 'was not keyed'
|
||||||
|
|
||||||
@ -39,11 +41,15 @@ function localScopedComposables () {
|
|||||||
})()]
|
})()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fromNonComponentFile () {
|
||||||
|
return [_assert(ShouldNotBeKeyed)]
|
||||||
|
}
|
||||||
|
|
||||||
function useCustomKeyedComposable (arg?: string) {
|
function useCustomKeyedComposable (arg?: string) {
|
||||||
return _assert(arg)
|
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')
|
const skippedLocalScopedComposables = localScopedComposables().every(res => res === 'was not keyed')
|
||||||
</script>
|
</script>
|
||||||
|
Loading…
Reference in New Issue
Block a user