import * as t from "../../../types";

export var metadata = {
  optional: true,
  group: "builtin-prepass"
};

export function AssignmentExpression() {
  var left = this.get("left");
  if (!left.isIdentifier()) return;

  var binding = this.scope.getBinding(left.node.name);
  if (!binding) return;

  var right = this.get("right");
  var evaluated = right.evaluate();
  //if (evaluated.confident) binding.setValue(evaluated.value);
}

export var Scopable = {
  exit() {
    for (var name in this.scope.bindings) {
      var binding = this.scope.bindings[name];
      binding.clearValue();
    }
  }
};

export var Expression = {
  exit() {
    var res = this.evaluate();
    if (res.confident) return t.valueToNode(res.value);
  }
};

@@ -282,16 +282,38 @@
 !binding
+ %7C%7C binding.deoptValue
 ) return
@@ -321,21 +321,25 @@
 %0A%0A  var 
-right
+evaluated
  = this.
@@ -354,33 +354,199 @@
 ht%22)
-;%0A  var evaluated = right
+.evaluate();%0A  if (evaluated.confident) %7B%0A    binding.setValue(evaluated.value);%0A  %7D else %7B%0A    binding.deoptValue();%0A  %7D%0A%7D%0A%0Aexport function IfStatement() %7B%0A  var evaluated = this.get(%22test%22)
 .eva
@@ -560,14 +560,13 @@
 ;%0A  
-//
 if (
+!
 eval
@@ -586,42 +586,131 @@
 nt) 
-binding.setValue(evaluated.value);
+return this.skip();%0A%0A  if (evaluated.value) %7B%0A    this.skipKey(%22alternate%22);%0A  %7D else %7B%0A    this.skipKey(%22consequent%22);%0A  %7D
 %0A%7D%0A%0A

add control flow support and deopt bindings on assignments that we can't understand