Skip to content

Commit e90b836

Browse files
committed
test(compiler): tests for RootNode codegen transform
1 parent 24bd6c2 commit e90b836

File tree

2 files changed

+122
-3
lines changed

2 files changed

+122
-3
lines changed

packages/compiler-core/__tests__/transform.spec.ts

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,20 @@ import {
77
ExpressionNode
88
} from '../src/ast'
99
import { ErrorCodes, createCompilerError } from '../src/errors'
10-
import { TO_STRING, CREATE_VNODE, COMMENT } from '../src/runtimeConstants'
10+
import {
11+
TO_STRING,
12+
CREATE_VNODE,
13+
COMMENT,
14+
OPEN_BLOCK,
15+
CREATE_BLOCK,
16+
FRAGMENT,
17+
RENDER_SLOT
18+
} from '../src/runtimeConstants'
19+
import { transformIf } from '../src/transforms/vIf'
20+
import { transformFor } from '../src/transforms/vFor'
21+
import { transformElement } from '../src/transforms/transformElement'
22+
import { transformSlotOutlet } from '../src/transforms/transfromSlotOutlet'
23+
import { optimizeText } from '../src/transforms/optimizeText'
1124

1225
describe('compiler: transform', () => {
1326
test('context state', () => {
@@ -221,4 +234,110 @@ describe('compiler: transform', () => {
221234
expect(ast.imports).toContain(CREATE_VNODE)
222235
expect(ast.imports).toContain(COMMENT)
223236
})
237+
238+
describe('root codegenNode', () => {
239+
function transformWithCodegen(template: string) {
240+
const ast = parse(template)
241+
transform(ast, {
242+
nodeTransforms: [
243+
transformIf,
244+
transformFor,
245+
optimizeText,
246+
transformSlotOutlet,
247+
transformElement
248+
]
249+
})
250+
return ast
251+
}
252+
253+
function createBlockMatcher(args: any[]) {
254+
return {
255+
type: NodeTypes.JS_SEQUENCE_EXPRESSION,
256+
expressions: [
257+
{
258+
type: NodeTypes.JS_CALL_EXPRESSION,
259+
callee: `_${OPEN_BLOCK}`
260+
},
261+
{
262+
type: NodeTypes.JS_CALL_EXPRESSION,
263+
callee: `_${CREATE_BLOCK}`,
264+
arguments: args
265+
}
266+
]
267+
}
268+
}
269+
270+
test('no chidlren', () => {
271+
const ast = transformWithCodegen(``)
272+
expect(ast.codegenNode).toBeUndefined()
273+
})
274+
275+
test('single <slot/>', () => {
276+
const ast = transformWithCodegen(`<slot/>`)
277+
expect(ast.codegenNode).toMatchObject(
278+
createBlockMatcher([
279+
`_${FRAGMENT}`,
280+
`null`,
281+
{
282+
type: NodeTypes.JS_CALL_EXPRESSION,
283+
callee: `_${RENDER_SLOT}`
284+
}
285+
])
286+
)
287+
})
288+
289+
test('single element', () => {
290+
const ast = transformWithCodegen(`<div/>`)
291+
expect(ast.codegenNode).toMatchObject(createBlockMatcher([`"div"`]))
292+
})
293+
294+
test('root v-if', () => {
295+
const ast = transformWithCodegen(`<div v-if="ok" />`)
296+
expect(ast.codegenNode).toMatchObject({
297+
type: NodeTypes.IF
298+
})
299+
})
300+
301+
test('root v-for', () => {
302+
const ast = transformWithCodegen(`<div v-for="i in list" />`)
303+
expect(ast.codegenNode).toMatchObject({
304+
type: NodeTypes.FOR
305+
})
306+
})
307+
308+
test('single text', () => {
309+
const ast = transformWithCodegen(`hello`)
310+
expect(ast.codegenNode).toMatchObject({
311+
type: NodeTypes.TEXT
312+
})
313+
})
314+
315+
test('single interpolation', () => {
316+
const ast = transformWithCodegen(`{{ foo }}`)
317+
expect(ast.codegenNode).toMatchObject({
318+
type: NodeTypes.INTERPOLATION
319+
})
320+
})
321+
322+
test('single CompoundExpression', () => {
323+
const ast = transformWithCodegen(`{{ foo }} bar baz`)
324+
expect(ast.codegenNode).toMatchObject({
325+
type: NodeTypes.COMPOUND_EXPRESSION
326+
})
327+
})
328+
329+
test('multiple children', () => {
330+
const ast = transformWithCodegen(`<div/><div/>`)
331+
expect(ast.codegenNode).toMatchObject(
332+
createBlockMatcher([
333+
`_${FRAGMENT}`,
334+
`null`,
335+
[
336+
{ type: NodeTypes.ELEMENT, tag: `div` },
337+
{ type: NodeTypes.ELEMENT, tag: `div` }
338+
]
339+
])
340+
)
341+
})
342+
})
224343
})

packages/compiler-core/src/transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ function finalizeRoot(root: RootNode, context: TransformContext) {
196196
// only child is a <slot/> - it needs to be in a fragment block.
197197
if (child.tagType === ElementTypes.SLOT) {
198198
root.codegenNode = createBlockExpression(
199-
[helper(FRAGMENT), `null`, child.codegenNode],
199+
[helper(FRAGMENT), `null`, child.codegenNode!],
200200
context
201201
)
202202
} else {
203203
// turn root element into a block
204204
root.codegenNode = createBlockExpression(
205-
child.codegenNode.arguments,
205+
child.codegenNode!.arguments,
206206
context
207207
)
208208
}

0 commit comments

Comments
 (0)