Skip to content

extra trailing commas generated that would break IE8; get closure-compiler clean? #318

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

Closed
glycerine opened this issue Oct 6, 2015 · 3 comments

Comments

@glycerine
Copy link

google's closure javascript compiler (https://developers.google.com/closure/compiler/) detects some extra trailing commas in the javascript generated by gopherjs.

I tried closure out on Dmitri's live-refreshing markdown code; http://dmitri.shuralyov.com/projects/live-markdown/markdown.go. (Btw this is a very cool project.)

After generating markdown.js from gopherjs, then I run the closure compiler on it. There are a bunch of warnings about unreachable code which I ignored, but 2 errors look like they could be problematic, and result in closure compiler not producing output.

Aside: it looks like closure -O ADVANCED will compress a 3MB gopherjs generated javascript file down to 1.6MB (minified and dead-code elimination done); once I manually removed the commas--nice to know.

run:

bash-3.2$ java -jar ./build/compiler.jar < markdown.js > markdown-minified.js
The compiler is waiting for input via stdin.
stdin:320: ERROR - Parse error. IE8 (and below) will parse trailing commas in array and object literals incorrectly. If you are targeting newer versions of JS, set the appropriate language_in option.
          set: function(value) { obj[fieldProp] = value; },
                                                          ^

stdin:1528: ERROR - Parse error. IE8 (and below) will parse trailing commas in array and object literals incorrectly. If you are targeting newer versions of JS, set the appropriate language_in option.
    },
     ^

2 error(s), 0 warning(s)
bash-3.2$ 

the errors detected are here:

var $pointerOfStructConversion = function(obj, type) {
  if(obj.$proxies === undefined) {
    obj.$proxies = {};
    obj.$proxies[obj.constructor.string] = obj;
  }
  var proxy = obj.$proxies[type.string];
  if (proxy === undefined) {
    var properties = {};
    for (var i = 0; i < type.elem.fields.length; i++) {
      (function(fieldProp) {
        properties[fieldProp] = {
          get: function() { return obj[fieldProp]; },
          set: function(value) { obj[fieldProp] = value; },  /////<<< line 320, has extra comma at end
        };
      })(type.elem.fields[i].prop);
    }
    proxy = Object.create(type.prototype, properties);
    proxy.$val = proxy;
    obj.$proxies[type.string] = proxy;
    proxy.$proxies = obj.$proxies;
  }
  return proxy;
};
var $send = function(chan, value) {                                                                                             
  if (chan.$closed) {                                                                                                           
    $throwRuntimeError("send on closed channel");                                                                               
  }                                                                                                                             
  var queuedRecv = chan.$recvQueue.shift();                                                                                     
  if (queuedRecv !== undefined) {                                                                                               
    queuedRecv([value, true]);                                                                                                  
    return;                                                                                                                     
  }                                                                                                                             
  if (chan.$buffer.length < chan.$capacity) {                                                                                   
    chan.$buffer.push(value);                                                                                                   
    return;                                                                                                                     
  }                                                                                                                             

  var thisGoroutine = $curGoroutine;                                                                                            
  chan.$sendQueue.push(function() {                                                                                             
    $schedule(thisGoroutine);                                                                                                   
    return value;                                                                                                               
  });                                                                                                                           
  $block();                                                                                                                     
  return {                                                                                                                      
    $blk: function() {                                                                                                          
      if (chan.$closed) {                                                                                                       
        $throwRuntimeError("send on closed channel");                                                                           
      }                                                                                                                         
    },                             /////<<<< line 1528, extra comma at end                                                                                          
  };                                                                                                                            
}; 
@glycerine
Copy link
Author

resources-- the compiler.jar (built with java 8), the full markdown.js output and the original markdown.go are posted to google drive here:

https://drive.google.com/folderview?id=0BxpncsKbxeGWRlFyWDRteWZhV00&usp=sharing

neelance added a commit that referenced this issue Oct 6, 2015
@neelance
Copy link
Member

neelance commented Oct 6, 2015

@glycerine I've removed those two commas.

GopherJS knows more about the code than the closure compiler can ever know (because of JS' dynamic nature). In theory, the minification that GopherJS can perform is always better than the closure compiler. If you can get the closure compiler to reduce the size of the output without breaking any functionality, then I'll be happy to build such optimization into GopherJS itself. Hint: It's very likely that the closue compiler will break reflection (see #186).

@glycerine
Copy link
Author

@neelance: thanks, and good point about reflection. I don't think there's more to do here that isn't discussed more extensively in #186, so I'll close this.

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