refactor: expose parse in compiler-dom, improve sfc parse error handling

This commit is contained in:
Evan You
2019-12-22 19:44:21 -05:00
parent 7d436ab59a
commit 90ddb7c260
33 changed files with 243 additions and 147 deletions

View File

@@ -1,5 +1,6 @@
import { parse } from '../src'
import { mockWarn } from '@vue/runtime-test'
import { baseParse, baseCompile } from '@vue/compiler-core'
describe('compiler:sfc', () => {
mockWarn()
@@ -7,13 +8,14 @@ describe('compiler:sfc', () => {
describe('source map', () => {
test('style block', () => {
const style = parse(`<style>\n.color {\n color: red;\n }\n</style>\n`)
.styles[0]
.descriptor.styles[0]
// TODO need to actually test this with SourceMapConsumer
expect(style.map).not.toBeUndefined()
})
test('script block', () => {
const script = parse(`<script>\nconsole.log(1)\n }\n</script>\n`).script
const script = parse(`<script>\nconsole.log(1)\n }\n</script>\n`)
.descriptor.script
// TODO need to actually test this with SourceMapConsumer
expect(script!.map).not.toBeUndefined()
})
@@ -30,12 +32,12 @@ export default {}
<style>
h1 { color: red }
</style>`
const padFalse = parse(content.trim(), { pad: false })
const padFalse = parse(content.trim(), { pad: false }).descriptor
expect(padFalse.template!.content).toBe('\n<div></div>\n')
expect(padFalse.script!.content).toBe('\nexport default {}\n')
expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
const padTrue = parse(content.trim(), { pad: true })
const padTrue = parse(content.trim(), { pad: true }).descriptor
expect(padTrue.script!.content).toBe(
Array(3 + 1).join('//\n') + '\nexport default {}\n'
)
@@ -43,7 +45,7 @@ h1 { color: red }
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
)
const padLine = parse(content.trim(), { pad: 'line' })
const padLine = parse(content.trim(), { pad: 'line' }).descriptor
expect(padLine.script!.content).toBe(
Array(3 + 1).join('//\n') + '\nexport default {}\n'
)
@@ -51,7 +53,7 @@ h1 { color: red }
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
)
const padSpace = parse(content.trim(), { pad: 'space' })
const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
expect(padSpace.script!.content).toBe(
`<template>\n<div></div>\n</template>\n<script>`.replace(/./g, ' ') +
'\nexport default {}\n'
@@ -65,13 +67,42 @@ h1 { color: red }
})
test('should ignore nodes with no content', () => {
expect(parse(`<template/>`).template).toBe(null)
expect(parse(`<script/>`).script).toBe(null)
expect(parse(`<style/>`).styles.length).toBe(0)
expect(parse(`<custom/>`).customBlocks.length).toBe(0)
expect(parse(`<template/>`).descriptor.template).toBe(null)
expect(parse(`<script/>`).descriptor.script).toBe(null)
expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
})
describe('error', () => {
test('nested templates', () => {
const content = `
<template v-if="ok">ok</template>
<div><div></div></div>
`
const sfc = parse(`<template>${content}</template>`).descriptor
expect(sfc.template!.content).toBe(content)
})
test('error tolerance', () => {
const { errors } = parse(`<template>`)
expect(errors.length).toBe(1)
})
test('should parse as DOM by default', () => {
const { errors } = parse(`<template><input></template>`)
expect(errors.length).toBe(0)
})
test('custom compiler', () => {
const { errors } = parse(`<template><input></template>`, {
compiler: {
parse: baseParse,
compile: baseCompile
}
})
expect(errors.length).toBe(1)
})
describe('warnings', () => {
test('should only allow single template element', () => {
parse(`<template><div/></template><template><div/></template>`)
expect(