]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): require <template> or <script> in SFC (#6781)
author花果山大圣 <316783812@qq.com>
Tue, 8 Nov 2022 03:34:39 +0000 (11:34 +0800)
committerGitHub <noreply@github.com>
Tue, 8 Nov 2022 03:34:39 +0000 (22:34 -0500)
fix #6676

packages/compiler-sfc/__tests__/parse.spec.ts
packages/compiler-sfc/src/parse.ts

index 9db9bb7f3e6e57151b24b7d188e35457fad0f80c..5f1db5e24995d295a7bea2e2fa2d7c4a41ba2246 100644 (file)
@@ -268,7 +268,9 @@ h1 { color: red }
   })
 
   test('treat custom blocks as raw text', () => {
-    const { errors, descriptor } = parse(`<foo> <-& </foo>`)
+    const { errors, descriptor } = parse(
+      `<template><input></template><foo> <-& </foo>`
+    )
     expect(errors.length).toBe(0)
     expect(descriptor.customBlocks[0].content).toBe(` <-& `)
   })
@@ -309,5 +311,13 @@ h1 { color: red }
         ).errors.length
       ).toBe(0)
     })
+
+    // # 6676
+    test('should throw error if no <template> or <script> is present', () => {
+      assertWarning(
+        parse(`import { ref } from 'vue'`).errors,
+        `At least one <template> or <script> is required in a single file component`
+      )
+    })
   })
 })
index 5c7d3b8e80e4fbb10e125ec08f5f962e29886dfd..79065fc667eb2d1821d058530b276d7e8c6bdac1 100644 (file)
@@ -149,7 +149,6 @@ export function parse(
       errors.push(e)
     }
   })
-
   ast.children.forEach(node => {
     if (node.type !== NodeTypes.ELEMENT) {
       return
@@ -218,7 +217,13 @@ export function parse(
         break
     }
   })
-
+  if (!descriptor.template && !descriptor.script && !descriptor.scriptSetup) {
+    errors.push(
+      new SyntaxError(
+        `At least one <template> or <script> is required in a single file component.`
+      )
+    )
+  }
   if (descriptor.scriptSetup) {
     if (descriptor.scriptSetup.src) {
       errors.push(