Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

var is not hoisted to the nearest variable scope #4123

Open
bradzacher opened this issue Sep 14, 2023 · 1 comment
Open

var is not hoisted to the nearest variable scope #4123

bradzacher opened this issue Sep 14, 2023 · 1 comment

Comments

@bradzacher
Copy link

input code:

function foo(arg) {
  switch(arg.type){
      case 'foo':
          var _arg_foo;
          (_arg_foo = arg.foo) === null || _arg_foo === void 0 ? void 0 : _arg_foo.foo();
          break;
      case 'bar':
          var _arg_foo1;
          (_arg_foo1 = arg.foo) === null || _arg_foo1 === void 0 ? void 0 : _arg_foo1.bar();
          break;
      case 'baz':
          var _arg_foo2;
          (_arg_foo2 = arg.foo) === null || _arg_foo2 === void 0 ? void 0 : _arg_foo2.baz();
          break;
  }
}
console.log(foo);

Note: input code is the generated by swc downlevelling optional chaining

Actual CC output:

console.log(function(a) {
  switch(a.type) {
    case "foo":
      var b;
      null === (b = a.g) || void 0 === b ? void 0 : b.g();
      break;
    case "bar":
      var c;
      null === (c = a.g) || void 0 === c ? void 0 : c.h();
      break;
    case "baz":
      var d;
      null === (d = a.g) || void 0 === d ? void 0 : d.i();
  }
});

Expected CC output:

console.log(function(a) {
  var b,c,d;
  switch(a.type) {
    case "foo":
      null === (b = a.g) || void 0 === b ? void 0 : b.g();
      break;
    case "bar":
      null === (c = a.g) || void 0 === c ? void 0 : c.h();
      break;
    case "baz":
      null === (d = a.g) || void 0 === d ? void 0 : d.i();
  }
});

CC does not hoist the vars to the nearest variable scope, even though (as I understand it) it should be runtime equivalent to do so.
If it did hoist the variables then it could collapse them into a single declaration and save the additional var per variable - which would leads to +4b per variable declared within the same variable scope.

@bradzacher
Copy link
Author

It's worth noting that currently CC doesn't even reorder variable declarations when they're within the same scope:

Input

function foo(arg) {
  var _arg_foo;
  (_arg_foo = arg.foo) === null || _arg_foo === void 0 ? void 0 : _arg_foo.foo();
  var _arg_foo1;
  (_arg_foo1 = arg.foo) === null || _arg_foo1 === void 0 ? void 0 : _arg_foo1.bar();
  var _arg_foo2;
  (_arg_foo2 = arg.foo) === null || _arg_foo2 === void 0 ? void 0 : _arg_foo2.baz();
}
console.log(foo);

Actual CC Output

console.log(function(a) {
  var b;
  null === (b = a.g) || void 0 === b ? void 0 : b.g();
  var c;
  null === (c = a.g) || void 0 === c ? void 0 : c.h();
  var d;
  null === (d = a.g) || void 0 === d ? void 0 : d.i();
});

Expected CC Output

console.log(function(a) {
  var b,c,d;
  null === (b = a.g) || void 0 === b ? void 0 : b.g();
  null === (c = a.g) || void 0 === c ? void 0 : c.h();
  null === (d = a.g) || void 0 === d ? void 0 : d.i();
});

cc service playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant