Przeglądaj źródła

fix(compiler-vapor): handle boolean as constant node (#13994)

zhiyuanzmj 5 miesięcy temu
rodzic
commit
c1f228913b

+ 2 - 2
packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

@@ -42,7 +42,7 @@ export function render(_ctx) {
         const n2 = _createComponentWithFallback(_component_Bar)
         _withVaporDirectives(n2, [[_directive_hello, void 0, void 0, { world: true }]])
         return n3
-      })
+      }, null, true)
       return n0
     })
   }, true)
@@ -230,7 +230,7 @@ export function render(_ctx) {
   const n1 = _createIf(() => (true), () => {
     const n3 = t0()
     return n3
-  })
+  }, null, true)
   _renderEffect(() => _setProp(n4, "disabled", _ctx.foo))
   return n6
 }"

+ 1 - 1
packages/compiler-vapor/__tests__/transforms/__snapshots__/transformTemplateRef.spec.ts.snap

@@ -58,7 +58,7 @@ export function render(_ctx) {
     const n2 = t0()
     _setTemplateRef(n2, "foo")
     return n2
-  })
+  }, null, true)
   return n0
 }"
 `;

+ 6 - 3
packages/compiler-vapor/__tests__/transforms/__snapshots__/vIf.spec.ts.snap

@@ -110,10 +110,13 @@ export function render(_ctx) {
   }, () => _createIf(() => (_ctx.orNot), () => {
     const n4 = t1()
     return n4
-  }, () => {
-    const n7 = t2()
+  }, () => _createIf(() => (false), () => {
+    const n7 = t1()
     return n7
-  }))
+  }, () => {
+    const n10 = t2()
+    return n10
+  }, true)))
   return n0
 }"
 `;

+ 1 - 1
packages/compiler-vapor/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap

@@ -356,7 +356,7 @@ export function render(_ctx) {
   }, () => {
     const n5 = _createComponentWithFallback(_component_Bar)
     return n5
-  })
+  }, true)
   return n6
 }"
 `;

+ 7 - 4
packages/compiler-vapor/__tests__/transforms/vIf.spec.ts

@@ -182,7 +182,7 @@ describe('compiler: v-if', () => {
 
   test('v-if + v-else-if + v-else', () => {
     const { code, ir } = compileWithVIf(
-      `<div v-if="ok"/><p v-else-if="orNot"/><template v-else>fine</template>`,
+      `<div v-if="ok"/><p v-else-if="orNot"/><p v-else-if="false"/><template v-else>fine</template>`,
     )
     expect(code).matchSnapshot()
     expect(ir.template).toEqual(['<div></div>', '<p></p>', 'fine'])
@@ -206,9 +206,12 @@ describe('compiler: v-if', () => {
           },
         },
         negative: {
-          type: IRNodeTypes.BLOCK,
-          dynamic: {
-            children: [{ template: 2 }],
+          type: IRNodeTypes.IF,
+          negative: {
+            type: IRNodeTypes.BLOCK,
+            dynamic: {
+              children: [{ template: 2 }],
+            },
           },
         },
       },

+ 3 - 1
packages/compiler-vapor/src/transforms/vIf.ts

@@ -119,7 +119,9 @@ export function processIf(
         id: -1,
         condition: dir.exp!,
         positive: branch,
-        once: context.inVOnce,
+        once:
+          context.inVOnce ||
+          isStaticExpression(dir.exp!, context.options.bindingMetadata),
       }
     }
 

+ 6 - 0
packages/compiler-vapor/src/utils.ts

@@ -55,6 +55,12 @@ export function isStaticExpression(
   if (node.ast) {
     return isConstantNode(node.ast, bindings)
   } else if (node.ast === null) {
+    if (
+      !node.isStatic &&
+      (node.content === 'true' || node.content === 'false')
+    ) {
+      return true
+    }
     const type = bindings[node.content]
     return type === BindingTypes.LITERAL_CONST
   }