fix(compiler-sfc): require <template> or <script> in SFC (#6781)

fix #6676
This commit is contained in:
花果山大圣 2022-11-08 11:34:39 +08:00 committed by GitHub
parent 9768949ce0
commit a0c7f271a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -268,7 +268,9 @@ h1 { color: red }
}) })
test('treat custom blocks as raw text', () => { 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(errors.length).toBe(0)
expect(descriptor.customBlocks[0].content).toBe(` <-& `) expect(descriptor.customBlocks[0].content).toBe(` <-& `)
}) })
@ -309,5 +311,13 @@ h1 { color: red }
).errors.length ).errors.length
).toBe(0) ).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`
)
})
}) })
}) })

View File

@ -149,7 +149,6 @@ export function parse(
errors.push(e) errors.push(e)
} }
}) })
ast.children.forEach(node => { ast.children.forEach(node => {
if (node.type !== NodeTypes.ELEMENT) { if (node.type !== NodeTypes.ELEMENT) {
return return
@ -218,7 +217,13 @@ export function parse(
break 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) {
if (descriptor.scriptSetup.src) { if (descriptor.scriptSetup.src) {
errors.push( errors.push(