From 4e0ab97c70986957a18231946efb58193ebb40cc Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 3 Feb 2023 16:26:56 +0800 Subject: [PATCH] build: fix handling of const enum that rely on previous values --- scripts/const-enum.mjs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/scripts/const-enum.mjs b/scripts/const-enum.mjs index 5942b795c..133b6da18 100644 --- a/scripts/const-enum.mjs +++ b/scripts/const-enum.mjs @@ -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) }