From d183095d86db4560549e0b00e8dd244e8f5f1986 Mon Sep 17 00:00:00 2001 From: Che Guevara <836934184@qq.com> Date: Sun, 28 Nov 2021 19:58:32 +0800 Subject: [PATCH 1/3] feat(runtime-core): enhance the warning when not explicitly declare the variable --- packages/runtime-core/src/componentPublicInstance.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 8298aff42b1..16c36d8e318 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -386,6 +386,15 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { `but is not defined on instance.` ) } + } else if ( + __DEV__ && + !__TEST__ && + !(key[0] === '$' || key[0] === '_') + ) { + warn( + `Property ${JSON.stringify(key)} was accessed during render ` + + `but is not defined on instance.` + ) } }, From 848e27cf280940ee4be0243e1e1f0425ac6f1ec7 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 24 Oct 2024 07:00:15 +0000 Subject: [PATCH 2/3] [autofix.ci] apply automated fixes --- packages/runtime-core/src/componentPublicInstance.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index e48d0fbd934..215a80b17da 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -526,14 +526,10 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { `but is not defined on instance.`, ) } - } else if ( - __DEV__ && - !__TEST__ && - !(key[0] === '$' || key[0] === '_') - ) { + } else if (__DEV__ && !__TEST__ && !(key[0] === '$' || key[0] === '_')) { warn( `Property ${JSON.stringify(key)} was accessed during render ` + - `but is not defined on instance.` + `but is not defined on instance.`, ) } }, From d516036ee22ca8d875eb9629ae0c385069daf39a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E4=BB=B2?= Date: Fri, 25 Oct 2024 11:33:42 +0800 Subject: [PATCH 3/3] feat(runtime-core): enhance the warning when not explicitly declare the variable --- .../src/componentPublicInstance.ts | 2 +- packages/vue/__tests__/index.spec.ts | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 215a80b17da..8a769b97180 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -526,7 +526,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { `but is not defined on instance.`, ) } - } else if (__DEV__ && !__TEST__ && !(key[0] === '$' || key[0] === '_')) { + } else if (__DEV__ && !__TEST__ && !isReservedPrefix(key[0])) { warn( `Property ${JSON.stringify(key)} was accessed during render ` + `but is not defined on instance.`, diff --git a/packages/vue/__tests__/index.spec.ts b/packages/vue/__tests__/index.spec.ts index 0c969f15981..e76577e138a 100644 --- a/packages/vue/__tests__/index.spec.ts +++ b/packages/vue/__tests__/index.spec.ts @@ -311,4 +311,27 @@ describe('compiler + runtime integration', () => { app.mount(root) expect(root.innerHTML).toBe('
60000000100000111
') }) + + test('Warning when use undeclared variable in template', () => { + const app = createApp({ + template: ` +
handler
+
dddd{{dddd}}
+
no warn: {{message}}
+ `, + setup() { + return { + message: 'hello', + } + }, + }) + const root = document.createElement('div') + app.mount(root) + expect( + `[Vue warn]: Property "handler" was accessed during render but is not defined on instance.`, + ).toHaveBeenWarned() + expect( + `[Vue warn]: Property "dddd" was accessed during render but is not defined on instance.`, + ).toHaveBeenWarned() + }) })