test(compiler-vapor): refactor compile

This commit is contained in:
三咲智子 Kevin Deng 2024-01-27 22:13:34 +08:00
parent 164fd90df4
commit 2b5e8e4df6
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
6 changed files with 77 additions and 154 deletions

View File

@ -0,0 +1,22 @@
import { type CompilerOptions, generate, parse, transform } from '../../src'
export function makeCompile(options: CompilerOptions = {}) {
return (template: string, overrideOptions: CompilerOptions = {}) => {
const ast = parse(template, {
prefixIdentifiers: true,
...options,
...overrideOptions,
})
const ir = transform(ast, {
prefixIdentifiers: true,
...options,
...overrideOptions,
})
const { code, helpers, vaporHelpers } = generate(ir, {
prefixIdentifiers: true,
...options,
...overrideOptions,
})
return { ast, ir, code, helpers, vaporHelpers }
}
}

View File

@ -1,35 +1,13 @@
import { ErrorCodes, NodeTypes } from '@vue/compiler-dom' import { ErrorCodes, NodeTypes } from '@vue/compiler-dom'
import { import { IRNodeTypes, transformElement, transformVBind } from '../../src'
type CompilerOptions, import { makeCompile } from './_utils'
IRNodeTypes,
type RootIRNode,
compile as _compile,
generate,
parse,
transform,
transformElement,
transformVBind,
} from '../../src'
function compileWithVBind( const compileWithVBind = makeCompile({
template: string, nodeTransforms: [transformElement],
options: CompilerOptions = {}, directiveTransforms: {
): { bind: transformVBind,
ir: RootIRNode },
code: string })
} {
const ast = parse(template, { prefixIdentifiers: true, ...options })
const ir = transform(ast, {
nodeTransforms: [transformElement],
directiveTransforms: {
bind: transformVBind,
},
prefixIdentifiers: true,
...options,
})
const { code } = generate(ir, { prefixIdentifiers: true, ...options })
return { ir, code }
}
describe('compiler v-bind', () => { describe('compiler v-bind', () => {
test('basic', () => { test('basic', () => {

View File

@ -1,33 +1,13 @@
import { import { BindingTypes, DOMErrorCodes, NodeTypes } from '@vue/compiler-dom'
BindingTypes, import { IRNodeTypes, transformElement, transformVHtml } from '../../src'
DOMErrorCodes, import { makeCompile } from './_utils'
NodeTypes,
parse,
} from '@vue/compiler-dom'
import {
type CompilerOptions,
IRNodeTypes,
compile as _compile,
generate,
transform,
} from '../../src'
import { getBaseTransformPreset } from '../../src/compile'
function compileWithVHtml(template: string, options: CompilerOptions = {}) { const compileWithVHtml = makeCompile({
const ast = parse(template, { prefixIdentifiers: true, ...options }) nodeTransforms: [transformElement],
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true) directiveTransforms: {
const ir = transform(ast, { html: transformVHtml,
nodeTransforms, },
directiveTransforms, })
prefixIdentifiers: true,
...options,
})
const { code, helpers, vaporHelpers } = generate(ir, {
prefixIdentifiers: true,
...options,
})
return { ir, code, helpers, vaporHelpers }
}
describe('v-html', () => { describe('v-html', () => {
test('should convert v-html to innerHTML', () => { test('should convert v-html to innerHTML', () => {

View File

@ -1,31 +1,13 @@
import { BindingTypes, ErrorCodes, NodeTypes, parse } from '@vue/compiler-dom' import { BindingTypes, ErrorCodes, NodeTypes } from '@vue/compiler-dom'
import { import { IRNodeTypes, transformElement, transformVOn } from '../../src'
type CompilerOptions, import { makeCompile } from './_utils'
IRNodeTypes,
compile as _compile,
generate,
transform,
} from '../../src'
import { transformVOn } from '../../src/transforms/vOn' const compileWithVOn = makeCompile({
import { transformElement } from '../../src/transforms/transformElement' nodeTransforms: [transformElement],
directiveTransforms: {
function compileWithVOn(template: string, options: CompilerOptions = {}) { on: transformVOn,
const ast = parse(template, { prefixIdentifiers: true, ...options }) },
const ir = transform(ast, { })
nodeTransforms: [transformElement],
directiveTransforms: {
on: transformVOn,
},
prefixIdentifiers: true,
...options,
})
const { code, helpers, vaporHelpers } = generate(ir, {
prefixIdentifiers: true,
...options,
})
return { ir, code, helpers, vaporHelpers }
}
describe('v-on', () => { describe('v-on', () => {
test('simple expression', () => { test('simple expression', () => {

View File

@ -1,28 +1,13 @@
import { BindingTypes, NodeTypes, parse } from '@vue/compiler-dom' import { BindingTypes, NodeTypes } from '@vue/compiler-dom'
import { import { IRNodeTypes } from '../../src'
type CompilerOptions,
IRNodeTypes,
compile as _compile,
generate as generate,
transform,
} from '../../src'
import { getBaseTransformPreset } from '../../src/compile' import { getBaseTransformPreset } from '../../src/compile'
import { makeCompile } from './_utils'
function compileWithOnce(template: string, options: CompilerOptions = {}) { const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true)
const ast = parse(template, { prefixIdentifiers: true, ...options }) const compileWithOnce = makeCompile({
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true) nodeTransforms,
const ir = transform(ast, { directiveTransforms,
nodeTransforms, })
directiveTransforms,
prefixIdentifiers: true,
...options,
})
const { code, helpers, vaporHelpers } = generate(ir, {
prefixIdentifiers: true,
...options,
})
return { ir, code, helpers, vaporHelpers }
}
describe('compiler: v-once', () => { describe('compiler: v-once', () => {
test('basic', () => { test('basic', () => {
@ -38,9 +23,10 @@ describe('compiler: v-once', () => {
}, },
}, },
) )
expect(helpers.size).toBe(0)
expect(ir.effect).toEqual([])
expect(code).toMatchSnapshot()
expect(helpers).lengthOf(0)
expect(ir.effect).lengthOf(0)
expect(ir.operation).toMatchObject([ expect(ir.operation).toMatchObject([
{ {
id: 1, id: 1,
@ -80,16 +66,14 @@ describe('compiler: v-once', () => {
parent: 3, parent: 3,
}, },
]) ])
expect(code).toMatchSnapshot()
}) })
test('as root node', () => { test('as root node', () => {
const { ir, code, helpers } = compileWithOnce(`<div :id="foo" v-once />`) const { ir, code, helpers } = compileWithOnce(`<div :id="foo" v-once />`)
expect(helpers.size).toBe(0) expect(code).toMatchSnapshot()
expect(ir.effect).toEqual([]) expect(helpers).lengthOf(0)
expect(ir.effect).lengthOf(0)
expect(ir.operation).toMatchObject([ expect(ir.operation).toMatchObject([
{ {
type: IRNodeTypes.SET_PROP, type: IRNodeTypes.SET_PROP,
@ -106,8 +90,6 @@ describe('compiler: v-once', () => {
}, },
}, },
]) ])
expect(code).toMatchSnapshot()
expect(code).not.contains('effect') expect(code).not.contains('effect')
}) })
@ -115,9 +97,10 @@ describe('compiler: v-once', () => {
const { ir, code, helpers } = compileWithOnce( const { ir, code, helpers } = compileWithOnce(
`<div><div :id="foo" v-once /></div>`, `<div><div :id="foo" v-once /></div>`,
) )
expect(helpers.size).toBe(0)
expect(ir.effect).toEqual([])
expect(code).toMatchSnapshot()
expect(helpers).lengthOf(0)
expect(ir.effect).lengthOf(0)
expect(ir.operation).toMatchObject([ expect(ir.operation).toMatchObject([
{ {
type: IRNodeTypes.SET_PROP, type: IRNodeTypes.SET_PROP,
@ -135,8 +118,6 @@ describe('compiler: v-once', () => {
}, },
}, },
]) ])
expect(code).toMatchSnapshot()
}) })
test.todo('on component') test.todo('on component')
@ -146,11 +127,11 @@ describe('compiler: v-once', () => {
const { ir, code, helpers } = compileWithOnce( const { ir, code, helpers } = compileWithOnce(
`<div v-once><div v-once/></div>`, `<div v-once><div v-once/></div>`,
) )
expect(helpers.size).toBe(0)
expect(ir.effect).toMatchObject([])
expect(ir.operation).toMatchObject([])
expect(code).toMatchSnapshot() expect(code).toMatchSnapshot()
expect(helpers).lengthOf(0)
expect(ir.effect).lengthOf(0)
expect(ir.operation).lengthOf(0)
}) })
test.todo('with hoistStatic: true') test.todo('with hoistStatic: true')

View File

@ -1,33 +1,13 @@
import { import { BindingTypes, DOMErrorCodes, NodeTypes } from '@vue/compiler-dom'
BindingTypes, import { IRNodeTypes, transformElement, transformVText } from '../../src'
DOMErrorCodes, import { makeCompile } from './_utils'
NodeTypes,
parse,
} from '@vue/compiler-dom'
import {
type CompilerOptions,
IRNodeTypes,
compile as _compile,
generate,
transform,
} from '../../src'
import { getBaseTransformPreset } from '../../src/compile'
function compileWithVText(template: string, options: CompilerOptions = {}) { const compileWithVText = makeCompile({
const ast = parse(template, { prefixIdentifiers: true, ...options }) nodeTransforms: [transformElement],
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(true) directiveTransforms: {
const ir = transform(ast, { text: transformVText,
nodeTransforms, },
directiveTransforms, })
prefixIdentifiers: true,
...options,
})
const { code, helpers, vaporHelpers } = generate(ir, {
prefixIdentifiers: true,
...options,
})
return { ir, code, helpers, vaporHelpers }
}
describe('v-text', () => { describe('v-text', () => {
test('should convert v-text to textContent', () => { test('should convert v-text to textContent', () => {