chore(doc-gen): include type parameters in d.ts file

Closes #2859
This commit is contained in:
Peter Bacon Darwin 2015-07-03 14:49:00 +01:00 committed by Alex Eagle
parent e5405e4ba2
commit 72257ec87d
4 changed files with 55 additions and 25 deletions

View File

@ -1,5 +1,5 @@
export interface MyInterface {
optionalProperty? : string
(param: string) : string
<T, U extends Findable<T>>(param: T) : U
new (param: number) : MyInterface
}

View File

@ -202,7 +202,22 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
location: getLocation(memberSymbol)
};
if (memberSymbol.flags & (ts.SymbolFlags.Method | ts.SymbolFlags.Signature)) {
memberDoc.typeParameters = getTypeParameters(typeChecker, memberSymbol);
if(memberSymbol.flags & (ts.SymbolFlags.Signature) ) {
memberDoc.parameters = getParameters(typeChecker, memberSymbol);
memberDoc.returnType = getReturnType(typeChecker, memberSymbol);
switch(memberDoc.name) {
case '__call':
memberDoc.name = '';
break;
case '__new':
memberDoc.name = 'new';
break;
}
}
if (memberSymbol.flags & ts.SymbolFlags.Method) {
// NOTE: we use the property name `parameters` here so we don't conflict
// with the `params` property that will be updated by dgeni reading the
// `@param` tags from the docs
@ -214,7 +229,7 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
memberDoc.name = 'constructor';
}
if(memberSymbol.flags & (ts.SymbolFlags.Value | ts.SymbolFlags.Signature) ) {
if(memberSymbol.flags & ts.SymbolFlags.Value) {
memberDoc.returnType = getReturnType(typeChecker, memberSymbol);
}
@ -248,6 +263,16 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
});
}
function getTypeParameters(typeChecker, symbol) {
var declaration = symbol.valueDeclaration || symbol.declarations[0];
var sourceFile = ts.getSourceFileOfNode(declaration);
if (!declaration.typeParameters) return;
var typeParams = declaration.typeParameters.map(function(type) {
return getText(sourceFile, type).trim();
});
return typeParams;
}
function getReturnType(typeChecker, symbol) {
var declaration = symbol.valueDeclaration || symbol.declarations[0];
var sourceFile = ts.getSourceFileOfNode(declaration);

View File

@ -67,12 +67,14 @@ describe('readTypeScriptModules', function() {
var moduleDoc = docs[0];
var exportedInterface = moduleDoc.exports[0];
expect(exportedInterface.callMember).toBeDefined();
expect(exportedInterface.callMember.parameters).toBeDefined();
expect(exportedInterface.callMember.returnType).toBeDefined();
expect(exportedInterface.callMember.parameters).toEqual(['param: T']);
expect(exportedInterface.callMember.returnType).toEqual('U');
expect(exportedInterface.callMember.typeParameters).toEqual(['T', 'U extends Findable<T>']);
expect(exportedInterface.newMember).toBeDefined();
expect(exportedInterface.newMember.parameters).toBeDefined();
expect(exportedInterface.newMember.returnType).toBeDefined();
expect(exportedInterface.newMember.parameters).toEqual(['param: number']);
expect(exportedInterface.newMember.returnType).toEqual('MyInterface');
});
});

View File

@ -1,3 +1,4 @@
{%- macro commentBlock(doc, level) -%}
{%- if doc.content | trim %}
@ -5,6 +6,19 @@
{$ doc.content | trim | replace(r/^/gm, "* ") | indent(level, true) | replace(r/\n$/, "") $}
{$ '*/' | indent(level, true) | replace(r/\n$/, "") $}{% endif -%}
{%- endmacro -%}
{%- macro memberInfo(member) -%}
{$ commentBlock(member, 5) $}
{$ member.name $}{% if member.optional %}?{% endif -%}
{% if member.typeParameters %}<{% for typeParam in member.typeParameters %}{$ typeParam $}{% if not loop.last %}, {% endif %}{% endfor %}>{% endif -%}
{%- if member.parameters -%}({% for param in member.parameters %}{$ param $}{% if not loop.last %}, {% endif %}{% endfor %}){%- endif -%}
{%- if member.returnType -%}
: {$ member.returnType $}{%- else -%}: void
{%- endif -%};
{%- endmacro -%}
// Type definitions for Angular v{$ versionInfo.currentVersion.full | replace(r/\+/, "_") $}
// Project: http://angular.io/
// Definitions by: angular team <https://github.com/angular/>
@ -26,25 +40,14 @@ declare module "{$ module.id $}" {
{%- endif %}
{$ export.docType $} {$ export.name $}{$ export.typeParams $}{$ export.heritage $}
{%- if export.docType == 'class' or export.docType == 'interface' %} {
{%- if export.newMember -%}
{$ commentBlock(export.newMember, 5) $}
new ({% for param in export.newMember.parameters %}{$ param $}{% if not loop.last %}, {% endif %}{% endfor %})
{%- if export.newMember.returnType %}: {$ export.newMember.returnType $}{%- else -%}: any{% endif -%}
;
{%- if export.newMember %}
{$ memberInfo(export.newMember) $}
{% endif %}
{%- if export.callMember -%}
{$ commentBlock(export.callMember, 5) $}
({% for param in export.callMember.parameters %}{$ param $}{% if not loop.last %}, {% endif %}{% endfor %})
{%- if export.callMember.returnType %}: {$ export.callMember.returnType $}{%- else -%}: any{% endif -%}
;
{% endif %}
{%- for member in export.members -%}
{$ commentBlock(member, 5) $}
{$ member.name $}
{%- if member.optional %}?{% endif -%}
{%- if member.parameters %}({% for param in member.parameters %}{$ param $}{% if not loop.last %}, {% endif %}{% endfor %}){%- endif %}
{%- if member.returnType %}: {$ member.returnType $}{%- else -%}: void{% endif -%}
;
{%- if export.callMember %}
{$ memberInfo(export.callMember) $}
{% endif -%}
{%- for member in export.members %}
{$ memberInfo(member) $}
{%- endfor %}
}