wip: expression rewrite

This commit is contained in:
Evan You
2019-09-23 02:52:54 -04:00
parent 70656690e2
commit bb8524e199
10 changed files with 224 additions and 23 deletions

View File

@@ -119,7 +119,7 @@ export function generate(
options: CodegenOptions = {}
): CodegenResult {
const context = createCodegenContext(ast, options)
const { mode, push, useWith, indent, deindent } = context
const { mode, push, useWith, indent, deindent, newline } = context
const imports = ast.imports.join(', ')
if (mode === 'function') {
// generate const declarations for helpers
@@ -135,13 +135,19 @@ export function generate(
push(`export default `)
}
push(`function render() {`)
// generate asset resolution statements
ast.statements.forEach(s => push(s + `\n`))
if (useWith) {
indent()
push(`with (this) {`)
}
indent()
// generate asset resolution statements
if (ast.statements.length) {
ast.statements.forEach(s => {
push(s)
newline()
})
newline()
}
if (useWith) {
push(`with (this) {`)
indent()
}
push(`return `)
genChildren(ast.children, context)
if (useWith) {
@@ -236,6 +242,10 @@ function genNode(node: CodegenNode, context: CodegenContext) {
break
case NodeTypes.JS_ARRAY_EXPRESSION:
genArrayExpression(node, context)
break
default:
__DEV__ &&
assert(false, `unhandled codegen node type: ${(node as any).type}`)
}
}
@@ -254,9 +264,9 @@ function genText(node: TextNode | ExpressionNode, context: CodegenContext) {
}
function genExpression(node: ExpressionNode, context: CodegenContext) {
// if (node.codegenNode) {
// TODO handle transformed expression
// }
if (node.children) {
return genCompoundExpression(node, context)
}
const text = node.isStatic ? JSON.stringify(node.content) : node.content
context.push(text, node)
}
@@ -265,9 +275,9 @@ function genExpressionAsPropertyKey(
node: ExpressionNode,
context: CodegenContext
) {
// if (node.codegenNode) {
// TODO handle transformed expression
// }
if (node.children) {
return genCompoundExpression(node, context)
}
if (node.isStatic) {
// only quote keys if necessary
const text = /^\d|[^\w]/.test(node.content)
@@ -279,6 +289,17 @@ function genExpressionAsPropertyKey(
}
}
function genCompoundExpression(node: ExpressionNode, context: CodegenContext) {
for (let i = 0; i < node.children!.length; i++) {
const child = node.children![i]
if (isString(child)) {
context.push(child)
} else {
genExpression(child, context)
}
}
}
function genComment(node: CommentNode, context: CodegenContext) {
context.push(`<!--${node.content}-->`, node)
}