Skip to content
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
42 changes: 10 additions & 32 deletions packages/compiler-vapor/src/transforms/transformText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import {
} from '@vue/compiler-dom'
import type { NodeTransform, TransformContext } from '../transform'
import { DynamicFlag, IRNodeTypes } from '../ir'
import {
getLiteralExpressionValue,
isConstantExpression,
isStaticExpression,
} from '../utils'
import { getLiteralExpressionValue } from '../utils'
import { escapeHtml } from '@vue/shared'

type TextLike = TextNode | InterpolationNode
Expand Down Expand Up @@ -62,7 +58,7 @@ export const transformText: NodeTransform = (node, context) => {
// all text like with interpolation
if (!isFragment && isAllTextLike && hasInterp) {
processTextContainer(
node.children as TextLike[],
processTextLikeChildren(node.children as TextLike[], context),
context as TransformContext<ElementNode>,
)
} else if (hasInterp) {
Expand Down Expand Up @@ -112,35 +108,17 @@ function processInterpolation(context: TransformContext<InterpolationNode>) {
return
}

const nonConstantExps = values.filter(v => !isConstantExpression(v))
const isStatic =
!nonConstantExps.length ||
nonConstantExps.every(e =>
isStaticExpression(e, context.options.bindingMetadata),
) ||
context.inVOnce

if (isStatic) {
context.registerOperation({
type: IRNodeTypes.SET_TEXT,
element: id,
values,
})
} else {
context.registerEffect(values, {
type: IRNodeTypes.SET_TEXT,
element: id,
values,
})
}
context.registerEffect(values, {
type: IRNodeTypes.SET_TEXT,
element: id,
values,
})
}

function processTextContainer(
children: TextLike[],
export function processTextContainer(
values: SimpleExpressionNode[],
context: TransformContext<ElementNode>,
) {
const values = processTextLikeChildren(children, context)

): void {
const literals = values.map(getLiteralExpressionValue)

if (literals.every(l => l != null)) {
Expand Down
20 changes: 2 additions & 18 deletions packages/compiler-vapor/src/transforms/vText.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DOMErrorCodes, createDOMCompilerError } from '@vue/compiler-dom'
import { IRNodeTypes } from '../ir'
import { EMPTY_EXPRESSION } from './utils'
import type { DirectiveTransform } from '../transform'
import { getLiteralExpressionValue } from '../utils'
import { isVoidTag } from '../../../shared/src'
import { processTextContainer } from './transformText'

export const transformVText: DirectiveTransform = (dir, node, context) => {
let { exp, loc } = dir
Expand All @@ -25,20 +24,5 @@ export const transformVText: DirectiveTransform = (dir, node, context) => {
return
}

const literal = getLiteralExpressionValue(exp)
if (literal != null) {
context.childrenTemplate = [String(literal)]
} else {
context.childrenTemplate = [' ']
context.registerOperation({
type: IRNodeTypes.GET_TEXT_CHILD,
parent: context.reference(),
})
context.registerEffect([exp], {
type: IRNodeTypes.SET_TEXT,
element: context.reference(),
values: [exp],
generated: true,
})
}
processTextContainer([exp], context)
}
5 changes: 2 additions & 3 deletions packages/compiler-vapor/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { BigIntLiteral, NumericLiteral, StringLiteral } from '@babel/types'
import { isGloballyAllowed } from '@vue/shared'
import {
type AttributeNode,
Expand Down Expand Up @@ -75,10 +74,10 @@ export function resolveExpression(

export function getLiteralExpressionValue(
exp: SimpleExpressionNode,
): number | string | boolean | null {
): string | null {
if (exp.ast) {
if (exp.ast.type === 'StringLiteral') {
return (exp.ast as StringLiteral | NumericLiteral | BigIntLiteral).value
return exp.ast.value
} else if (
exp.ast.type === 'TemplateLiteral' &&
exp.ast.expressions.length === 0
Expand Down
Loading