Skip to content

Commit

Permalink
update:Change screen state to "Generate render function based on AST" (
Browse files Browse the repository at this point in the history
  • Loading branch information
2nofa11 authored Feb 8, 2024
1 parent 5c088f5 commit bacf38f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { ElementNode, NodeTypes, TemplateChildNode, TextNode } from './ast'

export const generate = ({
tag,
props,
textContent,
children,
}: {
tag: string
props: Record<string, string>
textContent: string
children: TemplateChildNode[]
}): string => {
return `return () => {
return `return function render() {
const { h } = ChibiVue;
return h("${tag}", { ${Object.entries(props)
.map(([k, v]) => `${k}: "${v}"`)
.join(', ')} }, ["${textContent}"]);
return ${genNode(children[0])};
}`
}

const genNode = (node: TemplateChildNode): string => {
switch (node.type) {
case NodeTypes.ELEMENT:
return genElement(node)
case NodeTypes.TEXT:
return genText(node)
default:
return ''
}
}

const genElement = (el: ElementNode): string => {
return `h("${el.tag}", {${el.props
.map(({ name, value }) => `${name}: "${value?.content}"`)
.join(', ')}}, [${el.children.map(it => genNode(it)).join(', ')}])`
}

const genText = (text: TextNode): string => {
return `\`${text.content}\``
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { generate } from './codegen'
import { baseParse } from './parse'

export function baseCompile(template: string) {
const parseResult = baseParse(template.trim())
console.log(
'🚀 ~ file: compile.ts:6 ~ baseCompile ~ parseResult:',
parseResult,
)

// TODO: codegen
// const code = generate(parseResult);
// return code;
return ''
const code = generate(parseResult)
return code
}

0 comments on commit bacf38f

Please sign in to comment.