Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Commit

Permalink
feat: support ConditionalExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 4, 2018
1 parent 36e5b7b commit d85afbd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
30 changes: 30 additions & 0 deletions test/ConditionalExpression.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit d85afbd

Please sign in to comment.