Skip to content

Commit

Permalink
Prepend 'var ' for root symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
ahocevar committed Aug 9, 2014
1 parent 9780e9f commit 63abbe2
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions tasks/generate-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,23 @@ function generateExterns(typedefs, symbols, externs) {
var partialNamespace = namespace.join('.');
if (!(partialNamespace in namespaces ||
partialNamespace in constructors)) {
namespaces[partialNamespace] = true;
lines.push('/**');
lines.push(' * @type {Object}');
lines.push(' */');
lines.push(
(namespace.length == 1 ? 'var ' : '') + partialNamespace + ';');
lines.push(nameToJS(partialNamespace) + ';');
lines.push('\n');
}
});
}

function nameToJS(name) {
if (name.indexOf('.') == -1) {
namespaces[name] = true;
name = 'var ' + name;
}
return name;
}

function processSymbol(symbol) {
addNamespaces(symbol.name.split('#')[0]);

Expand All @@ -71,7 +77,7 @@ function generateExterns(typedefs, symbols, externs) {
lines.push('/**');
lines.push(' * @constructor');
lines.push(' */');
lines.push(constructor + ' = function() {};');
lines.push(nameToJS(constructor) + ' = function() {};');
lines.push('\n');
}
}
Expand Down Expand Up @@ -103,23 +109,24 @@ function generateExterns(typedefs, symbols, externs) {
}
lines.push(' */');
if (symbol.kind == 'function' || symbol.kind == 'class') {
lines.push(name + ' = function(' + args.join(', ') + ') {};');
lines.push(nameToJS(name) + ' = function(' + args.join(', ') + ') {};');
} else {
lines.push(name + ';');
lines.push(nameToJS(name) + ';');
}
lines.push('\n');
}

externs.forEach(processSymbol);

typedefs.forEach(function(typedef) {
addNamespaces(typedef.name);
lines.push('/**');
lines.push(' * @typedef {' + typedef.types.join('|') + '}');
lines.push(' */');
lines.push(typedef.name + ';');
lines.push(nameToJS(typedef.name) + ';');
lines.push('\n');
});

externs.forEach(processSymbol);
symbols.forEach(processSymbol);

return lines.join('\n');
Expand Down

0 comments on commit 63abbe2

Please sign in to comment.