Skip to content

Commit 098b6fc

Browse files
authored
fix(compiler-vapor): create dynamic text node (#193)
1 parent fb58e65 commit 098b6fc

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`compiler: text transform > consecutive text 1`] = `
4+
"import { createTextNode as _createTextNode } from 'vue/vapor';
5+
6+
export function render(_ctx) {
7+
const n0 = _createTextNode(() => [_ctx.msg])
8+
return n0
9+
}"
10+
`;
11+
12+
exports[`compiler: text transform > no consecutive text 1`] = `
13+
"import { createTextNode as _createTextNode } from 'vue/vapor';
14+
15+
export function render(_ctx) {
16+
const n0 = _createTextNode(["hello world"])
17+
return n0
18+
}"
19+
`;
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,63 @@
11
// TODO: add tests for this transform
2+
import {
3+
IRNodeTypes,
4+
transformChildren,
5+
transformElement,
6+
transformText,
7+
transformVBind,
8+
transformVOn,
9+
} from '../../src'
10+
11+
import { makeCompile } from './_utils'
12+
13+
const compileWithTextTransform = makeCompile({
14+
nodeTransforms: [transformElement, transformChildren, transformText],
15+
directiveTransforms: {
16+
bind: transformVBind,
17+
on: transformVOn,
18+
},
19+
})
20+
221
describe('compiler: text transform', () => {
3-
test.todo('basic')
22+
it('no consecutive text', () => {
23+
const { code, ir, vaporHelpers } = compileWithTextTransform(
24+
'{{ "hello world" }}',
25+
)
26+
expect(code).toMatchSnapshot()
27+
expect(vaporHelpers).contains.all.keys('createTextNode')
28+
expect(ir.block.operation).toMatchObject([
29+
{
30+
type: IRNodeTypes.CREATE_TEXT_NODE,
31+
id: 0,
32+
values: [
33+
{
34+
type: IRNodeTypes.SET_TEXT,
35+
content: '"hello world"',
36+
isStatic: false,
37+
},
38+
],
39+
effect: false,
40+
},
41+
])
42+
})
43+
44+
it('consecutive text', () => {
45+
const { code, ir, vaporHelpers } = compileWithTextTransform('{{ msg }}')
46+
expect(code).toMatchSnapshot()
47+
expect(vaporHelpers).contains.all.keys('createTextNode')
48+
expect(ir.block.operation).toMatchObject([
49+
{
50+
type: IRNodeTypes.CREATE_TEXT_NODE,
51+
id: 0,
52+
values: [
53+
{
54+
type: IRNodeTypes.SET_TEXT,
55+
content: 'msg',
56+
isStatic: false,
57+
},
58+
],
59+
effect: true,
60+
},
61+
])
62+
})
463
})

Diff for: packages/compiler-vapor/src/transforms/transformText.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function processTextLike(context: TransformContext<InterpolationNode>) {
5656
type: IRNodeTypes.CREATE_TEXT_NODE,
5757
id,
5858
values,
59-
effect: !values.some(isConstantExpression),
59+
effect: !values.every(isConstantExpression) && !context.inVOnce,
6060
})
6161
}
6262

0 commit comments

Comments
 (0)