浏览代码

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

Evan You 3 年之前
父节点
当前提交
4e0ab97c70
共有 1 个文件被更改,包括 23 次插入2 次删除
  1. 23 2
      scripts/const-enum.mjs

+ 23 - 2
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)
             }