closure compiler externs for underscore 1.4.4

Closure Compiler externs for underscore 1.4.4 are now available.

Underscore 1.4.4 adds two new functions, _.findWhere, and _.partial.

Also pushed some fixes to 1.4.3 externs:

  • typo in `properties` param for _.where
  • a bunch of methods should be able to operate on Arguments objects as well as Arrays. Previously, only Arrays were noted as valid params by the externs.

Closure Compiler externs updates

Just pushed fixes and updates to my Closer Compiler externs for underscore, and created a repo for Qunit externs (which is probably not of any use to anyone, but is used by for testing accuracy/completeness of underscore externs).

underscore-js-externs

  • Externs for latest underscore (1.4.3).
  • Some fixes to 1.3.3 externs, indicating that more methods may return wrapped objects (this is still incomplete).
  • Tests for externs file completeness/accuracy by running underscore’s own qunit tests through closure compiler. The output (using advanced compilation) is nasty and contains a lot of errors, but most of them are irrelevant. Some do point to legitimate issues, mostly related to uncommon ways of passing arguments to various methods.

qunit-js-externs

  • Externs missing for `throws`, callbacks, and configuration. Mostly complete.

Closure compiler externs files for underscore and backbone js

As part of some other things I’m working on I’ve begun building upon some existing closure compiler externs files for the Underscore and Backbone js libraries. Both are still in quite early stages, but if you need something like this, they’re currently better than nothing.

backbone-js-externs on github, for Backbone 0.9.2

underscore-js-externs on github, for Underscore 1.3.3

The Backbone externs file is taken from this gist, which was autogenerated by DotNetWise’s externs extractor. It does a decent but very basic job.

The Underscore externs file is taken from the official Closure Compiler repository. The only changes I’ve made so far are

  • fixing a type annotation for the _.uniq and _.unique functions that had a required parameter after a non-required one (Closure Compiler dislikes this)
  • using same type annotation for _.uniq and _.unique, since the latter is an alias for the former
  • beginning to add type annotations for functional-style usage of underscore, vs. object notation (e.g. _(collection).map(function(){}) vs. _.map(collection, function() {}). The existing externs file has only the latter.

I’ll probably also be adding externs for use with the SVG library Raphael at some point.

Objects in closure compiler record types

I’ve been diving much deeper into closure compiler’s type checking abilities lately, and while it is a great tool, the documentation could stand to be improved somewhat.

Sometimes when creating a record type, you will want to specify not just that some property is an array, or an object, but what sort of things are in the array, or what sort of properties are on the object.

A simple record type for an object with a string and number property:


/**
 * @typedef {{name: string, index: number}}
 */

A record type with an array of strings:


/**
 * @typedef {{networks: Array.<string>}}
 */

A record type, one of whose properties is an object:


/**
 * @typedef {{authorized:boolean, network_name: string,
 * details: Object.<{id: number, promotion_id: number}>, type: string}}
 */

 

This last one is the syntax I was having trouble coming up with, and it is not immediately obvious from the official documentation here. Hope this might help someone else who’s looking for the same thing.

Also, an example of objects inside an array (using this for a Vimeo video object):


/**
 * @typedef {{urls: {url: Array.<Object.<{type: string, _content: string}>>},
 * thumbnails: {thumbnail: Array.<Object.<{_content: string, width: string, height: string}>>} }}
 * }}
 */

 

You can also refer to other record types inside a record type:


/**
 * @typedef {{name: string, auth_provider: string, id: number}}
 */
MyRecordTypes.authorization;

/**
 * @typedef {{authorizations: Array.<MyRecordTypes.authorization> }}
 */
MyRecordTypes.authorizations;