Skip to content

Commit 4e0ab97

Browse files
committed
build: fix handling of const enum that rely on previous values
1 parent eb0c1e7 commit 4e0ab97

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

scripts/const-enum.mjs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,29 @@ export async function constEnum() {
8585

8686
// e.g. 1 << 2
8787
if (init.type === 'BinaryExpression') {
88-
// @ts-ignore assume all operands are literals
89-
const exp = `${init.left.value}${init.operator}${init.right.value}`
88+
const resolveValue = node => {
89+
if (
90+
node.type === 'NumericLiteral' ||
91+
node.type === 'StringLiteral'
92+
) {
93+
return node.value
94+
} else if (node.type === 'MemberExpression') {
95+
const exp = content.slice(node.start, node.end)
96+
if (!(exp in enumData.defines)) {
97+
throw new Error(
98+
`unhandled enum initialization expression ${exp} in ${file}`
99+
)
100+
}
101+
return enumData.defines[exp]
102+
} else {
103+
throw new Error(
104+
`unhandled BinaryExpression operand type ${node.type} in ${file}`
105+
)
106+
}
107+
}
108+
const exp = `${resolveValue(init.left)}${
109+
init.operator
110+
}${resolveValue(init.right)}`
90111
value = evaluate(exp)
91112
}
92113

0 commit comments

Comments
 (0)