From 78657ae6103bb95f06bb481001bb954968df1270 Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Sat, 28 Sep 2024 10:44:28 +0800 Subject: [PATCH 1/3] fix(compiler-sfc): not skip `TSInstantiationExpression` --- packages/compiler-sfc/src/script/definePropsDestructure.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/compiler-sfc/src/script/definePropsDestructure.ts b/packages/compiler-sfc/src/script/definePropsDestructure.ts index f9a56e32e44..27b4d445bbe 100644 --- a/packages/compiler-sfc/src/script/definePropsDestructure.ts +++ b/packages/compiler-sfc/src/script/definePropsDestructure.ts @@ -10,6 +10,7 @@ import type { import { walk } from 'estree-walker' import { BindingTypes, + TS_NODE_TYPES, extractIdentifiers, isFunctionType, isInDestructureAssignment, @@ -240,10 +241,7 @@ export function transformDestructuredProps( if ( parent && parent.type.startsWith('TS') && - parent.type !== 'TSAsExpression' && - parent.type !== 'TSNonNullExpression' && - parent.type !== 'TSSatisfiesExpression' && - parent.type !== 'TSTypeAssertion' + !TS_NODE_TYPES.includes(parent.type) ) { return this.skip() } From b11b742af98e6f270343b03232307349bfbc1ae8 Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Sat, 28 Sep 2024 21:59:54 +0800 Subject: [PATCH 2/3] chore: update test --- .../definePropsDestructure.spec.ts.snap | 38 +++++++++++++++++++ .../definePropsDestructure.spec.ts | 15 ++++++++ 2 files changed, 53 insertions(+) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap index 12d5a67db7e..5e4991ca20a 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap @@ -1,5 +1,24 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`sfc reactive props destructure > TSInstantiationExpression 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +type Foo = (data: T) => void + +export default /*@__PURE__*/_defineComponent({ + props: { + value: { type: Function } + }, + setup(__props: any) { + + + const foo = __props.value<123> + +return () => {} +} + +})" +`; + exports[`sfc reactive props destructure > aliasing 1`] = ` "import { toDisplayString as _toDisplayString } from "vue" @@ -320,3 +339,22 @@ return { rest } }" `; + +exports[`sfc reactive props destructure > with TSInstantiationExpression 1`] = ` +"import { defineComponent as _defineComponent } from 'vue' +type Foo = (data: T) => void + +export default /*@__PURE__*/_defineComponent({ + props: { + value: { type: Function } + }, + setup(__props: any) { + + + const foo = __props.value<123> + +return () => {} +} + +})" +`; diff --git a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts index 106e469f188..50602eb59bc 100644 --- a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts @@ -198,6 +198,21 @@ describe('sfc reactive props destructure', () => { }`) }) + test('with TSInstantiationExpression', () => { + const { content } = compile( + ` + + `, + { isProd: true }, + ) + assertCode(content) + expect(content).toMatch(`const foo = __props.value<123>`) + }) + test('aliasing', () => { const { content, bindings } = compile(`