diff --git a/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts b/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts index 6c55c131a7e..5790ec727ab 100644 --- a/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts +++ b/packages/compiler-vapor/__tests__/transforms/vModel.spec.ts @@ -8,6 +8,7 @@ import { } from '../../src' import { BindingTypes, DOMErrorCodes } from '@vue/compiler-dom' import { transformVOn } from '../../src/transforms/vOn' +import { parse } from '@babel/parser' const compileWithVModel = makeCompile({ nodeTransforms: [transformElement, transformChildren], @@ -575,4 +576,75 @@ describe('compiler: vModel transform', () => { expect(code).toMatchSnapshot() }, ) + + test.each([false, true])( + 'member expression w/ TS expressions (inline: %s)', + inline => { + const { code } = compileVapor( + ` + + + `, + { + prefixIdentifiers: true, + isTS: true, + expressionPlugins: ['typescript'], + inline, + bindingMetadata: { + form: BindingTypes.SETUP_REACTIVE_CONST, + state: BindingTypes.SETUP_REF, + }, + }, + ) + const form = inline ? 'form' : '_ctx.form' + const state = inline ? 'state.value' : '_ctx.state' + expect(code).toContain(`((${form}.a as string) = _value)`) + expect(code).toContain(`((${form}.b satisfies string) = _value)`) + expect(code).toContain(`(${form}!.c = _value)`) + expect(code).toContain(`(${state}!.d = _value)`) + expect(() => + parse(code, { + sourceType: 'module', + plugins: ['typescript'], + allowReturnOutsideFunction: inline, + }), + ).not.toThrow() + }, + ) + + test.each([BindingTypes.SETUP_LET, BindingTypes.SETUP_MAYBE_REF])( + 'member expression w/ TS expressions and %s bindings', + binding => { + const { code } = compileVapor( + ` + + `, + { + prefixIdentifiers: true, + isTS: true, + expressionPlugins: ['typescript'], + inline: true, + bindingMetadata: { + form: binding, + key: BindingTypes.SETUP_REF, + }, + }, + ) + + expect(code).toContain(`(_unref(form)!.name = _value)`) + expect(code).toContain(`((_unref(form)![key.value] as string) = _value)`) + expect(code).toContain( + `_isRef(form) ? (form.value = _value) : ${ + binding === BindingTypes.SETUP_LET ? '(form = _value)' : 'null' + }`, + ) + expect(() => + parse(code, { + sourceType: 'module', + plugins: ['typescript'], + allowReturnOutsideFunction: true, + }), + ).not.toThrow() + }, + ) }) diff --git a/packages/compiler-vapor/src/generators/expression.ts b/packages/compiler-vapor/src/generators/expression.ts index 02180873fdc..751e9742bc6 100644 --- a/packages/compiler-vapor/src/generators/expression.ts +++ b/packages/compiler-vapor/src/generators/expression.ts @@ -16,6 +16,7 @@ import { isFunctionType, isInDestructureAssignment, isStaticProperty, + unwrapTSNode, walkIdentifiers, } from '@vue/compiler-dom' import type { @@ -64,6 +65,11 @@ export function genExpression( return genIdentifier(content, context, loc, assignment) } + const target = assignment ? unwrapTSNode(ast!) : undefined + const isMemberTarget = + target?.type === 'MemberExpression' || + target?.type === 'OptionalMemberExpression' + const ids: Identifier[] = [] const parentStackMap = new Map() const parentStack: Node[] = [] @@ -82,7 +88,6 @@ export function genExpression( parentStack, ) - let hasMemberExpression = false if (ids.length) { const [frag, push] = buildCodeFragment() let lastEnd = 0 @@ -115,11 +120,6 @@ export function genExpression( const leadingText = content.slice(lastEnd, start) if (leadingText.length) push([leadingText, NewlineType.Unknown]) - hasMemberExpression ||= - parent && - (parent.type === 'MemberExpression' || - parent.type === 'OptionalMemberExpression') - push( ...genIdentifier( asParams ? id.name : source, @@ -129,7 +129,7 @@ export function genExpression( end: advancePositionWithClone(node.loc.start, source, end), source, }, - hasMemberExpression ? undefined : assignment, + isMemberTarget ? undefined : assignment, id, parent, parentStack, @@ -143,7 +143,12 @@ export function genExpression( if (lastEnd < content.length) { push([content.slice(lastEnd), NewlineType.Unknown]) } - if (assignment && hasMemberExpression) { + if (assignment && isMemberTarget) { + // `a.b as T = v` is not valid TS, `(a.b as T) = v` is + if (TS_NODE_TYPES.includes(ast!.type)) { + frag.unshift('(') + push(')') + } push(` = ${assignment}`) } return frag