Build externs file from info.json

This change adds all information that is needed to generate the externs
file to build/info.json, so tasks/generate-externs.js no longer needs
to spawn JSDoc.
This commit is contained in:
Andreas Hocevar
2014-08-01 17:50:59 +02:00
parent 0d86e4a237
commit 1e79acac20
8 changed files with 180 additions and 274 deletions
-16
View File
@@ -1,16 +0,0 @@
{
"opts": {
"recurse": true,
"template": "buildcfg/jsdoc/externs"
},
"tags": {
"allowUnknownTags": true
},
"source": {
"includePattern": "\\.js$"
},
"plugins": [
"buildcfg/jsdoc/api-plugin",
"buildcfg/jsdoc/define-plugin"
]
}
-130
View File
@@ -1,130 +0,0 @@
/**
* @fileoverview Generates a closure compiler externs file for exportable
* symbols (those with an api tag) and boolean defines (with a define tag and a
* default value).
*/
var assert = require('assert');
var fs = require('fs');
var path = require('path');
/**
* Publish hook for the JSDoc template. Writes to stdout.
* @param {function} data The root of the Taffy DB containing doclet records.
* @param {Object} opts Options.
*/
exports.publish = function(data, opts) {
// get all doclets with the "api" property or define (excluding enums,
// typedefs and events)
var docs = data(
[{define: {isObject: true}}, {api: {isString: true}}],
{isEnum: {'!is': true}},
{kind: {'!is': 'typedef'}},
{kind: {'!is': 'event'}}).get();
// get symbols data, filter out those that are members of private classes
var output = [];
var namespaces = {};
var constructors = {};
docs.filter(function(doc) {
var include = true;
var constructor = doc.memberof;
if (constructor && constructor.substr(-1) === '_') {
assert.strictEqual(doc.inherited, true,
'Unexpected export on private class: ' + doc.longname);
include = false;
}
return include;
}).forEach(function(doc) {
var parts = doc.longname.split('#')[0].split('.');
parts.pop();
var namespace = [];
parts.forEach(function(part) {
namespace.push(part);
var partialNamespace = namespace.join('.');
if (!(partialNamespace in namespaces)) {
namespaces[partialNamespace] = true;
output.push('/**');
output.push(' * @type {Object}');
output.push(' */');
output.push(
(namespace.length == 1 ? 'var ' : '') + partialNamespace + ';');
output.push('');
}
});
var signature = doc.longname;
if (signature.indexOf('#') > 0) {
signature = doc.longname.replace('#', '.prototype.');
var constructor = doc.longname.split('#')[0];
if (!(constructor in constructors)) {
constructors[constructor] = true;
output.push('/**');
output.push(' * @constructor');
output.push(' */');
output.push(constructor + ' = function() {}');
output.push('');
}
}
output.push('/**');
if (doc.define) {
output.push(' * @define');
output.push(' * @type {boolean}');
output.push(' */');
output.push(doc.longname + ';');
} else {
if (doc.kind == 'class') {
output.push(' * @constructor');
}
if (doc.type) {
var types = [];
doc.type.names.forEach(function(name) {
types.push(name);
});
output.push(' * @type {' + types.join('|') + '}');
}
var args = [];
if (doc.params) {
doc.params.forEach(function(param) {
args.push(param.name);
var names = [];
param.type.names.forEach(function(name) {
names.push(name);
});
output.push(' * @param {' +
(param.variable ? '...' : '') +
names.join('|') +
(param.optional ? '=' : '') +
'} ' + param.name);
});
}
if (doc.returns) {
var returnTypes = [];
doc.returns[0].type.names.forEach(function(name) {
returnTypes.push(name);
});
output.push(' * @return {' + returnTypes.join('|') + '}');
}
if (doc.tags) {
doc.tags.forEach(function(tag) {
if (tag.title == 'template') {
output.push(' * @' + tag.title + ' ' + tag.value);
}
});
}
output.push(' */');
if (doc.kind == 'function' || doc.kind == 'class') {
output.push(signature + ' = function(' + args.join(', ') + ') {};');
} else {
output.push(signature);
}
}
output.push('');
});
process.stdout.write(output.join('\n'));
};
+2 -2
View File
@@ -10,7 +10,7 @@
"includePattern": "\\.js$"
},
"plugins": [
"buildcfg/jsdoc/api-plugin",
"buildcfg/jsdoc/define-plugin"
"buildcfg/jsdoc/info/api-plugin",
"buildcfg/jsdoc/info/define-plugin"
]
}
+48 -2
View File
@@ -44,12 +44,58 @@ exports.publish = function(data, opts) {
default: doc.define.default
});
} else {
symbols.push({
var types;
var symbol = {
name: doc.longname,
kind: doc.kind,
description: doc.classdesc || doc.description,
path: path.join(doc.meta.path, doc.meta.filename)
});
};
if (doc.type) {
var types = [];
doc.type.names.forEach(function(name) {
types.push(name);
});
symbol.types = types;
}
if (doc.params) {
var params = [];
doc.params.forEach(function(param) {
var paramInfo = {
name: param.name
};
params.push(paramInfo);
var types = [];
param.type.names.forEach(function(name) {
types.push(name);
});
paramInfo.types = types;
if (typeof param.variable == 'boolean') {
paramInfo.variable = param.variable;
}
if (typeof param.optional == 'boolean') {
paramInfo.optional = param.optional;
}
});
symbol.params = params;
}
if (doc.returns) {
var returns = [];
doc.returns[0].type.names.forEach(function(name) {
returns.push(name);
});
symbol.returns = returns;
}
if (doc.tags) {
doc.tags.every(function(tag) {
if (tag.title == 'template') {
symbol.template = tag.value;
return false;
}
return true;
});
}
symbols.push(symbol);
}
});