Skip to content

fix(compiler-vapor): empty interpolation #13592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: minor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,53 @@ export function render(_ctx) {
}"
`;

exports[`compiler: expression > empty interpolation 1`] = `
"import { template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler: expression > empty interpolation 2`] = `
"import { template as _template } from 'vue';
const t0 = _template(" ")

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler: expression > empty interpolation 3`] = `
"import { template as _template } from 'vue';
const t0 = _template("<div></div>", true)

export function render(_ctx) {
const n0 = t0()
return n0
}"
`;

exports[`compiler: expression > empty interpolation 4`] = `
"import { child as _child, toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template("<div> </div>", true)

export function render(_ctx) {
const n1 = t0()
const n0 = _child(n1)
const x1 = _child(n1)
_renderEffect(() => {
const _foo = _ctx.foo
_setText(n0, _toDisplayString(_foo))
_setText(x1, _toDisplayString(_foo))
})
return n1
}"
`;

exports[`compiler: expression > props 1`] = `
"import { toDisplayString as _toDisplayString, setText as _setText, renderEffect as _renderEffect, template as _template } from 'vue';
const t0 = _template(" ")
Expand Down
21 changes: 21 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,25 @@ describe('compiler: expression', () => {
expect(code).toMatchSnapshot()
expect(code).contains(`_String(_foo.id++)`)
})

test('empty interpolation', () => {
const { code } = compileWithExpression(`{{}}`)
const { code: code2 } = compileWithExpression(`{{ }}`)
const { code: code3 } = compileWithExpression(`<div>{{ }}</div>`)
const { code: code4 } = compileWithExpression(`<div>{{ foo }}{{ }}</div>`)

expect(code).toMatchSnapshot()
expect(code).not.toContain(`_toDisplayString`)
expect(code).not.toContain(`_setText`)

expect(code2).toMatchSnapshot()
expect(code2).not.toContain(`_toDisplayString`)
expect(code2).not.toContain(`_setText`)

expect(code3).toMatchSnapshot()
expect(code3).not.toContain(`_toDisplayString`)
expect(code3).not.toContain(`_setText`)

expect(code4).toMatchSnapshot()
})
})
38 changes: 29 additions & 9 deletions packages/compiler-vapor/src/transforms/transformText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ export const transformText: NodeTransform = (node, context) => {
}

function processInterpolation(context: TransformContext<InterpolationNode>) {
const children = context.parent!.node.children
const parentNode = context.parent!.node
const children = parentNode.children
const nexts = children.slice(context.index)
const idx = nexts.findIndex(n => !isTextLike(n))
const nodes = (idx > -1 ? nexts.slice(0, idx) : nexts) as Array<TextLike>
Expand All @@ -97,10 +98,18 @@ function processInterpolation(context: TransformContext<InterpolationNode>) {
if (prev && prev.type === NodeTypes.TEXT) {
nodes.unshift(prev)
}
const values = processTextLikeChildren(nodes, context)

if (values.length === 0 && parentNode.type !== NodeTypes.ROOT) {
return
}

context.template += ' '
const id = context.reference()
const values = nodes.map(node => createTextLikeExpression(node, context))

if (values.length === 0) {
return
}

const nonConstantExps = values.filter(v => !isConstantExpression(v))
const isStatic =
Expand Down Expand Up @@ -129,8 +138,10 @@ function processTextContainer(
children: TextLike[],
context: TransformContext<ElementNode>,
) {
const values = children.map(child => createTextLikeExpression(child, context))
const values = processTextLikeChildren(children, context)

const literals = values.map(getLiteralExpressionValue)

if (literals.every(l => l != null)) {
context.childrenTemplate = literals.map(l => String(l))
} else {
Expand All @@ -149,13 +160,22 @@ function processTextContainer(
}
}

function createTextLikeExpression(node: TextLike, context: TransformContext) {
markNonTemplate(node, context)
if (node.type === NodeTypes.TEXT) {
return createSimpleExpression(node.content, true, node.loc)
} else {
return node.content as SimpleExpressionNode
function processTextLikeChildren(nodes: TextLike[], context: TransformContext) {
const exps: SimpleExpressionNode[] = []
for (const node of nodes) {
let exp: SimpleExpressionNode
markNonTemplate(node, context)

if (node.type === NodeTypes.TEXT) {
exp = createSimpleExpression(node.content, true, node.loc)
} else {
exp = node.content as SimpleExpressionNode
}

if (exp.content) exps.push(exp)
}

return exps
}

function isTextLike(node: TemplateChildNode): node is TextLike {
Expand Down