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

strange dead code elimination output #895

Open
bronze1man opened this issue Aug 14, 2018 · 1 comment
Open

strange dead code elimination output #895

bronze1man opened this issue Aug 14, 2018 · 1 comment

Comments

@bronze1man
Copy link

I have all done the test with http://babeljs.io/repl it says babel 6.x

input :

(function(){
  // not call function, should delete.
  function nfn1(){
    console.log("not use1");
  }
  // not use object ,should delete.
  var nobj = {};
  // not call function, should delete.
  nobj.m = function(){
    console.log("not use2");
  }
  var flag = false;
  if (flag==true){
  	console.log("not use3");
  }
  function kNew(){
    return {};
  }
  var gObj = null;
  function kGetDefault(){
    if (gObj === null){
		gObj = kNew();
	}
	return gObj;
  }
  function kSetDefault(ke){
    gObj = ke;
  }
  // /////////
  // should remain test:
  function uobj(){
    console.log("use 3");
  }
  // export api,must in the source code.
  window["abc"] = function(){
    console.log("use 1");
  }
  // direct run code, must in the output code
  console.log("use 2");
  uobj();

})();

output:

(function() {
  var h = null;
  (window.abc = function() {
    console.log("use 1");
  }),
    console.log("use 2"),
    (function() {
      console.log("use 3");
    })();
})();

What is the var h = null; stuff?

@bardiharborow
Copy link
Contributor

Reduced Test Case

Input:

(function(){
  var gObj = null;
  function kGetDefault() {
    if (gObj === null) {
      gObj = kNew();
    }
  }
})();

Output:

(function(){var b=null})();

There's no specification violation here, but the gObj variable could be optimized away as an enhancement (probably extra dead-code passes would be needed).

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

2 participants