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 {
type CompilerOptions,
IRNodeTypes,
type RootIRNode,
compile as _compile,
generate,
parse,
transform,
transformElement,
transformVBind,
} from '../../src'
import { IRNodeTypes, transformElement, transformVBind } from '../../src'
import { makeCompile } from './_utils'
function compileWithVBind(
template: string,
options: CompilerOptions = {},
): {
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 }
}
const compileWithVBind = makeCompile({
nodeTransforms: [transformElement],
directiveTransforms: {
bind: transformVBind,
},
})
describe('compiler v-bind', () => {
test('basic', () => {

View File

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

View File

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

View File

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

View File

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