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
})
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(`<template><div/></template><template><div/></template>`)
- expect(
+ assertWarning(
+ parse(`<template><div/></template><template><div/></template>`).errors,
`Single file component can contain only one <template> element`
- ).toHaveBeenWarned()
+ )
})
test('should only allow single script element', () => {
- parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
- expect(
+ assertWarning(
+ parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
+ .errors,
`Single file component can contain only one <script> element`
- ).toHaveBeenWarned()
+ )
})
test('should only allow single script setup element', () => {
- parse(
- `<script setup>console.log(1)</script><script setup>console.log(1)</script>`
- )
- expect(
+ assertWarning(
+ parse(
+ `<script setup>console.log(1)</script><script setup>console.log(1)</script>`
+ ).errors,
`Single file component can contain only one <script setup> element`
- ).toHaveBeenWarned()
+ )
})
test('should not warn script & script setup', () => {
- parse(
- `<script setup>console.log(1)</script><script>console.log(1)</script>`
- )
expect(
- `Single file component can contain only one`
- ).not.toHaveBeenWarned()
+ parse(
+ `<script setup>console.log(1)</script><script>console.log(1)</script>`
+ ).errors.length
+ ).toBe(0)
})
})
})
} 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'
export interface SFCParseResult {
descriptor: SFCDescriptor
- errors: CompilerError[]
+ errors: (CompilerError | SyntaxError)[]
}
const SFC_CACHE_MAX_SIZE = 500
customBlocks: []
}
- const errors: CompilerError[] = []
+ const errors: (CompilerError | SyntaxError)[] = []
const ast = compiler.parse(source, {
// there are no components at SFC parsing level
isNativeTag: () => true,
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(
+ `<script setup> cannot be used with the "src" attribute since ` +
+ `its syntax will be ambiguous outside of the component.`
+ )
+ )
+ break
+ }
descriptor.scriptSetup = block
break
}
descriptor.script = block
break
}
- warnDuplicateBlock(source, filename, node, isSetup)
+ errors.push(createDuplicateBlockError(node, isSetup))
break
case 'style':
descriptor.styles.push(createBlock(node, source, pad) as SFCStyleBlock)
return result
}
-function warnDuplicateBlock(
- source: string,
- filename: string,
+function createDuplicateBlockError(
node: ElementNode,
isScriptSetup = false
-) {
- const codeFrame = generateCodeFrame(
- source,
- node.loc.start.offset,
- node.loc.end.offset
- )
- const location = `${filename}:${node.loc.start.line}:${node.loc.start.column}`
- console.warn(
+): CompilerError {
+ const err = new SyntaxError(
`Single file component can contain only one <${node.tag}${
isScriptSetup ? ` setup` : ``
- }> element (${location}):\n\n${codeFrame}`
- )
+ }> element`
+ ) as CompilerError
+ err.loc = node.loc
+ return err
}
function createBlock(