diff --git a/src/evaluate.ts b/src/evaluate.ts index 8d6c281a..7de5f27a 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -465,6 +465,11 @@ const evaluate_map = { "||": () => evaluate(node.left, scope) || evaluate(node.right, scope), "&&": () => evaluate(node.left, scope) && evaluate(node.right, scope) }[node.operator](); + }, + ConditionalExpression: (node: types.ConditionalExpression, scope: Scope) => { + return evaluate(node.test, scope) + ? evaluate(node.consequent, scope) + : evaluate(node.alternate, scope); } }; diff --git a/test/ConditionalExpression.test.ts b/test/ConditionalExpression.test.ts new file mode 100644 index 00000000..46134d09 --- /dev/null +++ b/test/ConditionalExpression.test.ts @@ -0,0 +1,30 @@ +import test from "ava"; +import * as fs from "fs"; + +import vm from "../src/vm"; + +test("ConditionalExpression-1", t => { + const sandbox: any = vm.createContext({}); + + const num: any = vm.runInContext( + ` +module.exports = true ? 1 : 2; + `, + sandbox + ); + + t.deepEqual(num, 1); +}); + +test("ConditionalExpression-or-2", t => { + const sandbox: any = vm.createContext({}); + + const num: any = vm.runInContext( + ` +module.exports = false ? 1 : 2; + `, + sandbox + ); + + t.deepEqual(num, 2); +});