Explorar o código

refactor: throw an error using with <template>

三咲智子 Kevin Deng %!s(int64=2) %!d(string=hai) anos
pai
achega
cee3fd3291

+ 18 - 0
packages/compiler-sfc/__tests__/compileScript/defineRender.spec.ts

@@ -43,4 +43,22 @@ describe('defineRender()', () => {
     expect(content).toMatch(`return renderFn`)
     expect(content).not.toMatch('defineRender')
   })
+
+  describe('errors', () => {
+    test('w/ <template>', () => {
+      expect(() =>
+        compile(
+          `
+      <script setup lang="tsx">
+      defineRender(<div />)
+      </script>
+      <template>
+        <span>hello</span>
+      </template>
+    `,
+          { defineRender: true }
+        )
+      ).toThrow(`defineRender() cannot be used with <template>.`)
+    })
+  })
 })

+ 4 - 1
packages/compiler-sfc/src/compileScript.ts

@@ -808,7 +808,10 @@ export function compileScript(
   let returned = ''
   if (ctx.renderFunction) {
     if (sfc.template) {
-      warnOnce(`<template> is ignored when using ${DEFINE_RENDER}().`)
+      ctx.error(
+        `${DEFINE_RENDER}() cannot be used with <template>.`,
+        ctx.renderFunction
+      )
     }
     if (ctx.renderFunction.type === 'JSXElement') {
       returned = '() => '