Skip to content
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

feat(compiler-vapor): add jsx support for setText and createTextNode #12893

Open
wants to merge 3 commits into
base: vapor
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
13 changes: 7 additions & 6 deletions packages/compiler-vapor/src/generators/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export function genSetText(
context: CodegenContext,
): CodeFragment[] {
const { helper } = context
const { element, values, generated } = oper
const texts = combineValues(values, context)
const { element, values, generated, jsx } = oper
const texts = combineValues(values, context, jsx)
return [
NEWLINE,
...genCall(helper('setText'), `${generated ? 'x' : 'n'}${element}`, texts),
Expand All @@ -27,29 +27,30 @@ export function genCreateTextNode(
context: CodegenContext,
): CodeFragment[] {
const { helper } = context
const { id, values } = oper
const { id, values, jsx } = oper
return [
NEWLINE,
`const n${id} = `,
...genCall(
helper('createTextNode'),
values && combineValues(values, context),
values && combineValues(values, context, jsx),
),
]
}

function combineValues(
values: SimpleExpressionNode[],
context: CodegenContext,
jsx?: boolean,
): CodeFragment[] {
return values.flatMap((value, i) => {
let exp = genExpression(value, context)
if (getLiteralExpressionValue(value) == null) {
if (!jsx && getLiteralExpressionValue(value) == null) {
// dynamic, wrap with toDisplayString
exp = genCall(context.helper('toDisplayString'), exp)
}
if (i > 0) {
exp.unshift(' + ')
exp.unshift(jsx ? ', ' : ' + ')
}
return exp
})
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-vapor/src/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export interface SetTextIRNode extends BaseIRNode {
element: number
values: SimpleExpressionNode[]
generated?: boolean // whether this is a generated empty text node by `processTextLikeContainer`
jsx?: boolean
}

export type KeyOverride = [find: string, replacement: string]
Expand Down Expand Up @@ -161,6 +162,7 @@ export interface CreateTextNodeIRNode extends BaseIRNode {
type: IRNodeTypes.CREATE_TEXT_NODE
id: number
values?: SimpleExpressionNode[]
jsx?: boolean
}

export interface InsertNodeIRNode extends BaseIRNode {
Expand Down