Skip to content

Instantly share code, notes, and snippets.

@rodyhaddad
Created July 24, 2014 18:16
Show Gist options
  • Save rodyhaddad/57e060cc3100fab8bf53 to your computer and use it in GitHub Desktop.
Save rodyhaddad/57e060cc3100fab8bf53 to your computer and use it in GitHub Desktop.
function test() {
let a = 4;
if (true) {
const x = 1;
var y = 2;
let a = 3;
a = x + y;
}
console.log(a);
if (typeof a === "undefined") {
console.log('a should change to the top-level one', a);
}
if (typeof x === "undefined") {
console.log('should stay as x in the typeof');
}
}
// becomes
function test() {
var $__0;
var $__1;
var $__2;
$__0 = 4;
if (true) {
$__1 = 1;
var y = 2;
$__2 = 3;
$__2 = $__1 + y;
}
console.log($__0);
if (typeof $__0 === "undefined") {
console.log('a should change to the top-level one', $__0);
}
if (typeof x === "undefined") {
console.log('should stay as x in the typeof');
}
}
@arv
Copy link

arv commented Jul 24, 2014

A cleaner output might look like:

// becomes
function test() {
  var a = 4;
  if (true) {
    var $__1 = 1;
    var y = 2;
    var $__2 = 3;
    $__2 = $__1 + y;
  }
  console.log(a);
  if (typeof a === "undefined") {
    console.log('a should change to the top-level one', a);
  }
  if (typeof x === "undefined") {
    console.log('should stay as x in the typeof');
  }
}

Or rename the vars to something that has the original name in to make it easier to reason about

function test() {
  var a = 4;
  if (true) {
    var $__x = 1;
    var y = 2;
    var $__a = 3;
    $__a = $__x + y;
  }
  console.log(a);
  if (typeof a === "undefined") {
    console.log('a should change to the top-level one', a);
  }
  if (typeof x === "undefined") {
    console.log('should stay as x in the typeof');
  }
}

I'm not sure how safe a renaming like that would be. We could do a more sophisticated temp var scheme at some later point. Don't let that block you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment