Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): merge props and emits strategy #8052

Merged
merged 9 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion packages/runtime-core/__tests__/componentEmits.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
h,
nodeOps,
toHandlers,
nextTick
nextTick,
ComponentPublicInstance
} from '@vue/runtime-test'
import { isEmitListener } from '../src/componentEmits'

Expand Down Expand Up @@ -454,4 +455,55 @@ describe('component: emit', () => {
await nextTick()
expect(fn).not.toHaveBeenCalled()
})

test('merge string array emits', async () => {
const ComponentA = defineComponent({
emits: ['one', 'two']
})
const ComponentB = defineComponent({
emits: ['three']
})
const renderFn = vi.fn(function (this: ComponentPublicInstance) {
expect(this.$options.emits).toEqual(['one', 'two', 'three'])
return h('div')
})
const ComponentC = defineComponent({
render: renderFn,
mixins: [ComponentA, ComponentB]
})
const el = nodeOps.createElement('div')
expect(renderFn).toHaveBeenCalledTimes(0)
render(h(ComponentC), el)
expect(renderFn).toHaveBeenCalledTimes(1)
})

test('merge object emits', async () => {
const twoFn = vi.fn((v: unknown) => !v)
const ComponentA = defineComponent({
emits: {
one: null,
two: twoFn
}
})
const ComponentB = defineComponent({
emits: ['three']
})
const renderFn = vi.fn(function (this: ComponentPublicInstance) {
expect(this.$options.emits).toEqual({
one: null,
two: twoFn,
three: null
})
expect(this.$options.emits.two).toBe(twoFn)
return h('div')
})
const ComponentC = defineComponent({
render: renderFn,
mixins: [ComponentA, ComponentB]
})
const el = nodeOps.createElement('div')
expect(renderFn).toHaveBeenCalledTimes(0)
render(h(ComponentC), el)
expect(renderFn).toHaveBeenCalledTimes(1)
})
})
9 changes: 7 additions & 2 deletions packages/runtime-core/src/apiSetupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,15 @@ function getContext(): SetupContext {
return i.setupContext || (i.setupContext = createSetupContext(i))
}

function normalizePropsOrEmits(props: ComponentPropsOptions | EmitsOptions) {
/**
* @internal
*/
export function normalizePropsOrEmits(
props: ComponentPropsOptions | EmitsOptions
) {
return isArray(props)
? props.reduce(
(normalized, p) => ((normalized[p] = {}), normalized),
(normalized, p) => ((normalized[p] = null), normalized),
{} as ComponentObjectPropsOptions | ObjectEmitsOptions
)
: props
Expand Down
36 changes: 32 additions & 4 deletions packages/runtime-core/src/componentOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ import {
import {
ComponentObjectPropsOptions,
ExtractPropTypes,
ExtractDefaultPropTypes
ExtractDefaultPropTypes,
ComponentPropsOptions
} from './componentProps'
import { EmitsOptions, EmitsToProps } from './componentEmits'
import { Directive } from './directives'
Expand All @@ -75,6 +76,7 @@ import {
import { OptionMergeFunction } from './apiCreateApp'
import { LifecycleHooks } from './enums'
import { SlotsType } from './componentSlots'
import { normalizePropsOrEmits } from './apiSetupHelpers'

/**
* Interface for declaring custom options.
Expand Down Expand Up @@ -1069,8 +1071,8 @@ export function mergeOptions(

export const internalOptionMergeStrats: Record<string, Function> = {
data: mergeDataFn,
props: mergeObjectOptions, // TODO
emits: mergeObjectOptions, // TODO
props: mergeEmitsOrPropsOptions,
emits: mergeEmitsOrPropsOptions,
// objects
methods: mergeObjectOptions,
computed: mergeObjectOptions,
Expand Down Expand Up @@ -1147,7 +1149,33 @@ function mergeAsArray<T = Function>(to: T[] | T | undefined, from: T | T[]) {
}

function mergeObjectOptions(to: Object | undefined, from: Object | undefined) {
return to ? extend(extend(Object.create(null), to), from) : from
return to ? extend(Object.create(null), to, from) : from
}

function mergeEmitsOrPropsOptions(
to: EmitsOptions | undefined,
from: EmitsOptions | undefined
): EmitsOptions | undefined
function mergeEmitsOrPropsOptions(
to: ComponentPropsOptions | undefined,
from: ComponentPropsOptions | undefined
): ComponentPropsOptions | undefined
function mergeEmitsOrPropsOptions(
to: ComponentPropsOptions | EmitsOptions | undefined,
from: ComponentPropsOptions | EmitsOptions | undefined
) {
if (to) {
if (isArray(to) && isArray(from)) {
return [...new Set([...to, ...from])]
}
return extend(
Object.create(null),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is added to avoid mutating raw user objects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like the same problem that's currently occurring in the devtools when inspecting components with mixins: https://github.com/vuejs/devtools/issues/2037

normalizePropsOrEmits(to),
normalizePropsOrEmits(from ?? {})
)
} else {
return from
}
}

function mergeWatchOptions(
Expand Down