From a370e8006a70ea49a7d04c8c1a42d0947eba5dea Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 25 Apr 2023 09:29:30 +0800 Subject: [PATCH] perf(compiler-sfc): infer ref binding type for more built-in methods --- .../compiler-sfc/__tests__/compileScript.spec.ts | 15 +++++++++++++++ .../__tests__/compileScript/defineProps.spec.ts | 2 +- packages/compiler-sfc/src/compileScript.ts | 12 ++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index eec434758e8..afe656b4a2c 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -1520,4 +1520,19 @@ describe('SFC genDefaultAs', () => { ) assertCode(content) }) + + test('binding type for edge cases', () => { + const { bindings } = compile( + `` + ) + expect(bindings).toStrictEqual({ + toRef: BindingTypes.SETUP_CONST, + props: BindingTypes.SETUP_REACTIVE_CONST, + foo: BindingTypes.SETUP_REF + }) + }) }) diff --git a/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts b/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts index f52307d41b3..fe5ef293ad4 100644 --- a/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts @@ -581,7 +581,7 @@ const props = defineProps({ foo: String }) `) expect(bindings).toStrictEqual({ - bar: BindingTypes.SETUP_MAYBE_REF, + bar: BindingTypes.SETUP_REF, computed: BindingTypes.SETUP_CONST }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 2fa022ec2d7..1fbbb8f5c96 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1095,8 +1095,16 @@ function walkDeclaration( : BindingTypes.SETUP_CONST } else if (isConst) { if ( - isCallOf(init, userImportAliases['ref']) || - isCallOf(init, DEFINE_MODEL) + isCallOf( + init, + m => + m === userImportAliases['ref'] || + m === userImportAliases['computed'] || + m === userImportAliases['shallowRef'] || + m === userImportAliases['customRef'] || + m === userImportAliases['toRef'] || + m === DEFINE_MODEL + ) ) { bindingType = BindingTypes.SETUP_REF } else {