fix(compiler-vapor): create dynamic text node (#193)

This commit is contained in:
Doctor Wu 2024-04-30 21:28:55 +08:00 committed by GitHub
parent fb58e65d3d
commit 098b6fcf65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 81 additions and 3 deletions

View File

@ -0,0 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
exports[`compiler: text transform > consecutive text 1`] = `
"import { createTextNode as _createTextNode } from 'vue/vapor';
export function render(_ctx) {
const n0 = _createTextNode(() => [_ctx.msg])
return n0
}"
`;
exports[`compiler: text transform > no consecutive text 1`] = `
"import { createTextNode as _createTextNode } from 'vue/vapor';
export function render(_ctx) {
const n0 = _createTextNode(["hello world"])
return n0
}"
`;

View File

@ -1,4 +1,63 @@
// TODO: add tests for this transform
describe('compiler: text transform', () => {
test.todo('basic')
import {
IRNodeTypes,
transformChildren,
transformElement,
transformText,
transformVBind,
transformVOn,
} from '../../src'
import { makeCompile } from './_utils'
const compileWithTextTransform = makeCompile({
nodeTransforms: [transformElement, transformChildren, transformText],
directiveTransforms: {
bind: transformVBind,
on: transformVOn,
},
})
describe('compiler: text transform', () => {
it('no consecutive text', () => {
const { code, ir, vaporHelpers } = compileWithTextTransform(
'{{ "hello world" }}',
)
expect(code).toMatchSnapshot()
expect(vaporHelpers).contains.all.keys('createTextNode')
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.CREATE_TEXT_NODE,
id: 0,
values: [
{
type: IRNodeTypes.SET_TEXT,
content: '"hello world"',
isStatic: false,
},
],
effect: false,
},
])
})
it('consecutive text', () => {
const { code, ir, vaporHelpers } = compileWithTextTransform('{{ msg }}')
expect(code).toMatchSnapshot()
expect(vaporHelpers).contains.all.keys('createTextNode')
expect(ir.block.operation).toMatchObject([
{
type: IRNodeTypes.CREATE_TEXT_NODE,
id: 0,
values: [
{
type: IRNodeTypes.SET_TEXT,
content: 'msg',
isStatic: false,
},
],
effect: true,
},
])
})
})

View File

@ -56,7 +56,7 @@ function processTextLike(context: TransformContext<InterpolationNode>) {
type: IRNodeTypes.CREATE_TEXT_NODE,
id,
values,
effect: !values.some(isConstantExpression),
effect: !values.every(isConstantExpression) && !context.inVOnce,
})
}