refactor(compiler-vapor): simplify genExpression

This commit is contained in:
三咲智子 Kevin Deng 2024-03-12 15:50:23 +08:00
parent c79629f0ef
commit 421eba3e01
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
1 changed files with 42 additions and 42 deletions

View File

@ -19,13 +19,11 @@ export function genExpression(
context: CodegenContext, context: CodegenContext,
assignment?: string, assignment?: string,
): CodeFragment[] { ): CodeFragment[] {
const { const { prefixIdentifiers } = context.options
options: { prefixIdentifiers }, const { content, ast, isStatic, loc } = node
} = context
const { content: rawExpr, ast, isStatic, loc } = node
if (isStatic) { if (isStatic) {
return [[JSON.stringify(rawExpr), NewlineType.None, loc]] return [[JSON.stringify(content), NewlineType.None, loc]]
} }
if ( if (
@ -36,16 +34,16 @@ export function genExpression(
ast === false || ast === false ||
isConstantExpression(node) isConstantExpression(node)
) { ) {
return [[rawExpr, NewlineType.None, loc], assignment && ` = ${assignment}`] return [[content, NewlineType.None, loc], assignment && ` = ${assignment}`]
} }
// the expression is a simple identifier // the expression is a simple identifier
if (ast === null) { if (ast === null) {
return genIdentifier(rawExpr, context, loc, assignment) return genIdentifier(content, context, loc, assignment)
} }
const ids: Identifier[] = [] const ids: Identifier[] = []
const parentStackMap = new WeakMap<Identifier, Node[]>() const parentStackMap = new Map<Identifier, Node[]>()
const parentStack: Node[] = [] const parentStack: Node[] = []
walkIdentifiers( walkIdentifiers(
ast!, ast!,
@ -56,20 +54,22 @@ export function genExpression(
false, false,
parentStack, parentStack,
) )
let hasMemberExpression = false let hasMemberExpression = false
if (ids.length) { if (ids.length) {
ids.sort((a, b) => a.start! - b.start!)
const [frag, push] = buildCodeFragment() const [frag, push] = buildCodeFragment()
ids.forEach((id, i) => { ids
.sort((a, b) => a.start! - b.start!)
.forEach((id, i) => {
// range is offset by -1 due to the wrapping parens when parsed // range is offset by -1 due to the wrapping parens when parsed
const start = id.start! - 1 const start = id.start! - 1
const end = id.end! - 1 const end = id.end! - 1
const last = ids[i - 1] const last = ids[i - 1]
const leadingText = rawExpr.slice(last ? last.end! - 1 : 0, start) const leadingText = content.slice(last ? last.end! - 1 : 0, start)
if (leadingText.length) push([leadingText, NewlineType.Unknown]) if (leadingText.length) push([leadingText, NewlineType.Unknown])
const source = rawExpr.slice(start, end) const source = content.slice(start, end)
const parentStack = parentStackMap.get(id)! const parentStack = parentStackMap.get(id)!
const parent = parentStack[parentStack.length - 1] const parent = parentStack[parentStack.length - 1]
@ -94,8 +94,8 @@ export function genExpression(
), ),
) )
if (i === ids.length - 1 && end < rawExpr.length) { if (i === ids.length - 1 && end < content.length) {
push([rawExpr.slice(end), NewlineType.Unknown]) push([content.slice(end), NewlineType.Unknown])
} }
}) })
@ -104,7 +104,7 @@ export function genExpression(
} }
return frag return frag
} else { } else {
return [[rawExpr, NewlineType.Unknown, loc]] return [[content, NewlineType.Unknown, loc]]
} }
} }