diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts
index 1378e64a..d81ce60c 100644
--- a/packages/compiler-sfc/__tests__/parse.spec.ts
+++ b/packages/compiler-sfc/__tests__/parse.spec.ts
@@ -1,11 +1,8 @@
import { parse } from '../src'
-import { mockWarn } from '@vue/shared'
import { baseParse, baseCompile } from '@vue/compiler-core'
import { SourceMapConsumer } from 'source-map'
describe('compiler:sfc', () => {
- mockWarn()
-
describe('source map', () => {
test('style block', () => {
// Padding determines how many blank lines will there be before the style block
@@ -143,36 +140,40 @@ h1 { color: red }
})
describe('warnings', () => {
+ function assertWarning(errors: Error[], msg: string) {
+ expect(errors.some(e => e.message.match(msg))).toBe(true)
+ }
+
test('should only allow single template element', () => {
- parse(``)
- expect(
+ assertWarning(
+ parse(``).errors,
`Single file component can contain only one element`
- ).toHaveBeenWarned()
+ )
})
test('should only allow single script element', () => {
- parse(``)
- expect(
+ assertWarning(
+ parse(``)
+ .errors,
`Single file component can contain only one `
- )
- expect(
+ assertWarning(
+ parse(
+ ``
+ ).errors,
`Single file component can contain only one `
- )
expect(
- `Single file component can contain only one`
- ).not.toHaveBeenWarned()
+ parse(
+ ``
+ ).errors.length
+ ).toBe(0)
})
})
})
diff --git a/packages/compiler-sfc/src/parse.ts b/packages/compiler-sfc/src/parse.ts
index bcbfabb8..5cfbeb10 100644
--- a/packages/compiler-sfc/src/parse.ts
+++ b/packages/compiler-sfc/src/parse.ts
@@ -8,7 +8,6 @@ import {
} from '@vue/compiler-core'
import * as CompilerDOM from '@vue/compiler-dom'
import { RawSourceMap, SourceMapGenerator } from 'source-map'
-import { generateCodeFrame } from '@vue/shared'
import { TemplateCompiler } from './compileTemplate'
import { compileScript, SFCScriptCompileOptions } from './compileScript'
@@ -61,7 +60,7 @@ export interface SFCDescriptor {
export interface SFCParseResult {
descriptor: SFCDescriptor
- errors: CompilerError[]
+ errors: (CompilerError | SyntaxError)[]
}
const SFC_CACHE_MAX_SIZE = 500
@@ -102,7 +101,7 @@ export function parse(
customBlocks: []
}
- const errors: CompilerError[] = []
+ const errors: (CompilerError | SyntaxError)[] = []
const ast = compiler.parse(source, {
// there are no components at SFC parsing level
isNativeTag: () => true,
@@ -148,13 +147,22 @@ export function parse(
false
) as SFCTemplateBlock
} else {
- warnDuplicateBlock(source, filename, node)
+ errors.push(createDuplicateBlockError(node))
}
break
case 'script':
const block = createBlock(node, source, pad) as SFCScriptBlock
const isSetup = !!block.attrs.setup
if (isSetup && !descriptor.scriptSetup) {
+ if (block.src) {
+ errors.push(
+ new SyntaxError(
+ `