mirror of https://github.com/vuejs/core.git
refactor(compiler-vapor): simplify genExpression
This commit is contained in:
parent
c79629f0ef
commit
421eba3e01
|
@ -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,55 +54,57 @@ 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
|
||||||
// range is offset by -1 due to the wrapping parens when parsed
|
.sort((a, b) => a.start! - b.start!)
|
||||||
const start = id.start! - 1
|
.forEach((id, i) => {
|
||||||
const end = id.end! - 1
|
// range is offset by -1 due to the wrapping parens when parsed
|
||||||
const last = ids[i - 1]
|
const start = id.start! - 1
|
||||||
|
const end = id.end! - 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]
|
||||||
|
|
||||||
hasMemberExpression ||=
|
hasMemberExpression ||=
|
||||||
parent &&
|
parent &&
|
||||||
(parent.type === 'MemberExpression' ||
|
(parent.type === 'MemberExpression' ||
|
||||||
parent.type === 'OptionalMemberExpression')
|
parent.type === 'OptionalMemberExpression')
|
||||||
|
|
||||||
push(
|
push(
|
||||||
...genIdentifier(
|
...genIdentifier(
|
||||||
source,
|
|
||||||
context,
|
|
||||||
{
|
|
||||||
start: advancePositionWithClone(node.loc.start, source, start),
|
|
||||||
end: advancePositionWithClone(node.loc.start, source, end),
|
|
||||||
source,
|
source,
|
||||||
},
|
context,
|
||||||
hasMemberExpression ? undefined : assignment,
|
{
|
||||||
id,
|
start: advancePositionWithClone(node.loc.start, source, start),
|
||||||
parent,
|
end: advancePositionWithClone(node.loc.start, source, end),
|
||||||
parentStack,
|
source,
|
||||||
),
|
},
|
||||||
)
|
hasMemberExpression ? undefined : assignment,
|
||||||
|
id,
|
||||||
|
parent,
|
||||||
|
parentStack,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
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])
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (assignment && hasMemberExpression) {
|
if (assignment && hasMemberExpression) {
|
||||||
push(` = ${assignment}`)
|
push(` = ${assignment}`)
|
||||||
}
|
}
|
||||||
return frag
|
return frag
|
||||||
} else {
|
} else {
|
||||||
return [[rawExpr, NewlineType.Unknown, loc]]
|
return [[content, NewlineType.Unknown, loc]]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue