2019-09-25 03:49:02 +08:00
|
|
|
import {
|
2019-10-02 03:43:59 +08:00
|
|
|
locStub,
|
2019-09-25 03:49:02 +08:00
|
|
|
generate,
|
|
|
|
NodeTypes,
|
|
|
|
RootNode,
|
2019-09-27 23:42:02 +08:00
|
|
|
createSimpleExpression,
|
2019-09-25 03:49:02 +08:00
|
|
|
createObjectExpression,
|
|
|
|
createObjectProperty,
|
2019-09-25 04:35:01 +08:00
|
|
|
createArrayExpression,
|
2019-09-27 23:42:02 +08:00
|
|
|
createCompoundExpression,
|
2019-10-02 00:25:13 +08:00
|
|
|
createInterpolation,
|
2019-10-02 03:43:59 +08:00
|
|
|
createSequenceExpression,
|
|
|
|
createCallExpression,
|
2019-10-06 10:47:20 +08:00
|
|
|
createConditionalExpression,
|
|
|
|
IfCodegenNode,
|
|
|
|
ForCodegenNode
|
2019-09-25 03:49:02 +08:00
|
|
|
} from '../src'
|
2019-10-06 05:18:25 +08:00
|
|
|
import {
|
|
|
|
CREATE_VNODE,
|
|
|
|
COMMENT,
|
|
|
|
TO_STRING,
|
|
|
|
RESOLVE_DIRECTIVE,
|
|
|
|
helperNameMap,
|
|
|
|
RESOLVE_COMPONENT
|
|
|
|
} from '../src/runtimeHelpers'
|
2019-10-02 03:43:59 +08:00
|
|
|
import { createElementWithCodegen } from './testUtils'
|
2019-10-06 11:20:02 +08:00
|
|
|
import { PatchFlags } from '@vue/shared'
|
2019-09-25 03:49:02 +08:00
|
|
|
|
|
|
|
function createRoot(options: Partial<RootNode> = {}): RootNode {
|
|
|
|
return {
|
|
|
|
type: NodeTypes.ROOT,
|
|
|
|
children: [],
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: [],
|
|
|
|
components: [],
|
|
|
|
directives: [],
|
2019-09-26 02:13:33 +08:00
|
|
|
hoists: [],
|
2019-10-08 22:50:00 +08:00
|
|
|
codegenNode: createSimpleExpression(`null`, false),
|
2019-10-02 03:43:59 +08:00
|
|
|
loc: locStub,
|
2019-09-25 03:49:02 +08:00
|
|
|
...options
|
|
|
|
}
|
|
|
|
}
|
2019-09-20 11:05:51 +08:00
|
|
|
|
|
|
|
describe('compiler: codegen', () => {
|
2019-09-25 03:49:02 +08:00
|
|
|
test('module mode preamble', () => {
|
|
|
|
const root = createRoot({
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: [CREATE_VNODE, RESOLVE_DIRECTIVE]
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
const { code } = generate(root, { mode: 'module' })
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`import { ${helperNameMap[CREATE_VNODE]}, ${
|
|
|
|
helperNameMap[RESOLVE_DIRECTIVE]
|
|
|
|
} } from "vue"`
|
|
|
|
)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('function mode preamble', () => {
|
|
|
|
const root = createRoot({
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: [CREATE_VNODE, RESOLVE_DIRECTIVE]
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
const { code } = generate(root, { mode: 'function' })
|
2019-09-26 03:09:58 +08:00
|
|
|
expect(code).toMatch(`const _Vue = Vue`)
|
2019-09-26 07:17:45 +08:00
|
|
|
expect(code).toMatch(
|
2019-10-06 05:18:25 +08:00
|
|
|
`const { ${helperNameMap[CREATE_VNODE]}: _${
|
|
|
|
helperNameMap[CREATE_VNODE]
|
|
|
|
}, ${helperNameMap[RESOLVE_DIRECTIVE]}: _${
|
|
|
|
helperNameMap[RESOLVE_DIRECTIVE]
|
|
|
|
} } = _Vue`
|
2019-09-26 07:17:45 +08:00
|
|
|
)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('function mode preamble w/ prefixIdentifiers: true', () => {
|
|
|
|
const root = createRoot({
|
2019-10-06 05:18:25 +08:00
|
|
|
helpers: [CREATE_VNODE, RESOLVE_DIRECTIVE]
|
2019-09-26 07:17:45 +08:00
|
|
|
})
|
|
|
|
const { code } = generate(root, {
|
|
|
|
mode: 'function',
|
|
|
|
prefixIdentifiers: true
|
|
|
|
})
|
|
|
|
expect(code).not.toMatch(`const _Vue = Vue`)
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`const { ${helperNameMap[CREATE_VNODE]}, ${
|
|
|
|
helperNameMap[RESOLVE_DIRECTIVE]
|
|
|
|
} } = Vue`
|
|
|
|
)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-10-06 05:18:25 +08:00
|
|
|
test('assets', () => {
|
2019-09-25 03:49:02 +08:00
|
|
|
const root = createRoot({
|
2019-10-11 01:55:26 +08:00
|
|
|
components: [`Foo`, `bar-baz`, `barbaz`],
|
2019-10-06 05:18:25 +08:00
|
|
|
directives: [`my_dir`]
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
const { code } = generate(root, { mode: 'function' })
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`const _component_Foo = _${helperNameMap[RESOLVE_COMPONENT]}("Foo")\n`
|
|
|
|
)
|
|
|
|
expect(code).toMatch(
|
2019-10-11 01:55:26 +08:00
|
|
|
`const _component_bar_baz = _${
|
2019-10-06 05:18:25 +08:00
|
|
|
helperNameMap[RESOLVE_COMPONENT]
|
|
|
|
}("bar-baz")\n`
|
|
|
|
)
|
2019-10-11 01:55:26 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`const _component_barbaz = _${
|
|
|
|
helperNameMap[RESOLVE_COMPONENT]
|
|
|
|
}("barbaz")\n`
|
|
|
|
)
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`const _directive_my_dir = _${
|
|
|
|
helperNameMap[RESOLVE_DIRECTIVE]
|
|
|
|
}("my_dir")\n`
|
|
|
|
)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-09-26 02:13:33 +08:00
|
|
|
test('hoists', () => {
|
|
|
|
const root = createRoot({
|
|
|
|
hoists: [
|
2019-10-02 03:43:59 +08:00
|
|
|
createSimpleExpression(`hello`, false, locStub),
|
2019-09-26 02:13:33 +08:00
|
|
|
createObjectExpression(
|
|
|
|
[
|
|
|
|
createObjectProperty(
|
2019-10-02 03:43:59 +08:00
|
|
|
createSimpleExpression(`id`, true, locStub),
|
|
|
|
createSimpleExpression(`foo`, true, locStub)
|
2019-09-26 02:13:33 +08:00
|
|
|
)
|
|
|
|
],
|
2019-10-02 03:43:59 +08:00
|
|
|
locStub
|
2019-09-26 02:13:33 +08:00
|
|
|
)
|
|
|
|
]
|
|
|
|
})
|
|
|
|
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({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: {
|
|
|
|
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({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: createInterpolation(`hello`, locStub)
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
)
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(`return _${helperNameMap[TO_STRING]}(hello)`)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('comment', () => {
|
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: {
|
|
|
|
type: NodeTypes.COMMENT,
|
|
|
|
content: 'foo',
|
|
|
|
loc: locStub
|
|
|
|
}
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
)
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(
|
|
|
|
`return _${helperNameMap[CREATE_VNODE]}(_${
|
|
|
|
helperNameMap[COMMENT]
|
2019-10-11 23:04:56 +08:00
|
|
|
}, null, "foo")`
|
2019-10-06 05:18:25 +08:00
|
|
|
)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-10-03 01:11:07 +08:00
|
|
|
test('compound expression', () => {
|
2019-09-25 03:49:02 +08:00
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: createCompoundExpression([
|
|
|
|
`_ctx.`,
|
|
|
|
createSimpleExpression(`foo`, false, locStub),
|
|
|
|
` + `,
|
2019-09-25 03:49:02 +08:00
|
|
|
{
|
2019-10-03 01:11:07 +08:00
|
|
|
type: NodeTypes.INTERPOLATION,
|
|
|
|
loc: locStub,
|
|
|
|
content: createSimpleExpression(`bar`, false, locStub)
|
2019-09-25 03:49:02 +08:00
|
|
|
}
|
2019-10-03 01:11:07 +08:00
|
|
|
])
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
)
|
2019-10-06 05:18:25 +08:00
|
|
|
expect(code).toMatch(`return _ctx.foo + _${helperNameMap[TO_STRING]}(bar)`)
|
2019-09-26 07:17:45 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-10-03 01:11:07 +08:00
|
|
|
test('ifNode', () => {
|
2019-09-25 04:35:01 +08:00
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: {
|
|
|
|
type: NodeTypes.IF,
|
|
|
|
loc: locStub,
|
|
|
|
branches: [],
|
|
|
|
codegenNode: createSequenceExpression([
|
|
|
|
createSimpleExpression('foo', false),
|
|
|
|
createSimpleExpression('bar', false)
|
2019-10-06 10:47:20 +08:00
|
|
|
]) as IfCodegenNode
|
2019-10-03 01:11:07 +08:00
|
|
|
}
|
2019-09-25 04:35:01 +08:00
|
|
|
})
|
|
|
|
)
|
2019-10-03 01:11:07 +08:00
|
|
|
expect(code).toMatch(`return (foo, bar)`)
|
2019-09-25 04:35:01 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-10-03 01:11:07 +08:00
|
|
|
test('forNode', () => {
|
2019-09-25 03:49:02 +08:00
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: {
|
|
|
|
type: NodeTypes.FOR,
|
|
|
|
loc: locStub,
|
|
|
|
source: createSimpleExpression('foo', false),
|
|
|
|
valueAlias: undefined,
|
|
|
|
keyAlias: undefined,
|
|
|
|
objectIndexAlias: undefined,
|
|
|
|
children: [],
|
|
|
|
codegenNode: createSequenceExpression([
|
|
|
|
createSimpleExpression('foo', false),
|
|
|
|
createSimpleExpression('bar', false)
|
2019-10-06 10:47:20 +08:00
|
|
|
]) as ForCodegenNode
|
2019-10-03 01:11:07 +08:00
|
|
|
}
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
)
|
2019-10-02 00:25:13 +08:00
|
|
|
expect(code).toMatch(`return (foo, bar)`)
|
2019-09-25 04:35:01 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
2019-10-06 10:47:20 +08:00
|
|
|
test('Element (callExpression + objectExpression + TemplateChildNode[])', () => {
|
2019-09-25 04:35:01 +08:00
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: createElementWithCodegen([
|
|
|
|
// string
|
|
|
|
`"div"`,
|
|
|
|
// ObjectExpression
|
|
|
|
createObjectExpression(
|
2019-09-25 04:35:01 +08:00
|
|
|
[
|
2019-10-03 01:11:07 +08:00
|
|
|
createObjectProperty(
|
|
|
|
createSimpleExpression(`id`, true, locStub),
|
|
|
|
createSimpleExpression(`foo`, true, locStub)
|
|
|
|
),
|
|
|
|
createObjectProperty(
|
|
|
|
createSimpleExpression(`prop`, false, locStub),
|
|
|
|
createSimpleExpression(`bar`, false, locStub)
|
|
|
|
),
|
|
|
|
// compound expression as computed key
|
|
|
|
createObjectProperty(
|
2019-09-25 04:35:01 +08:00
|
|
|
{
|
2019-10-03 01:11:07 +08:00
|
|
|
type: NodeTypes.COMPOUND_EXPRESSION,
|
2019-10-02 03:43:59 +08:00
|
|
|
loc: locStub,
|
2019-10-03 01:11:07 +08:00
|
|
|
children: [
|
|
|
|
`foo + `,
|
|
|
|
createSimpleExpression(`bar`, false, locStub)
|
|
|
|
]
|
|
|
|
},
|
|
|
|
createSimpleExpression(`bar`, false, locStub)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
locStub
|
|
|
|
),
|
|
|
|
// ChildNode[]
|
|
|
|
[
|
|
|
|
createElementWithCodegen([
|
|
|
|
`"p"`,
|
|
|
|
createObjectExpression(
|
|
|
|
[
|
|
|
|
createObjectProperty(
|
|
|
|
// should quote the key!
|
|
|
|
createSimpleExpression(`some-key`, true, locStub),
|
|
|
|
createSimpleExpression(`foo`, true, locStub)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
locStub
|
|
|
|
)
|
|
|
|
])
|
|
|
|
],
|
2019-10-06 10:47:20 +08:00
|
|
|
// flag
|
|
|
|
PatchFlags.FULL_PROPS + ''
|
2019-10-03 01:11:07 +08:00
|
|
|
])
|
2019-09-25 03:49:02 +08:00
|
|
|
})
|
|
|
|
)
|
|
|
|
expect(code).toMatch(`
|
2019-10-06 05:18:25 +08:00
|
|
|
return _${helperNameMap[CREATE_VNODE]}("div", {
|
2019-09-25 03:49:02 +08:00
|
|
|
id: "foo",
|
2019-09-25 04:35:01 +08:00
|
|
|
[prop]: bar,
|
|
|
|
[foo + bar]: bar
|
2019-09-28 12:19:24 +08:00
|
|
|
}, [
|
2019-10-06 05:18:25 +08:00
|
|
|
_${helperNameMap[CREATE_VNODE]}("p", { "some-key": "foo" })
|
2019-10-06 10:47:20 +08:00
|
|
|
], ${PatchFlags.FULL_PROPS})`)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('ArrayExpression', () => {
|
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
|
|
|
codegenNode: createArrayExpression([
|
|
|
|
createSimpleExpression(`foo`, false),
|
|
|
|
createCallExpression(`bar`, [`baz`])
|
|
|
|
])
|
|
|
|
})
|
|
|
|
)
|
|
|
|
expect(code).toMatch(`return [
|
2019-09-25 03:49:02 +08:00
|
|
|
foo,
|
2019-10-06 10:47:20 +08:00
|
|
|
bar(baz)
|
|
|
|
]`)
|
2019-09-25 03:49:02 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2019-10-02 00:25:13 +08:00
|
|
|
|
2019-10-02 03:43:59 +08:00
|
|
|
test('SequenceExpression', () => {
|
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: createSequenceExpression([
|
|
|
|
createSimpleExpression(`foo`, false),
|
|
|
|
createCallExpression(`bar`, [`baz`])
|
|
|
|
])
|
2019-10-02 03:43:59 +08:00
|
|
|
})
|
|
|
|
)
|
|
|
|
expect(code).toMatch(`return (foo, bar(baz))`)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2019-10-02 00:25:13 +08:00
|
|
|
|
2019-10-02 03:43:59 +08:00
|
|
|
test('ConditionalExpression', () => {
|
|
|
|
const { code } = generate(
|
|
|
|
createRoot({
|
2019-10-03 01:11:07 +08:00
|
|
|
codegenNode: createConditionalExpression(
|
|
|
|
createSimpleExpression(`ok`, false),
|
|
|
|
createCallExpression(`foo`),
|
|
|
|
createConditionalExpression(
|
|
|
|
createSimpleExpression(`orNot`, false),
|
|
|
|
createCallExpression(`bar`),
|
|
|
|
createCallExpression(`baz`)
|
|
|
|
)
|
|
|
|
)
|
2019-10-02 03:43:59 +08:00
|
|
|
})
|
|
|
|
)
|
|
|
|
expect(code).toMatch(
|
2019-10-03 01:11:07 +08:00
|
|
|
`return ok
|
2019-10-02 03:43:59 +08:00
|
|
|
? foo()
|
|
|
|
: orNot
|
|
|
|
? bar()
|
2019-10-03 01:11:07 +08:00
|
|
|
: baz()`
|
2019-10-02 03:43:59 +08:00
|
|
|
)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
2019-09-20 11:05:51 +08:00
|
|
|
})
|