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

Commit

Permalink
fix: assignment should calculate the right side first, close #12
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed May 17, 2018
1 parent 67d1dd7 commit 5bdcbdf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,8 @@ const visitors: EvaluateMap = {
AssignmentExpression(path) {
const { node, scope } = path;
let $var: IVar;
// right first
const rightValue = evaluate(path.createChild(node.right));

if (isIdentifier(node.left)) {
const { name } = node.left;
Expand Down Expand Up @@ -1203,7 +1205,7 @@ const visitors: EvaluateMap = {
$var.set($var.value & v);
return $var.value;
}
}[node.operator](evaluate(path.createChild(node.right)));
}[node.operator](rightValue);
},
LogicalExpression(path) {
const { node } = path;
Expand Down
20 changes: 20 additions & 0 deletions test/common/assignment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import test from "ava";
import vm from "../../src/vm";

test("Assignment should calculate the right expression first", t => {
const sandbox: any = vm.createContext({});

try {
vm.runInContext(
`
const a = 123;
a = b
`,
sandbox
);
t.fail("it should throw an error");
} catch (err) {
t.deepEqual(err.message, "b is not defined");
}
});

0 comments on commit 5bdcbdf

Please sign in to comment.