Search code examples
javascriptgoogle-closure-compiler

Modify builtin extern for closure compiler


I'm working with google's closure compiler and I encountered the following error when building:

path/to/file.js:...: WARNING - actual parameter 1 of Element.prototype.scrollIntoView does not match formal parameter
found   : {behavior: string, block: string}
required: (boolean|undefined)
    target.scrollIntoView({'block': 'start', 'behavior': 'smooth'});
                          ^

Obviously, here target is an Element.

Ok, so it looks like closure wants a boolean, but mdn seems to think that an object is a perfectly fine argument. MDN references https://drafts.csswg.org/cssom-view/#dom-element-scrollintoview for it's definition which agrees (and the smooth scrolling actually works in Firefox).

It looks like Closure defines the extern in w3c_css.js which references http://www.w3.org/TR/cssom-view/#dom-element-scrollintoview in a comment. However, it looks like they don't even have the definition there correct since w3.org has 2 (optional) arguments in the signature and closure seems to only allow passing 1.

Smooth scrolling is a nice-to-have (and I'll likely replace this piece of code with a better cross-browser solution at some point), but in the meantime, how do I silence that warning?


Solution

  • Permanent answer

    Submit a pull request to the github project and fix it for everyone: https://github.com/google/closure-compiler/blob/master/externs/browser/w3c_css.js#L2125 - If you aren't comfortable doing that, I'll do it for you.

    Temporary answer

    Define your own extern overwriting the signature:

    /** @externs */
    
    /**
     * @suppress {duplicate}
     * @param {(boolean|{behavior: string, block: string})=} opt_top
     * @see http://www.w3.org/TR/cssom-view/#dom-element-scrollintoview
     */
    Element.prototype.scrollIntoView = function(opt_top) {};