javascriptgoogle-closure-compilergoogle-closure-templates

Pre-compiled Closure Templates - "Variable referenced before declaration" Warning in Closure Compiler


java -jar SoyToJsSrcCompiler.jar --shouldGenerateJsdoc --outputPathFormat 
           simple.js --srcs simple.soy

SoyToJsSrcCompiler generates a js file which looks like this:

if (typeof templates == 'undefined') { var templates = {}; }
if (typeof templates.simple == 'undefined') { templates.simple = {}; }

/**
 * @param {Object.<string, *>=} opt_data
 * @param {(null|undefined)=} opt_ignored
 * @return {string}
 * @notypecheck
 */

 templates.simple.tinyButton = function(opt_data, opt_ignored) {
     .....
 };

I am using Closure Compiler with --warning_level=VERBOSE and --compilation_level ADVANCED_OPTIMIZATIONS

and I am getting this warning:

simple.js:1: WARNING - Variable referenced before declaration: templates
if (typeof templates == 'undefined') { var templates = {}; }

How can I clear this warning?


Solution

  • One workaround is to declare the variables in an externs file with:

    /** @suppress {duplicate} */
    var template;
    

    But the Soy compiler should be fixed. I expect people don't see this because you typically use it with Closure Library and in that mode the Soy compiler should be generating:

    goog.provide('template.simple')