mirror of https://github.com/vuejs/core.git
refactor(compiler-vapor): code fragment with falsy value
This commit is contained in:
parent
9ffd4b6c75
commit
ef12b99d0c
|
@ -9,7 +9,7 @@ import {
|
||||||
} from '@vue/compiler-dom'
|
} from '@vue/compiler-dom'
|
||||||
import type { IREffect, RootIRNode, VaporHelper } from './ir'
|
import type { IREffect, RootIRNode, VaporHelper } from './ir'
|
||||||
import { SourceMapGenerator } from 'source-map-js'
|
import { SourceMapGenerator } from 'source-map-js'
|
||||||
import { extend, isString, remove } from '@vue/shared'
|
import { extend, isArray, isString, remove } from '@vue/shared'
|
||||||
import type { ParserPlugin } from '@babel/parser'
|
import type { ParserPlugin } from '@babel/parser'
|
||||||
import { genTemplate } from './generators/template'
|
import { genTemplate } from './generators/template'
|
||||||
import { genBlockFunctionContent } from './generators/block'
|
import { genBlockFunctionContent } from './generators/block'
|
||||||
|
@ -18,6 +18,7 @@ interface CodegenOptions extends BaseCodegenOptions {
|
||||||
expressionPlugins?: ParserPlugin[]
|
expressionPlugins?: ParserPlugin[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FalsyValue = false | null | undefined
|
||||||
export type CodeFragment =
|
export type CodeFragment =
|
||||||
| typeof NEWLINE
|
| typeof NEWLINE
|
||||||
| typeof LF
|
| typeof LF
|
||||||
|
@ -25,7 +26,9 @@ export type CodeFragment =
|
||||||
| typeof INDENT_END
|
| typeof INDENT_END
|
||||||
| string
|
| string
|
||||||
| [code: string, newlineIndex?: number, loc?: SourceLocation, name?: string]
|
| [code: string, newlineIndex?: number, loc?: SourceLocation, name?: string]
|
||||||
| undefined
|
| FalsyValue
|
||||||
|
|
||||||
|
type CodeFragments = Exclude<CodeFragment, any[]> | CodeFragment[]
|
||||||
|
|
||||||
export class CodegenContext {
|
export class CodegenContext {
|
||||||
options: Required<CodegenOptions>
|
options: Required<CodegenOptions>
|
||||||
|
@ -35,26 +38,27 @@ export class CodegenContext {
|
||||||
|
|
||||||
push: (...args: CodeFragment[]) => void
|
push: (...args: CodeFragment[]) => void
|
||||||
multi = (
|
multi = (
|
||||||
[left, right, seg]: [left: string, right: string, segment: string],
|
[left, right, seg]: [
|
||||||
...fns: Array<false | string | CodeFragment[]>
|
left: CodeFragment,
|
||||||
|
right: CodeFragment,
|
||||||
|
segment: CodeFragment,
|
||||||
|
],
|
||||||
|
...fns: CodeFragments[]
|
||||||
): CodeFragment[] => {
|
): CodeFragment[] => {
|
||||||
const frag: CodeFragment[] = []
|
const frag: CodeFragment[] = []
|
||||||
fns = fns.filter(Boolean)
|
fns = fns.filter(Boolean)
|
||||||
frag.push(left)
|
frag.push(left)
|
||||||
for (let [i, fn] of fns.entries()) {
|
for (let [i, fn] of (
|
||||||
if (fn) {
|
fns as Array<Exclude<CodeFragments, FalsyValue>>
|
||||||
if (isString(fn)) fn = [fn]
|
).entries()) {
|
||||||
frag.push(...fn)
|
if (!isArray(fn)) fn = [fn]
|
||||||
if (i < fns.length - 1) frag.push(seg)
|
frag.push(...fn)
|
||||||
}
|
if (i < fns.length - 1) frag.push(seg)
|
||||||
}
|
}
|
||||||
frag.push(right)
|
frag.push(right)
|
||||||
return frag
|
return frag
|
||||||
}
|
}
|
||||||
call = (
|
call = (name: string, ...args: CodeFragments[]): CodeFragment[] => {
|
||||||
name: string,
|
|
||||||
...args: Array<false | string | CodeFragment[]>
|
|
||||||
): CodeFragment[] => {
|
|
||||||
return [name, ...this.multi(['(', ')', ', '], ...args)]
|
return [name, ...this.multi(['(', ')', ', '], ...args)]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +157,7 @@ export function generate(
|
||||||
}
|
}
|
||||||
|
|
||||||
const functionName = 'render'
|
const functionName = 'render'
|
||||||
const isSetupInlined = !!options.inline
|
const isSetupInlined = options.inline
|
||||||
if (isSetupInlined) {
|
if (isSetupInlined) {
|
||||||
push(`(() => {`)
|
push(`(() => {`)
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue