Skip to content

fix(compiler-sfc): add error handling for defineModel() without variable assignment #13352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion packages/compiler-core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export enum ErrorCodes {
X_V_MODEL_ON_PROPS,
X_INVALID_EXPRESSION,
X_KEEP_ALIVE_INVALID_CHILDREN,

X_DEFINE_MODEL_NO_VARIABLE,
// generic errors
X_PREFIX_ID_NOT_SUPPORTED,
X_MODULE_MODE_NOT_SUPPORTED,
Expand Down Expand Up @@ -179,6 +179,7 @@ export const errorMessages: Record<ErrorCodes, string> = {
[ErrorCodes.X_INVALID_EXPRESSION]: `Error parsing JavaScript expression: `,
[ErrorCodes.X_KEEP_ALIVE_INVALID_CHILDREN]: `<KeepAlive> expects exactly one child component.`,
[ErrorCodes.X_VNODE_HOOKS]: `@vnode-* hooks in templates are no longer supported. Use the vue: prefix instead. For example, @vnode-mounted should be changed to @vue:mounted. @vnode-* hooks support has been removed in 3.4.`,
[ErrorCodes.X_DEFINE_MODEL_NO_VARIABLE]: `defineModel() must be assigned to a variable. For example: const model = defineModel()`,

// generic errors
[ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BindingTypes } from '@vue/compiler-core'
import { BindingTypes, ErrorCodes, errorMessages } from '@vue/compiler-core'
import { assertCode, compileSFCScript as compile } from '../utils'

describe('defineModel()', () => {
Expand Down Expand Up @@ -269,4 +269,14 @@ describe('defineModel()', () => {
modelValue: BindingTypes.SETUP_REF,
})
})

test('error when defineModel is not assigned to a variable', () => {
expect(() =>
compile(`
<script setup>
defineModel()
</script>
`),
).toThrow(errorMessages[ErrorCodes.X_DEFINE_MODEL_NO_VARIABLE])
})
})
11 changes: 10 additions & 1 deletion packages/compiler-sfc/src/script/defineModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import type { LVal, Node, TSType } from '@babel/types'
import type { ScriptCompileContext } from './context'
import { inferRuntimeType } from './resolveType'
import { UNKNOWN_TYPE, isCallOf, toRuntimeTypeString } from './utils'
import { BindingTypes, unwrapTSNode } from '@vue/compiler-dom'
import {
BindingTypes,
ErrorCodes,
errorMessages,
unwrapTSNode,
} from '@vue/compiler-dom'

export const DEFINE_MODEL = 'defineModel'

Expand All @@ -22,6 +27,10 @@ export function processDefineModel(
return false
}

if (!declId) {
ctx.error(errorMessages[ErrorCodes.X_DEFINE_MODEL_NO_VARIABLE], node)
}

ctx.hasDefineModelCall = true

const type =
Expand Down
Loading