build: fix handling of const enum that rely on previous values

This commit is contained in:
Evan You 2023-02-03 16:26:56 +08:00
parent eb0c1e7025
commit 4e0ab97c70
1 changed files with 23 additions and 2 deletions

View File

@ -85,8 +85,29 @@ export async function constEnum() {
// e.g. 1 << 2
if (init.type === 'BinaryExpression') {
// @ts-ignore assume all operands are literals
const exp = `${init.left.value}${init.operator}${init.right.value}`
const resolveValue = node => {
if (
node.type === 'NumericLiteral' ||
node.type === 'StringLiteral'
) {
return node.value
} else if (node.type === 'MemberExpression') {
const exp = content.slice(node.start, node.end)
if (!(exp in enumData.defines)) {
throw new Error(
`unhandled enum initialization expression ${exp} in ${file}`
)
}
return enumData.defines[exp]
} else {
throw new Error(
`unhandled BinaryExpression operand type ${node.type} in ${file}`
)
}
}
const exp = `${resolveValue(init.left)}${
init.operator
}${resolveValue(init.right)}`
value = evaluate(exp)
}