Search code examples
javascriptweb-audio-apigoogle-closure-compiler

Google Closure Compiler gives JSC_INEXISTENT_PROPERTY for new Web Audio API methods


Question

How can I make the Google Closure Compiler recognize new methods of an already supported API (in this case, the Web Audio API) or at least prevent it from renaming them (preferably using annotations in my code)?

Problem

I am using methods in AudioContext that are not recognized by the Google Closure Compiler (yet). For instance AudioContext.suspend and AudioContext.resume:

var ctx = new AudioContext;
ctx.suspend();
...
ctx.resume();

The closure compiler gives me "JSC_INEXISTENT_PROPERTY: Property X never defined on AudioContext...", and in some places it goes ahead and renames the method names to something short. For instance, the above code becomes:

var v=new AudioContext;v.a(); ... ;v.resume();

Here, v.a() should have been v.suspend(), so the generated code obviously doesn't work.

The root cause is that the Web Audio API definition in the closure compiler is not up to date, but rather than patching the closure compiler I would like to find a more generic workaround.


Solution

  • You should at least open an Issue or a even better, Pull request.

    To solve your problem without patching, you can add an extern file with only the definition of theses functions, it should work:

    AudioContext_fix_externs.js

    /**
     * @return {Promise}
     * @see https://developer.mozilla.org/fr/docs/Web/API/AudioContext/suspend
     */
    AudioContext.prototype.suspend = function(){};
    

    To use that extern file:

    java -jar compiler.jar --externs AudioContext_fix_externs.js ...
    

    https://developers.google.com/closure/compiler/docs/api-tutorial3#howto-app

    To open an issue : https://github.com/google/closure-compiler/issues/new