wip: fix all runtime-core type errors

This commit is contained in:
Evan You 2024-12-06 01:19:20 +08:00
parent 30e24ce986
commit 300bb0859a
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
9 changed files with 53 additions and 38 deletions

View File

@ -11,7 +11,7 @@ import {
withDirectives,
} from '@vue/runtime-test'
import {
type ComponentInternalInstance,
type GenericComponentInstance,
currentInstance,
} from '../src/component'
@ -111,7 +111,7 @@ describe('directives', () => {
unmounted,
}
let _instance: ComponentInternalInstance | null = null
let _instance: GenericComponentInstance | null = null
let _vnode: VNode | null = null
let _prevVnode: VNode | null = null
const Comp = {
@ -171,7 +171,7 @@ describe('directives', () => {
expect(prevVNode).toBe(_prevVnode)
}) as DirectiveHook)
let _instance: ComponentInternalInstance | null = null
let _instance: GenericComponentInstance | null = null
let _vnode: VNode | null = null
let _prevVnode: VNode | null = null
const Comp = {
@ -300,7 +300,7 @@ describe('directives', () => {
unmounted,
}
let _instance: ComponentInternalInstance | null = null
let _instance: GenericComponentInstance | null = null
let _vnode: VNode | null = null
let _prevVnode: VNode | null = null

View File

@ -13,7 +13,7 @@ import {
transformVNodeArgs,
} from '../src/vnode'
import { PatchFlags, ShapeFlags } from '@vue/shared'
import type { Data } from '@vue/runtime-shared'
import type { Data } from '../src/component'
import { h, isReactive, reactive, ref, setBlockTracking, withCtx } from '../src'
import { createApp, nodeOps, serializeInner } from '@vue/runtime-test'
import { setCurrentRenderingInstance } from '../src/componentRenderContext'

View File

@ -143,7 +143,7 @@ export function defineAsyncComponent<
},
setup() {
const instance = currentInstance!
const instance = currentInstance as ComponentInternalInstance
markAsyncBoundary(instance)
// already resolved

View File

@ -382,6 +382,14 @@ export interface GenericComponentInstance {
isUnmounted: boolean
isDeactivated: boolean
/**
* for tracking useId()
* first element is the current boundary prefix
* second number is the index of the useId call within that boundary
* @internal
*/
ids: [string, number, number]
// for vapor the following two are dev only
/**
* resolved props options
@ -394,8 +402,15 @@ export interface GenericComponentInstance {
*/
emitsOptions?: ObjectEmitsOptions | null
// the following are for error handling logic only
/**
* Public instance proxy, vdom only
*/
proxy?: any
/**
* suspense related
* @internal
*/
suspense: SuspenseBoundary | null
// lifecycle
/**
@ -497,13 +512,6 @@ export interface ComponentInternalInstance extends GenericComponentInstance {
* @internal
*/
render: InternalRenderFunction | null
/**
* for tracking useId()
* first element is the current boundary prefix
* second number is the index of the useId call within that boundary
* @internal
*/
ids: [string, number, number]
/**
* cache for proxy access type to avoid hasOwnProperty calls
* @internal
@ -600,11 +608,6 @@ export interface ComponentInternalInstance extends GenericComponentInstance {
* @internal
*/
ctx: Data
/**
* suspense related
* @internal
*/
suspense: SuspenseBoundary | null
/**
* suspense pending batch id
* @internal

View File

@ -1,4 +1,4 @@
import { type ComponentInternalInstance, currentInstance } from './component'
import { type ComponentInternalInstance, getCurrentInstance } from './component'
import {
type VNode,
type VNodeChild,
@ -94,16 +94,15 @@ const normalizeSlot = (
return rawSlot as Slot
}
const normalized = withCtx((...args: any[]) => {
if (
__DEV__ &&
currentInstance &&
(!ctx || ctx.root === currentInstance.root)
) {
warn(
`Slot "${key}" invoked outside of the render function: ` +
`this will not track dependencies used in the slot. ` +
`Invoke the slot function inside the render function instead.`,
)
if (__DEV__) {
const currentInstance = getCurrentInstance()
if (currentInstance && (!ctx || ctx.root === currentInstance.root)) {
warn(
`Slot "${key}" invoked outside of the render function: ` +
`this will not track dependencies used in the slot. ` +
`Invoke the slot function inside the render function instead.`,
)
}
}
return normalizeSlotValue(rawSlot(...args))
}, ctx) as Slot

View File

@ -1,4 +1,5 @@
import {
type ComponentInternalInstance,
type ComponentOptions,
type ConcreteComponent,
currentInstance,
@ -103,8 +104,13 @@ function resolveAsset(
const res =
// local registration
// check instance[type] first which is resolved for options API
resolve(instance[type] || (Component as ComponentOptions)[type], name) ||
resolve(
(instance as ComponentInternalInstance)[type] ||
(Component as ComponentOptions)[type],
name,
) ||
// global registration
// @ts-expect-error filters only exist in compat mode
resolve(instance.appContext[type], name)
if (!res && maybeSelfReference) {

View File

@ -1,11 +1,11 @@
import {
type ComponentInternalInstance,
getCurrentInstance,
type GenericComponentInstance,
getCurrentGenericInstance,
} from '../component'
import { warn } from '../warning'
export function useId(): string {
const i = getCurrentInstance()
const i = getCurrentGenericInstance()
if (i) {
return (i.appContext.config.idPrefix || 'v') + '-' + i.ids[0] + i.ids[1]++
} else if (__DEV__) {
@ -23,6 +23,6 @@ export function useId(): string {
* - components with async setup()
* - components with serverPrefetch
*/
export function markAsyncBoundary(instance: ComponentInternalInstance): void {
export function markAsyncBoundary(instance: GenericComponentInstance): void {
instance.ids = [instance.ids[0] + instance.ids[2]++ + '-', 0, 0]
}

View File

@ -2587,7 +2587,7 @@ function locateNonHydratedAsyncRoot(
}
}
export function invalidateMount(hooks: LifecycleHook): void {
export function invalidateMount(hooks: LifecycleHook | undefined): void {
if (hooks) {
for (let i = 0; i < hooks.length; i++)
hooks[i].flags! |= SchedulerJobFlags.DISPOSED

View File

@ -9,6 +9,7 @@ import {
type LifecycleHook,
type NormalizedPropsOptions,
type ObjectEmitsOptions,
type SuspenseBoundary,
currentInstance,
nextUid,
popWarningContext,
@ -121,6 +122,7 @@ export function createComponent(
)
instance.block = []
} else {
instance.setupState = setupResult
instance.block = component.render.call(null, setupResult)
}
} else {
@ -178,6 +180,10 @@ export class VaporComponentInstance implements GenericComponentInstance {
refs: Record<string, any>
// for provide / inject
provides: Record<string, any>
// for useId
ids: [string, number, number]
// for suspense
suspense: SuspenseBoundary | null
hasFallthrough: boolean
@ -211,7 +217,7 @@ export class VaporComponentInstance implements GenericComponentInstance {
this.vapor = true
this.uid = nextUid()
this.type = comp
this.parent = currentInstance // TODO when inside
this.parent = currentInstance // TODO proper parent source when inside vdom instance
this.appContext = currentInstance
? currentInstance.appContext
: emptyContext
@ -223,7 +229,8 @@ export class VaporComponentInstance implements GenericComponentInstance {
? currentInstance.provides
: Object.create(this.appContext.provides)
this.refs = EMPTY_OBJ
this.emitted = this.ec = this.exposed = this.propsDefaults = null
this.ids = currentInstance ? currentInstance.ids : ['', 0, 0]
this.emitted = this.exposed = this.propsDefaults = this.suspense = null
this.isMounted =
this.isUnmounted =
this.isUpdating =