feat(runtime-core): improve warning for extraneous event listeners (#1005)
fix #1001
This commit is contained in:
parent
dece6102aa
commit
cebad64d22
@ -337,7 +337,7 @@ describe('attribute fallthrough', () => {
|
|||||||
it('should warn when fallthrough fails on non-single-root', () => {
|
it('should warn when fallthrough fails on non-single-root', () => {
|
||||||
const Parent = {
|
const Parent = {
|
||||||
render() {
|
render() {
|
||||||
return h(Child, { foo: 1, class: 'parent' })
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -353,12 +353,13 @@ describe('attribute fallthrough', () => {
|
|||||||
render(h(Parent), root)
|
render(h(Parent), root)
|
||||||
|
|
||||||
expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned()
|
expect(`Extraneous non-props attributes (class)`).toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not warn when $attrs is used during render', () => {
|
it('should not warn when $attrs is used during render', () => {
|
||||||
const Parent = {
|
const Parent = {
|
||||||
render() {
|
render() {
|
||||||
return h(Child, { foo: 1, class: 'parent' })
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -374,13 +375,15 @@ describe('attribute fallthrough', () => {
|
|||||||
render(h(Parent), root)
|
render(h(Parent), root)
|
||||||
|
|
||||||
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
|
||||||
|
|
||||||
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not warn when context.attrs is used during render', () => {
|
it('should not warn when context.attrs is used during render', () => {
|
||||||
const Parent = {
|
const Parent = {
|
||||||
render() {
|
render() {
|
||||||
return h(Child, { foo: 1, class: 'parent' })
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,9 +399,72 @@ describe('attribute fallthrough', () => {
|
|||||||
render(h(Parent), root)
|
render(h(Parent), root)
|
||||||
|
|
||||||
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
|
||||||
|
|
||||||
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should not warn when context.attrs is used during render (functional)', () => {
|
||||||
|
const Parent = {
|
||||||
|
render() {
|
||||||
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Child: FunctionalComponent = (_, { attrs }) => [
|
||||||
|
h('div'),
|
||||||
|
h('div', attrs)
|
||||||
|
]
|
||||||
|
|
||||||
|
Child.props = ['foo']
|
||||||
|
|
||||||
|
const root = document.createElement('div')
|
||||||
|
document.body.appendChild(root)
|
||||||
|
render(h(Parent), root)
|
||||||
|
|
||||||
|
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
|
||||||
|
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should not warn when functional component has optional props', () => {
|
||||||
|
const Parent = {
|
||||||
|
render() {
|
||||||
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Child = (props: any) => [h('div'), h('div', { class: props.class })]
|
||||||
|
|
||||||
|
const root = document.createElement('div')
|
||||||
|
document.body.appendChild(root)
|
||||||
|
render(h(Parent), root)
|
||||||
|
|
||||||
|
expect(`Extraneous non-props attributes`).not.toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).not.toHaveBeenWarned()
|
||||||
|
expect(root.innerHTML).toBe(`<div></div><div class="parent"></div>`)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should warn when functional component has props and does not use attrs', () => {
|
||||||
|
const Parent = {
|
||||||
|
render() {
|
||||||
|
return h(Child, { foo: 1, class: 'parent', onBar: () => {} })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Child: FunctionalComponent = () => [h('div'), h('div')]
|
||||||
|
|
||||||
|
Child.props = ['foo']
|
||||||
|
|
||||||
|
const root = document.createElement('div')
|
||||||
|
document.body.appendChild(root)
|
||||||
|
render(h(Parent), root)
|
||||||
|
|
||||||
|
expect(`Extraneous non-props attributes`).toHaveBeenWarned()
|
||||||
|
expect(`Extraneous non-emits event listeners`).toHaveBeenWarned()
|
||||||
|
expect(root.innerHTML).toBe(`<div></div><div></div>`)
|
||||||
|
})
|
||||||
|
|
||||||
// #677
|
// #677
|
||||||
it('should update merged dynamic attrs on optimized child root', async () => {
|
it('should update merged dynamic attrs on optimized child root', async () => {
|
||||||
const aria = ref('true')
|
const aria = ref('true')
|
||||||
|
@ -490,7 +490,9 @@ function finishComponentSetup(
|
|||||||
|
|
||||||
const attrHandlers: ProxyHandler<Data> = {
|
const attrHandlers: ProxyHandler<Data> = {
|
||||||
get: (target, key: string) => {
|
get: (target, key: string) => {
|
||||||
markAttrsAccessed()
|
if (__DEV__) {
|
||||||
|
markAttrsAccessed()
|
||||||
|
}
|
||||||
return target[key]
|
return target[key]
|
||||||
},
|
},
|
||||||
set: () => {
|
set: () => {
|
||||||
|
@ -70,13 +70,25 @@ export function renderComponentRoot(
|
|||||||
} else {
|
} else {
|
||||||
// functional
|
// functional
|
||||||
const render = Component as FunctionalComponent
|
const render = Component as FunctionalComponent
|
||||||
|
// in dev, mark attrs accessed if optional props (attrs === props)
|
||||||
|
if (__DEV__ && attrs === props) {
|
||||||
|
markAttrsAccessed()
|
||||||
|
}
|
||||||
result = normalizeVNode(
|
result = normalizeVNode(
|
||||||
render.length > 1
|
render.length > 1
|
||||||
? render(props, {
|
? render(
|
||||||
attrs,
|
props,
|
||||||
slots,
|
__DEV__
|
||||||
emit
|
? {
|
||||||
})
|
get attrs() {
|
||||||
|
markAttrsAccessed()
|
||||||
|
return attrs
|
||||||
|
},
|
||||||
|
slots,
|
||||||
|
emit
|
||||||
|
}
|
||||||
|
: { attrs, slots, emit }
|
||||||
|
)
|
||||||
: render(props, null as any /* we know it doesn't need it */)
|
: render(props, null as any /* we know it doesn't need it */)
|
||||||
)
|
)
|
||||||
fallthroughAttrs = Component.props ? attrs : getFallthroughAttrs(attrs)
|
fallthroughAttrs = Component.props ? attrs : getFallthroughAttrs(attrs)
|
||||||
@ -107,17 +119,36 @@ export function renderComponentRoot(
|
|||||||
root.patchFlag |= PatchFlags.FULL_PROPS
|
root.patchFlag |= PatchFlags.FULL_PROPS
|
||||||
}
|
}
|
||||||
} else if (__DEV__ && !accessedAttrs && root.type !== Comment) {
|
} else if (__DEV__ && !accessedAttrs && root.type !== Comment) {
|
||||||
const hasListeners = Object.keys(attrs).some(isOn)
|
const allAttrs = Object.keys(attrs)
|
||||||
warn(
|
const eventAttrs: string[] = []
|
||||||
`Extraneous non-props attributes (` +
|
const extraAttrs: string[] = []
|
||||||
`${Object.keys(attrs).join(', ')}) ` +
|
for (let i = 0, l = allAttrs.length; i < l; i++) {
|
||||||
`were passed to component but could not be automatically inherited ` +
|
const key = allAttrs[i]
|
||||||
`because component renders fragment or text root nodes.` +
|
if (isOn(key)) {
|
||||||
(hasListeners
|
// remove `on`, lowercase first letter to reflect event casing accurately
|
||||||
? ` If the v-on listener is intended to be a component custom ` +
|
eventAttrs.push(key[2].toLowerCase() + key.slice(3))
|
||||||
`event listener only, declare it using the "emits" option.`
|
} else {
|
||||||
: ``)
|
extraAttrs.push(key)
|
||||||
)
|
}
|
||||||
|
}
|
||||||
|
if (extraAttrs.length) {
|
||||||
|
warn(
|
||||||
|
`Extraneous non-props attributes (` +
|
||||||
|
`${extraAttrs.join(', ')}) ` +
|
||||||
|
`were passed to component but could not be automatically inherited ` +
|
||||||
|
`because component renders fragment or text root nodes.`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (eventAttrs.length) {
|
||||||
|
warn(
|
||||||
|
`Extraneous non-emits event listeners (` +
|
||||||
|
`${eventAttrs.join(', ')}) ` +
|
||||||
|
`were passed to component but could not be automatically inherited ` +
|
||||||
|
`because component renders fragment or text root nodes. ` +
|
||||||
|
`If the listener is intended to be a component custom event listener only, ` +
|
||||||
|
`declare it using the "emits" option.`
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user