From 9cc40b0dd7856310017e2eac315176eecd6c1af5 Mon Sep 17 00:00:00 2001 From: daiwei Date: Thu, 14 Mar 2024 11:43:42 +0800 Subject: [PATCH 1/3] fix(compile-sfc): analyze v-bind shorthand usage in template --- .../__snapshots__/importUsageCheck.spec.ts.snap | 4 ++-- .../__tests__/compileScript/importUsageCheck.spec.ts | 5 +++-- packages/compiler-sfc/src/script/importUsageCheck.ts | 3 +++ 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap index 764d120a744..722e3340a74 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/importUsageCheck.spec.ts.snap @@ -66,14 +66,14 @@ return { get vMyDir() { return vMyDir } } exports[`dynamic arguments 1`] = ` "import { defineComponent as _defineComponent } from 'vue' -import { FooBar, foo, bar, unused, baz } from './x' +import { FooBar, foo, bar, unused, baz, msg } from './x' export default /*#__PURE__*/_defineComponent({ setup(__props, { expose: __expose }) { __expose(); -return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { return bar }, get baz() { return baz } } +return { get FooBar() { return FooBar }, get foo() { return foo }, get bar() { return bar }, get baz() { return baz }, get msg() { return msg } } } })" diff --git a/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts b/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts index d9fd1dfe529..b842f7a466d 100644 --- a/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/importUsageCheck.spec.ts @@ -45,7 +45,7 @@ test('directive', () => { test('dynamic arguments', () => { const { content } = compile(` `) expect(content).toMatch( `return { get FooBar() { return FooBar }, get foo() { return foo }, ` + - `get bar() { return bar }, get baz() { return baz } }`, + `get bar() { return bar }, get baz() { return baz }, get msg() { return msg } }`, ) assertCode(content) }) diff --git a/packages/compiler-sfc/src/script/importUsageCheck.ts b/packages/compiler-sfc/src/script/importUsageCheck.ts index 211efc49089..c7845604b2f 100644 --- a/packages/compiler-sfc/src/script/importUsageCheck.ts +++ b/packages/compiler-sfc/src/script/importUsageCheck.ts @@ -60,6 +60,9 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set { extractIdentifiers(ids, prop.forParseResult!.source) } else if (prop.exp) { extractIdentifiers(ids, prop.exp) + } else if (prop.name === 'bind' && !prop.exp) { + // v-bind shorthand rawName as identifier + ids.add(prop.rawName!.slice(1)) } } if ( From da890b7cb41454b2128bc298a529d3cfb85b6d77 Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 15 Mar 2024 16:58:33 +0800 Subject: [PATCH 2/3] Update importUsageCheck.ts --- packages/compiler-sfc/src/script/importUsageCheck.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/script/importUsageCheck.ts b/packages/compiler-sfc/src/script/importUsageCheck.ts index c7845604b2f..4db336cc307 100644 --- a/packages/compiler-sfc/src/script/importUsageCheck.ts +++ b/packages/compiler-sfc/src/script/importUsageCheck.ts @@ -62,7 +62,7 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set { extractIdentifiers(ids, prop.exp) } else if (prop.name === 'bind' && !prop.exp) { // v-bind shorthand rawName as identifier - ids.add(prop.rawName!.slice(1)) + ids.add((prop.arg as SimpleExpressionNode).content) } } if ( From 20d4c13154a77d5c61a4c168d41426d72618f3c1 Mon Sep 17 00:00:00 2001 From: edison Date: Fri, 15 Mar 2024 17:00:14 +0800 Subject: [PATCH 3/3] Update importUsageCheck.ts --- packages/compiler-sfc/src/script/importUsageCheck.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compiler-sfc/src/script/importUsageCheck.ts b/packages/compiler-sfc/src/script/importUsageCheck.ts index 4db336cc307..6b9fbc634cc 100644 --- a/packages/compiler-sfc/src/script/importUsageCheck.ts +++ b/packages/compiler-sfc/src/script/importUsageCheck.ts @@ -61,7 +61,7 @@ function resolveTemplateUsedIdentifiers(sfc: SFCDescriptor): Set { } else if (prop.exp) { extractIdentifiers(ids, prop.exp) } else if (prop.name === 'bind' && !prop.exp) { - // v-bind shorthand rawName as identifier + // v-bind shorthand name as identifier ids.add((prop.arg as SimpleExpressionNode).content) } }