vue3-yuanma/packages/compiler-core/__tests__/codegen.spec.ts

584 lines
15 KiB
TypeScript
Raw Normal View History

2019-09-25 03:49:02 +08:00
import {
locStub,
2019-09-25 03:49:02 +08:00
generate,
NodeTypes,
RootNode,
createSimpleExpression,
2019-09-25 03:49:02 +08:00
Namespaces,
ElementTypes,
createObjectExpression,
createObjectProperty,
2019-09-25 04:35:01 +08:00
createArrayExpression,
createCompoundExpression,
createInterpolation,
createSequenceExpression,
createCallExpression,
createConditionalExpression
2019-09-25 03:49:02 +08:00
} from '../src'
import {
CREATE_VNODE,
COMMENT,
TO_STRING,
RENDER_LIST
} from '../src/runtimeConstants'
import { createElementWithCodegen } from './testUtils'
2019-09-25 03:49:02 +08:00
function createRoot(options: Partial<RootNode> = {}): RootNode {
return {
type: NodeTypes.ROOT,
children: [],
imports: [],
statements: [],
hoists: [],
loc: locStub,
2019-09-25 03:49:02 +08:00
...options
}
}
describe('compiler: codegen', () => {
2019-09-25 03:49:02 +08:00
test('module mode preamble', () => {
const root = createRoot({
imports: [`helperOne`, `helperTwo`]
})
const { code } = generate(root, { mode: 'module' })
2019-09-26 10:29:37 +08:00
expect(code).toMatch(`import { helperOne, helperTwo } from "vue"`)
2019-09-25 03:49:02 +08:00
expect(code).toMatchSnapshot()
})
test('function mode preamble', () => {
const root = createRoot({
imports: [`helperOne`, `helperTwo`]
})
const { code } = generate(root, { mode: 'function' })
expect(code).toMatch(`const _Vue = Vue`)
expect(code).toMatch(
`const { helperOne: _helperOne, helperTwo: _helperTwo } = _Vue`
)
expect(code).toMatchSnapshot()
})
test('function mode preamble w/ prefixIdentifiers: true', () => {
const root = createRoot({
imports: [`helperOne`, `helperTwo`]
})
const { code } = generate(root, {
mode: 'function',
prefixIdentifiers: true
})
expect(code).not.toMatch(`const _Vue = Vue`)
expect(code).toMatch(`const { helperOne, helperTwo } = Vue`)
2019-09-25 03:49:02 +08:00
expect(code).toMatchSnapshot()
})
test('statements', () => {
2019-09-25 03:49:02 +08:00
const root = createRoot({
statements: [`const a = 1`, `const b = 2`]
})
const { code } = generate(root, { mode: 'function' })
expect(code).toMatch(`const a = 1\n`)
expect(code).toMatch(`const b = 2\n`)
expect(code).toMatchSnapshot()
})
test('hoists', () => {
const root = createRoot({
hoists: [
createSimpleExpression(`hello`, false, locStub),
createObjectExpression(
[
createObjectProperty(
createSimpleExpression(`id`, true, locStub),
createSimpleExpression(`foo`, true, locStub)
)
],
locStub
)
]
})
const { code } = generate(root)
expect(code).toMatch(`const _hoisted_1 = hello`)
expect(code).toMatch(`const _hoisted_2 = { id: "foo" }`)
expect(code).toMatchSnapshot()
})
2019-09-25 03:49:02 +08:00
test('prefixIdentifiers: true should inject _ctx statement', () => {
const { code } = generate(createRoot(), { prefixIdentifiers: true })
expect(code).toMatch(`const _ctx = this\n`)
expect(code).toMatchSnapshot()
})
test('static text', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.TEXT,
content: 'hello',
isEmpty: false,
loc: locStub
2019-09-25 03:49:02 +08:00
}
]
})
)
expect(code).toMatch(`return "hello"`)
expect(code).toMatchSnapshot()
})
test('interpolation', () => {
const { code } = generate(
createRoot({
children: [createInterpolation(`hello`, locStub)]
2019-09-25 03:49:02 +08:00
})
)
expect(code).toMatch(`return _${TO_STRING}(hello)`)
2019-09-25 03:49:02 +08:00
expect(code).toMatchSnapshot()
})
test('comment', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.COMMENT,
content: 'foo',
loc: locStub
2019-09-25 03:49:02 +08:00
}
]
})
)
expect(code).toMatch(`return _${CREATE_VNODE}(_${COMMENT}, 0, "foo")`)
2019-09-25 03:49:02 +08:00
expect(code).toMatchSnapshot()
})
test('text + comment + interpolation', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.TEXT,
content: 'foo',
isEmpty: false,
loc: locStub
2019-09-25 03:49:02 +08:00
},
createInterpolation(`hello`, locStub),
2019-09-25 03:49:02 +08:00
{
type: NodeTypes.COMMENT,
content: 'foo',
loc: locStub
2019-09-25 03:49:02 +08:00
}
]
})
)
expect(code).toMatch(`
return [
"foo",
_${TO_STRING}(hello),
_${CREATE_VNODE}(_${COMMENT}, 0, "foo")
2019-09-25 03:49:02 +08:00
]`)
expect(code).toMatchSnapshot()
})
test('text + comment + interpolation w/ prefixIdentifiers: true', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.TEXT,
content: 'foo',
isEmpty: false,
loc: locStub
},
createInterpolation(`hello`, locStub),
{
type: NodeTypes.COMMENT,
content: 'foo',
loc: locStub
}
]
}),
{
prefixIdentifiers: true
}
)
expect(code).toMatch(`
return [
"foo",
${TO_STRING}(hello),
${CREATE_VNODE}(${COMMENT}, 0, "foo")
]`)
expect(code).toMatchSnapshot()
})
2019-09-25 04:35:01 +08:00
test('compound expression', () => {
const { code } = generate(
createRoot({
children: [
createCompoundExpression([
`_ctx.`,
createSimpleExpression(`foo`, false, locStub),
` + `,
{
type: NodeTypes.INTERPOLATION,
loc: locStub,
content: createSimpleExpression(`bar`, false, locStub)
}
])
2019-09-25 04:35:01 +08:00
]
})
)
expect(code).toMatch(`return _ctx.foo + _${TO_STRING}(bar)`)
2019-09-25 04:35:01 +08:00
expect(code).toMatchSnapshot()
})
2019-09-25 03:49:02 +08:00
test('ifNode', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.IF,
loc: locStub,
branches: [],
codegenNode: createSequenceExpression([
createSimpleExpression('foo', false),
createSimpleExpression('bar', false)
])
2019-09-25 03:49:02 +08:00
}
]
})
)
expect(code).toMatch(`return (foo, bar)`)
2019-09-25 04:35:01 +08:00
expect(code).toMatchSnapshot()
})
2019-09-25 03:49:02 +08:00
test('forNode', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression(`list`, false, locStub),
valueAlias: createSimpleExpression(`v`, false, locStub),
keyAlias: createSimpleExpression(`k`, false, locStub),
objectIndexAlias: createSimpleExpression(`i`, false, locStub),
children: [createInterpolation(`v`, locStub)]
2019-09-25 03:49:02 +08:00
}
]
})
)
expect(code).toMatch(
`return _${RENDER_LIST}(list, (v, k, i) => {
return _${TO_STRING}(v)
})`
)
expect(code).toMatchSnapshot()
})
test('forNode w/ prefixIdentifiers: true', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression(`list`, false, locStub),
valueAlias: createSimpleExpression(`v`, false, locStub),
keyAlias: createSimpleExpression(`k`, false, locStub),
objectIndexAlias: createSimpleExpression(`i`, false, locStub),
children: [createInterpolation(`v`, locStub)]
}
]
}),
{
prefixIdentifiers: true
}
)
expect(code).toMatch(
`return ${RENDER_LIST}(list, (v, k, i) => {
return ${TO_STRING}(v)
})`
)
2019-09-25 03:49:02 +08:00
expect(code).toMatchSnapshot()
})
2019-09-25 04:35:01 +08:00
test('forNode w/ skipped value alias', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression(`list`, false, locStub),
2019-09-25 04:35:01 +08:00
valueAlias: undefined,
keyAlias: createSimpleExpression(`k`, false, locStub),
objectIndexAlias: createSimpleExpression(`i`, false, locStub),
children: [createInterpolation(`v`, locStub)]
2019-09-25 04:35:01 +08:00
}
]
})
)
expect(code).toMatch(
`return _${RENDER_LIST}(list, (__value, k, i) => {
return _${TO_STRING}(v)
})`
)
2019-09-25 04:35:01 +08:00
expect(code).toMatchSnapshot()
})
test('forNode w/ skipped key alias', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression(`list`, false, locStub),
valueAlias: createSimpleExpression(`v`, false, locStub),
2019-09-25 04:35:01 +08:00
keyAlias: undefined,
objectIndexAlias: createSimpleExpression(`i`, false, locStub),
children: [createInterpolation(`v`, locStub)]
2019-09-25 04:35:01 +08:00
}
]
})
)
expect(code).toMatch(
`return _${RENDER_LIST}(list, (v, __key, i) => {
return _${TO_STRING}(v)
})`
)
2019-09-25 04:35:01 +08:00
expect(code).toMatchSnapshot()
})
test('forNode w/ skipped value and key aliases', () => {
2019-09-25 03:49:02 +08:00
const { code } = generate(
createRoot({
children: [
{
2019-09-25 04:35:01 +08:00
type: NodeTypes.FOR,
loc: locStub,
source: createSimpleExpression(`list`, false, locStub),
2019-09-25 04:35:01 +08:00
valueAlias: undefined,
keyAlias: undefined,
objectIndexAlias: createSimpleExpression(`i`, false, locStub),
children: [createInterpolation(`v`, locStub)]
2019-09-25 04:35:01 +08:00
}
]
})
)
expect(code).toMatch(
`return _${RENDER_LIST}(list, (__value, __key, i) => {
return _${TO_STRING}(v)
})`
)
2019-09-25 04:35:01 +08:00
expect(code).toMatchSnapshot()
})
2019-09-29 01:12:41 +08:00
test('SlotFunctionExpression', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.ELEMENT,
tagType: ElementTypes.COMPONENT,
ns: Namespaces.HTML,
isSelfClosing: false,
tag: `Comp`,
loc: locStub,
2019-09-29 01:12:41 +08:00
props: [],
children: [],
codegenNode: {
type: NodeTypes.JS_CALL_EXPRESSION,
loc: locStub,
2019-09-29 01:12:41 +08:00
callee: `_${CREATE_VNODE}`,
arguments: [
`Comp`,
`0`,
{
type: NodeTypes.JS_OBJECT_EXPRESSION,
loc: locStub,
2019-09-29 01:12:41 +08:00
properties: [
{
type: NodeTypes.JS_PROPERTY,
loc: locStub,
2019-09-29 01:12:41 +08:00
key: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: true,
content: `default`,
loc: locStub
2019-09-29 01:12:41 +08:00
},
value: {
type: NodeTypes.JS_SLOT_FUNCTION,
loc: locStub,
2019-09-29 01:12:41 +08:00
params: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
content: `{ foo }`,
loc: locStub
2019-09-29 01:12:41 +08:00
},
returns: [
{
type: NodeTypes.INTERPOLATION,
loc: locStub,
2019-09-29 01:12:41 +08:00
content: {
type: NodeTypes.SIMPLE_EXPRESSION,
isStatic: false,
content: `foo`,
loc: locStub
2019-09-29 01:12:41 +08:00
}
}
]
}
}
]
}
]
}
}
]
})
)
expect(code).toMatch(
`return _createVNode(Comp, 0, {
default: ({ foo }) => [
_toString(foo)
]
})`
)
expect(code).toMatchSnapshot()
})
2019-09-25 04:35:01 +08:00
test('callExpression + objectExpression + arrayExpression', () => {
const { code } = generate(
createRoot({
children: [
createElementWithCodegen([
// string
`"div"`,
// ObjectExpression
createObjectExpression(
[
createObjectProperty(
createSimpleExpression(`id`, true, locStub),
createSimpleExpression(`foo`, true, locStub)
2019-09-25 04:35:01 +08:00
),
createObjectProperty(
createSimpleExpression(`prop`, false, locStub),
createSimpleExpression(`bar`, false, locStub)
2019-09-25 04:35:01 +08:00
),
// compound expression as computed key
createObjectProperty(
{
type: NodeTypes.COMPOUND_EXPRESSION,
loc: locStub,
2019-09-25 04:35:01 +08:00
children: [
`foo + `,
createSimpleExpression(`bar`, false, locStub)
2019-09-25 04:35:01 +08:00
]
},
createSimpleExpression(`bar`, false, locStub)
2019-09-25 04:35:01 +08:00
)
],
locStub
2019-09-25 04:35:01 +08:00
),
// ChildNode[]
[
createElementWithCodegen([
`"p"`,
2019-09-25 03:49:02 +08:00
createObjectExpression(
[
createObjectProperty(
2019-09-25 04:35:01 +08:00
// should quote the key!
createSimpleExpression(`some-key`, true, locStub),
createSimpleExpression(`foo`, true, locStub)
2019-09-25 03:49:02 +08:00
)
],
locStub
2019-09-25 03:49:02 +08:00
)
2019-09-25 04:35:01 +08:00
])
],
// ArrayExpression
createArrayExpression(
[
'foo',
{
type: NodeTypes.JS_CALL_EXPRESSION,
loc: locStub,
2019-09-25 04:35:01 +08:00
callee: CREATE_VNODE,
arguments: [`"p"`]
}
],
locStub
2019-09-25 04:35:01 +08:00
)
])
2019-09-25 03:49:02 +08:00
]
})
)
expect(code).toMatch(`
return ${CREATE_VNODE}("div", {
id: "foo",
2019-09-25 04:35:01 +08:00
[prop]: bar,
[foo + bar]: bar
}, [
${CREATE_VNODE}("p", { "some-key": "foo" })
], [
2019-09-25 03:49:02 +08:00
foo,
${CREATE_VNODE}("p")
])`)
expect(code).toMatchSnapshot()
})
test('SequenceExpression', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.IF,
loc: locStub,
branches: [],
codegenNode: createSequenceExpression([
createSimpleExpression(`foo`, false),
createCallExpression(`bar`, [`baz`])
])
}
]
})
)
expect(code).toMatch(`return (foo, bar(baz))`)
expect(code).toMatchSnapshot()
})
test('ConditionalExpression', () => {
const { code } = generate(
createRoot({
children: [
{
type: NodeTypes.IF,
loc: locStub,
branches: [],
codegenNode: createSequenceExpression([
createSimpleExpression(`foo`, false),
createConditionalExpression(
createSimpleExpression(`ok`, false),
createCallExpression(`foo`),
createConditionalExpression(
createSimpleExpression(`orNot`, false),
createCallExpression(`bar`),
createCallExpression(`baz`)
)
)
])
}
]
})
)
expect(code).toMatch(
`return (foo, ok
? foo()
: orNot
? bar()
: baz())`
)
expect(code).toMatchSnapshot()
})
})