From 7e47ae17f9326a4886b4b13700f1f6ffebe74d7f Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Sun, 28 Jan 2024 02:11:05 +0800 Subject: [PATCH] feat(compiler-vapor): add error check and simplify for v-if --- packages/compiler-vapor/src/transforms/vIf.ts | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/packages/compiler-vapor/src/transforms/vIf.ts b/packages/compiler-vapor/src/transforms/vIf.ts index c8473baa52..a3c69307e9 100644 --- a/packages/compiler-vapor/src/transforms/vIf.ts +++ b/packages/compiler-vapor/src/transforms/vIf.ts @@ -1,9 +1,12 @@ import { ElementTypes, + ErrorCodes, NodeTypes, type RootNode, type TemplateChildNode, type TemplateNode, + createCompilerError, + createSimpleExpression, } from '@vue/compiler-dom' import { type TransformContext, @@ -12,7 +15,6 @@ import { import { type BlockFunctionIRNode, IRNodeTypes, - type IfIRNode, type VaporDirectiveNode, } from '../ir' import { extend } from '@vue/shared' @@ -27,24 +29,29 @@ export function processIf( dir: VaporDirectiveNode, context: TransformContext, ) { - // TODO refactor this - const parentContext = extend({}, context, { - currentScopeIR: context.block, - }) + if (dir.name !== 'else' && (!dir.exp || !dir.exp.content.trim())) { + const loc = dir.exp ? dir.exp.loc : node.loc + context.options.onError( + createCompilerError(ErrorCodes.X_V_IF_NO_EXPRESSION, dir.loc), + ) + dir.exp = createSimpleExpression(`true`, false, loc) + } if (dir.name === 'if') { const id = context.reference() context.dynamic.ghost = true const [branch, onExit] = createIfBranch(node, dir, context) - const operation: IfIRNode = { - type: IRNodeTypes.IF, - id, - loc: dir.loc, - condition: dir.exp!, - positive: branch, + + return () => { + onExit() + context.registerOperation({ + type: IRNodeTypes.IF, + id, + loc: dir.loc, + condition: dir.exp!, + positive: branch, + }) } - parentContext.registerOperation(operation) - return onExit } } -- 2.47.3