Explorar o código

build: stricter conditions for UnaryExpression in const enum plugin

Evan You %!s(int64=3) %!d(string=hai) anos
pai
achega
b49b9eff20
Modificáronse 1 ficheiros con 11 adicións e 3 borrados
  1. 11 3
      scripts/const-enum.js

+ 11 - 3
scripts/const-enum.js

@@ -112,9 +112,17 @@ export async function constEnum() {
             }
 
             if (init.type === 'UnaryExpression') {
-              // @ts-ignore assume all operands are literals
-              const exp = `${init.operator}${init.argument.value}`
-              value = evaluate(exp)
+              if (
+                init.argument.type === 'StringLiteral' ||
+                init.argument.type === 'NumericLiteral'
+              ) {
+                const exp = `${init.operator}${init.argument.value}`
+                value = evaluate(exp)
+              } else {
+                throw new Error(
+                  `unhandled UnaryExpression argument type ${init.argument.type} in ${file}`
+                )
+              }
             }
 
             if (value === undefined) {