diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index 17ee4724f4d..8a8d2394f91 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -298,6 +298,26 @@ describe('compiler: element transform', () => { assert(`Portal`) }) + test('should error on element with no target', () => { + const onError = jest.fn() + parseWithElementTransform(``, { + onError + }) + + expect(onError.mock.calls[0][0]).toMatchObject({ + code: ErrorCodes.X_PORTAL_NO_TARGET + }) + }) + + test('should not error on element with bound target', () => { + const onError = jest.fn() + parseWithElementTransform(``, { + onError + }) + + expect(onError).not.toHaveBeenCalled() + }) + test('should handle ', () => { function assert(tag: string, content: string, hasFallback?: boolean) { const { root, node } = parseWithElementTransform( diff --git a/packages/compiler-core/src/errors.ts b/packages/compiler-core/src/errors.ts index 3a9e3469ee6..ea964ba0791 100644 --- a/packages/compiler-core/src/errors.ts +++ b/packages/compiler-core/src/errors.ts @@ -83,6 +83,7 @@ export const enum ErrorCodes { X_V_MODEL_MALFORMED_EXPRESSION, X_V_MODEL_ON_SCOPE_VARIABLE, X_INVALID_EXPRESSION, + X_PORTAL_NO_TARGET, // generic errors X_PREFIX_ID_NOT_SUPPORTED, @@ -175,6 +176,7 @@ export const errorMessages: { [code: number]: string } = { [ErrorCodes.X_V_MODEL_MALFORMED_EXPRESSION]: `v-model value must be a valid JavaScript member expression.`, [ErrorCodes.X_V_MODEL_ON_SCOPE_VARIABLE]: `v-model cannot be used on v-for or v-slot scope variables because they are not writable.`, [ErrorCodes.X_INVALID_EXPRESSION]: `Invalid JavaScript expression.`, + [ErrorCodes.X_PORTAL_NO_TARGET]: `Portal requires a "target" prop.`, // generic errors [ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED]: `"prefixIdentifiers" option is not supported in this build of compiler.`, diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 2d09bf5681b..f0ae7fc1706 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -125,6 +125,13 @@ export const transformElement: NodeTransform = (node, context) => { args.push(propsBuildResult.props) } } + + if (node.tagType === ElementTypes.PORTAL && !findProp(node, 'target')) { + context.onError( + createCompilerError(ErrorCodes.X_PORTAL_NO_TARGET, node.loc) + ) + } + // children const hasChildren = node.children.length > 0 if (hasChildren) {