-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jindy
committed
Jun 21, 2022
1 parent
750af23
commit f6caae3
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export function generate(ast) { | ||
// let code = "" | ||
// code += "return " | ||
const context = createCodegenContext() | ||
const { push } = context | ||
push("return ") | ||
const functionName = "render" | ||
const args = ["_ctx", "_cache"] | ||
const signature = args.join(",") | ||
// code += `function ${functionName}(${signature}){` | ||
push(`function ${functionName}(${signature}){`) | ||
|
||
// const node = ast.codegenNode | ||
// code += `return '${node.content}'` | ||
// code += `return` | ||
push(`return`) | ||
genNode(ast.codegenNode, context) | ||
// code += `}` | ||
push(`}`) | ||
return { | ||
code: context.code, | ||
} | ||
} | ||
|
||
function createCodegenContext(): any { | ||
const context = { | ||
code: "", | ||
push(source) { | ||
context.code += source | ||
}, | ||
} | ||
return context | ||
} | ||
|
||
function genNode(node, context) { | ||
const { push } = context | ||
// const node = ast.codegenNode | ||
push(` '${node.content}'`) | ||
// code += `return '${node.content}'` | ||
// return code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`codegen string 1`] = `"return function render(_ctx,_cache){return 'hi'}"`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { generate } from "../src/codegen" | ||
import { baseParse } from "../src/parse" | ||
import { transform } from "../src/transform" | ||
|
||
describe("codegen", () => { | ||
it("string", () => { | ||
const ast = baseParse("hi") | ||
transform(ast) | ||
const { code } = generate(ast) | ||
// 快照测试 | ||
expect(code).toMatchSnapshot() | ||
}) | ||
}) |