From c2dea88dbab93e90bb3c5f6ba63478ba4cde5158 Mon Sep 17 00:00:00 2001 From: shadowings-zy Date: Tue, 3 Nov 2020 12:57:52 +0800 Subject: [PATCH 1/4] fix(runtime-core): fix the warning "Extraneous non-emits event listeners" shows up even when emits is set, see issue#2540 --- packages/runtime-core/__tests__/componentEmits.spec.ts | 3 +++ packages/runtime-core/src/componentEmits.ts | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index 53c8d324ea3..38fddf9ddc3 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -290,5 +290,8 @@ describe('component: emit', () => { // .once listeners expect(isEmitListener(options, 'onClickOnce')).toBe(true) expect(isEmitListener(options, 'onclickOnce')).toBe(false) + // kebab-case listener + const kebabOptions = { 'test-event': null } + expect(isEmitListener(kebabOptions, 'onTestEvent')).toBe(true) }) }) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 6b9e505de62..a67ce33350e 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -204,6 +204,7 @@ export function isEmitListener( key = key.replace(/Once$/, '') return ( hasOwn(options, key[2].toLowerCase() + key.slice(3)) || - hasOwn(options, key.slice(2)) + hasOwn(options, key.slice(2)) || + hasOwn(options, hyphenate(key.slice(2))) ) } From bd15f7324fda26545120ea37b06890f6abee10c7 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 27 Nov 2020 15:07:35 -0500 Subject: [PATCH 2/4] Update componentEmits.ts --- packages/runtime-core/src/componentEmits.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index a67ce33350e..73dbb014d75 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -201,10 +201,6 @@ export function isEmitListener( if (!options || !isOn(key)) { return false } - key = key.replace(/Once$/, '') - return ( - hasOwn(options, key[2].toLowerCase() + key.slice(3)) || - hasOwn(options, key.slice(2)) || - hasOwn(options, hyphenate(key.slice(2))) - ) + key = key.slice(2).replace(/Once$/, '') + return hasOwn(options, hyphenate(key)) || hasOwn(options, key) } From f49b466a9cff4614b6a7788b4f26780d33199a40 Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 27 Nov 2020 15:15:20 -0500 Subject: [PATCH 3/4] Update componentEmits.ts --- packages/runtime-core/src/componentEmits.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentEmits.ts b/packages/runtime-core/src/componentEmits.ts index 73dbb014d75..81af34e6708 100644 --- a/packages/runtime-core/src/componentEmits.ts +++ b/packages/runtime-core/src/componentEmits.ts @@ -202,5 +202,9 @@ export function isEmitListener( return false } key = key.slice(2).replace(/Once$/, '') - return hasOwn(options, hyphenate(key)) || hasOwn(options, key) + return ( + hasOwn(options, key[0].toLowerCase() + key.slice(1)) || + hasOwn(options, hyphenate(key)) || + hasOwn(options, key) + ) } From 1223e6ead0bf41c7660b71ded189d65ccdefa81c Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 27 Nov 2020 15:18:59 -0500 Subject: [PATCH 4/4] Update componentEmits.spec.ts --- .../__tests__/componentEmits.spec.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/runtime-core/__tests__/componentEmits.spec.ts b/packages/runtime-core/__tests__/componentEmits.spec.ts index 38fddf9ddc3..2fb1fbb068a 100644 --- a/packages/runtime-core/__tests__/componentEmits.spec.ts +++ b/packages/runtime-core/__tests__/componentEmits.spec.ts @@ -283,15 +283,23 @@ describe('component: emit', () => { }) test('isEmitListener', () => { - const options = { click: null } + const options = { + click: null, + 'test-event': null, + fooBar: null, + FooBaz: null + } expect(isEmitListener(options, 'onClick')).toBe(true) expect(isEmitListener(options, 'onclick')).toBe(false) expect(isEmitListener(options, 'onBlick')).toBe(false) // .once listeners expect(isEmitListener(options, 'onClickOnce')).toBe(true) expect(isEmitListener(options, 'onclickOnce')).toBe(false) - // kebab-case listener - const kebabOptions = { 'test-event': null } - expect(isEmitListener(kebabOptions, 'onTestEvent')).toBe(true) + // kebab-case option + expect(isEmitListener(options, 'onTestEvent')).toBe(true) + // camelCase option + expect(isEmitListener(options, 'onFooBar')).toBe(true) + // PascalCase option + expect(isEmitListener(options, 'onFooBaz')).toBe(true) }) })