Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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(
`<input v-model="form.a as string" />
<Comp v-model="form.b satisfies string" />
<input v-model="form!.c" />
<input v-model="state!.d" />`,
{
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(
`<input v-model="form!.name" />
<Comp v-model="form![key] as string" />
<input v-model="form" />`,
{
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()
},
)
})
21 changes: 13 additions & 8 deletions packages/compiler-vapor/src/generators/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isFunctionType,
isInDestructureAssignment,
isStaticProperty,
unwrapTSNode,
walkIdentifiers,
} from '@vue/compiler-dom'
import type {
Expand Down Expand Up @@ -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<Identifier, Node[]>()
const parentStack: Node[] = []
Expand All @@ -82,7 +88,6 @@ export function genExpression(
parentStack,
)

let hasMemberExpression = false
if (ids.length) {
const [frag, push] = buildCodeFragment()
let lastEnd = 0
Expand Down Expand Up @@ -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,
Expand All @@ -129,7 +129,7 @@ export function genExpression(
end: advancePositionWithClone(node.loc.start, source, end),
source,
},
hasMemberExpression ? undefined : assignment,
isMemberTarget ? undefined : assignment,
id,
parent,
parentStack,
Expand All @@ -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
Expand Down
Loading