wip: test for transformExpression

This commit is contained in:
Evan You
2019-09-23 21:22:52 -04:00
parent acfa7bd46e
commit 4a82e7cdbc
3 changed files with 110 additions and 20 deletions

View File

@@ -294,17 +294,18 @@ function genExpressionAsPropertyKey(
node: ExpressionNode,
context: CodegenContext
) {
if (node.children) {
return genCompoundExpression(node, context)
}
if (node.isStatic) {
const { push } = context
const { content, children, isStatic } = node
if (children) {
push(`[`)
genCompoundExpression(node, context)
push(`]`)
} else if (isStatic) {
// only quote keys if necessary
const text = /^\d|[^\w]/.test(node.content)
? JSON.stringify(node.content)
: node.content
context.push(text, node)
const text = /^\d|[^\w]/.test(content) ? JSON.stringify(content) : content
push(text, node)
} else {
context.push(`[${node.content}]`, node)
push(`[${content}]`, node)
}
}

View File

@@ -74,11 +74,13 @@ export function processExpression(
walk(ast, {
enter(node, parent) {
if (node.type === 'Identifier') {
if (ids.indexOf(node) === -1) {
if (
ids.indexOf(node) === -1 &&
!knownIds[node.name] &&
shouldPrefix(node, parent)
) {
node.name = `_ctx.${node.name}`
ids.push(node)
if (!knownIds[node.name] && shouldPrefix(node, parent)) {
node.name = `_ctx.${node.name}`
}
}
} else if (isFunction(node)) {
node.params.forEach(p =>
@@ -123,11 +125,13 @@ export function processExpression(
})
)
if (i === ids.length - 1 && id.end < full.length - 1) {
children.push(full.slice(id.end))
children.push(full.slice(id.end - 1))
}
})
node.children = children
if (children.length) {
node.children = children
}
}
const globals = new Set(