Skip to content

Commit

Permalink
refactor(compiler-vapor): code fragment with falsy value
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Feb 6, 2024
1 parent 9ffd4b6 commit ef12b99
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions packages/compiler-vapor/src/generate.ts
Expand Up @@ -9,7 +9,7 @@ import {
} from '@vue/compiler-dom'
import type { IREffect, RootIRNode, VaporHelper } from './ir'
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 { genTemplate } from './generators/template'
import { genBlockFunctionContent } from './generators/block'
Expand All @@ -18,14 +18,17 @@ interface CodegenOptions extends BaseCodegenOptions {
expressionPlugins?: ParserPlugin[]
}

type FalsyValue = false | null | undefined
export type CodeFragment =
| typeof NEWLINE
| typeof LF
| typeof INDENT_START
| typeof INDENT_END
| string
| [code: string, newlineIndex?: number, loc?: SourceLocation, name?: string]
| undefined
| FalsyValue

type CodeFragments = Exclude<CodeFragment, any[]> | CodeFragment[]

export class CodegenContext {
options: Required<CodegenOptions>
Expand All @@ -35,26 +38,27 @@ export class CodegenContext {

push: (...args: CodeFragment[]) => void
multi = (
[left, right, seg]: [left: string, right: string, segment: string],
...fns: Array<false | string | CodeFragment[]>
[left, right, seg]: [
left: CodeFragment,
right: CodeFragment,
segment: CodeFragment,
],
...fns: CodeFragments[]
): CodeFragment[] => {
const frag: CodeFragment[] = []
fns = fns.filter(Boolean)
frag.push(left)
for (let [i, fn] of fns.entries()) {
if (fn) {
if (isString(fn)) fn = [fn]
frag.push(...fn)
if (i < fns.length - 1) frag.push(seg)
}
for (let [i, fn] of (
fns as Array<Exclude<CodeFragments, FalsyValue>>
).entries()) {
if (!isArray(fn)) fn = [fn]
frag.push(...fn)
if (i < fns.length - 1) frag.push(seg)
}
frag.push(right)
return frag
}
call = (
name: string,
...args: Array<false | string | CodeFragment[]>
): CodeFragment[] => {
call = (name: string, ...args: CodeFragments[]): CodeFragment[] => {
return [name, ...this.multi(['(', ')', ', '], ...args)]
}

Expand Down Expand Up @@ -153,7 +157,7 @@ export function generate(
}

const functionName = 'render'
const isSetupInlined = !!options.inline
const isSetupInlined = options.inline
if (isSetupInlined) {
push(`(() => {`)
} else {
Expand Down

0 comments on commit ef12b99

Please sign in to comment.