mirror of https://github.com/vuejs/core.git
Merge 704fee955e
into bb4ae25793
This commit is contained in:
commit
1070c5b7dd
|
@ -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
|
||||
|
|
|
@ -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}`,
|
||||
|
|
|
@ -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
|
||||
})
|
||||
|
|
|
@ -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` : ``)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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]
|
||||
|
|
Loading…
Reference in New Issue