Skip to content

Commit 561b78a

Browse files
chore(doc-gen): generate router typings file
Closes angular#2659
1 parent a7ea2e5 commit 561b78a

File tree

3 files changed

+94
-42
lines changed

3 files changed

+94
-42
lines changed
Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,77 @@
11
var _ = require('lodash');
2+
var path = require('canonical-path');
23

3-
module.exports = function createTypeDefinitionFile() {
4+
module.exports = function createTypeDefinitionFile(log) {
45

56
return {
67
$runAfter: ['processing-docs'],
78
$runBefore: ['docs-processed'],
9+
$validate: {
10+
dtsPath: { presence: true },
11+
dtsExtension: { presence: true },
12+
typeDefinitions: { presence: true }
13+
},
14+
dtsPath: 'typings',
15+
dtsExtension: '.d.ts',
16+
typeDefinitions: [
17+
{
18+
id: 'angular2/angular2',
19+
modules: {
20+
// The shape of the public API is determined by what is reexported into
21+
// angular2/angular2, with hacks layered into angular2.api.ts
22+
'angular2/angular2': 'angular2/angular2.api',
23+
}
24+
},
25+
{
26+
id: 'angular2/router',
27+
modules: {
28+
'angular2/router': 'angular2/router'
29+
}
30+
}
31+
],
832
$process: function(docs) {
9-
var typeDefDoc = {
10-
id: 'type-definition',
11-
aliases: ['type-definition'],
12-
path: 'type-definition',
13-
outputPath: 'typings/angular2/angular2.d.ts',
14-
modules: []
15-
};
33+
var dtsPath = this.dtsPath;
34+
var dtsExtension = this.dtsExtension;
35+
36+
// For each type definition that we wish to create we define a dgeni "doc" for it
37+
var typeDefDocs = _.map(this.typeDefinitions, function(def) {
38+
39+
var id = def.id + dtsExtension;
40+
var docPath = path.join(dtsPath, id);
41+
42+
return {
43+
docType: 'type-definition',
44+
id: id,
45+
aliases: [id],
46+
path: docPath,
47+
outputPath: docPath,
48+
// A type definition may include a number of top level modules
49+
// And those modules could be aliased (such as 'angular2/angular2.api' -> 'angular2/angular2')
50+
moduleDocs: _.transform(def.modules, function(moduleDocs, id, alias) {
51+
moduleDocs[id] = { id: alias, doc: null };
52+
})
53+
};
54+
});
55+
56+
// Now add all the module docs to their corresponding type definition doc
1657
_.forEach(docs, function(doc) {
17-
// The shape of the public API is determined by what is reexported into
18-
// angular2/angular2, with hacks layered into angular2.api.ts
19-
if (doc.id === 'angular2/angular2.api') {
20-
doc.id = 'angular2/angular2';
21-
typeDefDoc.modules.push(doc);
22-
}
58+
_.forEach(typeDefDocs, function(typeDefDoc) {
59+
if(typeDefDoc.moduleDocs[doc.id]) {
60+
typeDefDoc.moduleDocs[doc.id].doc = doc;
61+
}
62+
});
2363
});
24-
docs.push(typeDefDoc);
64+
65+
_.forEach(typeDefDocs, function(doc) {
66+
_.forEach(doc.moduleDocs, function(modDoc, alias) {
67+
if (!modDoc.doc) {
68+
log.error('createTypeDefinitionFile processor: no such module "' + alias + '" (Did you forget to add it to the modules to load?)');
69+
}
70+
});
71+
});
72+
73+
// Add all the type definition docs to the docs collection so that dgeni can process them
74+
return docs.concat(typeDefDocs);
2575
}
2676
};
2777
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends '../type-definition.template.html' %}
2+
{% block staticDeclarations %}
3+
// Angular depends transitively on these libraries.
4+
// If you don't have them installed you can run
5+
// $ tsd query es6-promise rx rx-lite --action install --save
6+
///<reference path="../es6-promise/es6-promise.d.ts"/>
7+
///<reference path="../rx/rx.d.ts"/>
8+
9+
interface List<T> extends Array<T> {}
10+
interface Map<K,V> {}
11+
interface StringMap<K,V> extends Map<K,V> {}
12+
interface Type {}
13+
14+
declare module "angular2/angular2" {
15+
type SetterFn = typeof Function;
16+
type int = number;
17+
18+
// See https://github.com/Microsoft/TypeScript/issues/1168
19+
class BaseException /* extends Error */ {
20+
message: string;
21+
stack: string;
22+
toString(): string;
23+
}
24+
}
25+
{% endblock %}

docs/dgeni-package/templates/type-definition.template.html

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
{$ doc.content | trim | replace(r/^/gm, "* ") | indent(level, true) | replace(r/\n$/, "") $}
66
{$ '*/' | indent(level, true) | replace(r/\n$/, "") $}{% endif -%}
77
{%- endmacro -%}
8-
98
// Type definitions for Angular v{$ versionInfo.currentVersion.full | replace(r/\+/, "_") $}
109
// Project: http://angular.io/
1110
// Definitions by: angular team <https://github.com/angular/>
@@ -16,34 +15,12 @@
1615
// Please do not create manual edits or send pull requests
1716
// modifying this file.
1817
// ***********************************************************
19-
20-
// Angular depends transitively on these libraries.
21-
// If you don't have them installed you can run
22-
// $ tsd query es6-promise rx rx-lite --action install --save
23-
///<reference path="../es6-promise/es6-promise.d.ts"/>
24-
///<reference path="../rx/rx.d.ts"/>
25-
26-
interface List<T> extends Array<T> {}
27-
interface Map<K,V> {}
28-
interface StringMap<K,V> extends Map<K,V> {}
29-
interface Type {}
30-
31-
declare module "angular2/angular2" {
32-
type SetterFn = typeof Function;
33-
type int = number;
34-
35-
// See https://github.com/Microsoft/TypeScript/issues/1168
36-
class BaseException /* extends Error */ {
37-
message: string;
38-
stack: string;
39-
toString(): string;
40-
}
41-
}
42-
{% for module in doc.modules %}
43-
{$ commentBlock(module, 1) $}
18+
{% block staticDeclarations %}{% endblock %}
19+
{% for alias, module in doc.moduleDocs %}
20+
{$ commentBlock(module.doc, 1) $}
4421
declare module "{$ module.id $}" {
4522

46-
{%- for export in module.exports -%}
23+
{%- for export in module.doc.exports -%}
4724
{%- if export.content -%}
4825
{$ commentBlock(export, 3) $}
4926
{%- endif %}

0 commit comments

Comments
 (0)