This commit is contained in:
zhiyuanzmj 2025-06-26 16:10:58 +08:00 committed by GitHub
commit 1070c5b7dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 21 additions and 14 deletions

View File

@ -19,7 +19,14 @@ import {
} from './generators/utils' } from './generators/utils'
import { setTemplateRefIdent } from './generators/templateRef' import { setTemplateRefIdent } from './generators/templateRef'
export type CodegenOptions = Omit<BaseCodegenOptions, 'optimizeImports'> type CustomGenOperation = (
opers: any,
context: CodegenContext,
) => CodeFragment[] | void
export type CodegenOptions = Omit<BaseCodegenOptions, 'optimizeImports'> & {
customGenOperation?: CustomGenOperation | null
}
export class CodegenContext { export class CodegenContext {
options: Required<CodegenOptions> options: Required<CodegenOptions>
@ -87,6 +94,7 @@ export class CodegenContext {
inline: false, inline: false,
bindingMetadata: {}, bindingMetadata: {},
expressionPlugins: [], expressionPlugins: [],
customGenOperation: null,
} }
this.options = extend(defaultOptions, options) this.options = extend(defaultOptions, options)
this.block = ir.block this.block = ir.block

View File

@ -88,6 +88,11 @@ export function genOperation(
case IRNodeTypes.GET_TEXT_CHILD: case IRNodeTypes.GET_TEXT_CHILD:
return genGetTextChild(oper, context) return genGetTextChild(oper, context)
default: default:
if (context.options.customGenOperation) {
const result = context.options.customGenOperation(oper, context)
if (result) return result
}
const exhaustiveCheck: never = oper const exhaustiveCheck: never = oper
throw new Error( throw new Error(
`Unhandled operation type in genOperation: ${exhaustiveCheck}`, `Unhandled operation type in genOperation: ${exhaustiveCheck}`,

View File

@ -10,8 +10,8 @@ export function genSetText(
context: CodegenContext, context: CodegenContext,
): CodeFragment[] { ): CodeFragment[] {
const { helper } = context const { helper } = context
const { element, values, generated, jsx } = oper const { element, values, generated } = oper
const texts = combineValues(values, context, jsx) const texts = combineValues(values, context)
return [ return [
NEWLINE, NEWLINE,
...genCall(helper('setText'), `${generated ? 'x' : 'n'}${element}`, texts), ...genCall(helper('setText'), `${generated ? 'x' : 'n'}${element}`, texts),
@ -21,16 +21,15 @@ export function genSetText(
function combineValues( function combineValues(
values: SimpleExpressionNode[], values: SimpleExpressionNode[],
context: CodegenContext, context: CodegenContext,
jsx?: boolean,
): CodeFragment[] { ): CodeFragment[] {
return values.flatMap((value, i) => { return values.flatMap((value, i) => {
let exp = genExpression(value, context) let exp = genExpression(value, context)
if (!jsx && getLiteralExpressionValue(value) == null) { if (getLiteralExpressionValue(value) == null) {
// dynamic, wrap with toDisplayString // dynamic, wrap with toDisplayString
exp = genCall(context.helper('toDisplayString'), exp) exp = genCall(context.helper('toDisplayString'), exp)
} }
if (i > 0) { if (i > 0) {
exp.unshift(jsx ? ', ' : ' + ') exp.unshift(' + ')
} }
return exp return exp
}) })

View File

@ -10,6 +10,8 @@ import {
import { isArray, isString } from '@vue/shared' import { isArray, isString } from '@vue/shared'
import type { CodegenContext } from '../generate' import type { CodegenContext } from '../generate'
export { genExpression } from './expression'
export const NEWLINE: unique symbol = Symbol(__DEV__ ? `newline` : ``) export const NEWLINE: unique symbol = Symbol(__DEV__ ? `newline` : ``)
/** increase offset but don't push actual code */ /** increase offset but don't push actual code */
export const LF: unique symbol = Symbol(__DEV__ ? `line feed` : ``) export const LF: unique symbol = Symbol(__DEV__ ? `line feed` : ``)

View File

@ -13,13 +13,7 @@ export {
type CodegenOptions, type CodegenOptions,
type VaporCodegenResult, type VaporCodegenResult,
} from './generate' } from './generate'
export { export * from './generators/utils'
genCall,
genMulti,
buildCodeFragment,
codeFragmentToString,
type CodeFragment,
} from './generators/utils'
export { export {
wrapTemplate, wrapTemplate,
compile, compile,

View File

@ -123,7 +123,6 @@ export interface SetTextIRNode extends BaseIRNode {
element: number element: number
values: SimpleExpressionNode[] values: SimpleExpressionNode[]
generated?: boolean // whether this is a generated empty text node by `processTextLikeContainer` generated?: boolean // whether this is a generated empty text node by `processTextLikeContainer`
jsx?: boolean
} }
export type KeyOverride = [find: string, replacement: string] export type KeyOverride = [find: string, replacement: string]