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'
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 {
options: Required<CodegenOptions>
@ -87,6 +94,7 @@ export class CodegenContext {
inline: false,
bindingMetadata: {},
expressionPlugins: [],
customGenOperation: null,
}
this.options = extend(defaultOptions, options)
this.block = ir.block

View File

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

View File

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

View File

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

View File

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

View File

@ -123,7 +123,6 @@ 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]