X Tutup
Skip to content

Commit 7141c15

Browse files
committed
fix(docs): Working generated angular2.d.ts
This requires some hacks, documented in https://docs.google.com/document/d/1nNebWTiLzz5ePcit_bjZPtaiSIFU4EsQKUlX7LX0c0A/edit Changes: - include subtyping info in angular2.d.ts by adding 'extends supertype' - export missing symbols needed transitively by angular2/angular2 - because of decorator/annotation mismatch, we can't export these to applications. So I've added a separate angular2.api.ts file to re-export specifically to .d.ts generation. - Hack to remove aliases introduced by 'import * as alias' syntax - Hack to deal with Error still an interface note that we require users to install the transitive dependencies - this is how TSD works.
1 parent 5357b15 commit 7141c15

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

docs/dgeni-package/processors/createTypeDefinitionFile.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ module.exports = function createTypeDefinitionFile() {
1515
};
1616
_.forEach(docs, function(doc) {
1717
// The shape of the public API is determined by what is reexported into
18-
// angular2/angular2.
19-
if (doc.id === 'angular2/angular2') {
18+
// angular2/angular2, with hacks layered into angular2.api.ts
19+
if (doc.id === 'angular2/angular2.api') {
20+
doc.id = 'angular2/angular2';
2021
typeDefDoc.modules.push(doc);
2122
}
2223
});

docs/dgeni-package/processors/readTypeScriptModules.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ var path = require('canonical-path');
33
var _ = require('lodash');
44
var ts = require('typescript');
55

6-
module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, modules, getFileInfo, getExportDocType, getContent, log) {
6+
module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, modules, getFileInfo,
7+
getExportDocType, getContent, log) {
78

89
return {
910
$runAfter: ['files-read'],
@@ -12,8 +13,8 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
1213
$validate: {
1314
sourceFiles: {presence: true},
1415
basePath: {presence: true},
15-
hidePrivateMembers: { inclusion: [true, false] },
16-
sortClassMembers: { inclusion: [true, false] },
16+
hidePrivateMembers: {inclusion: [true, false]},
17+
sortClassMembers: {inclusion: [true, false]},
1718
ignoreExportsMatching: {}
1819
},
1920

@@ -64,7 +65,7 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
6465

6566
// Generate docs for each of the export's members
6667
if (resolvedExport.flags & ts.SymbolFlags.HasMembers) {
67-
68+
6869
exportDoc.members = [];
6970
for(var memberName in resolvedExport.members) {
7071
log.silly('>>>>>> member: ' + memberName + ' from ' + exportDoc.id + ' in ' + moduleDoc.id);
@@ -115,6 +116,29 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
115116
}
116117

117118
function createExportDoc(name, exportSymbol, moduleDoc, basePath, typeChecker) {
119+
exportSymbol.declarations.forEach(function(decl) {
120+
var sourceFile = ts.getSourceFileOfNode(decl);
121+
if (decl.typeParameters) {
122+
name = name + '<' + getText(sourceFile, decl.typeParameters) + '>';
123+
}
124+
if (decl.heritageClauses) {
125+
decl.heritageClauses.forEach(function(heritage) {
126+
if (heritage.token == ts.SyntaxKind.ExtendsKeyword) {
127+
name = name + " extends ";
128+
heritage.types.forEach(function(typ, idx) {
129+
name = name + (idx > 0 ? ', ' : '') + typ.getFullText();
130+
});
131+
}
132+
if (heritage.token == ts.SyntaxKind.ImplementsKeyword) {
133+
name = name + " implements ";
134+
heritage.types.forEach(function(typ, idx) {
135+
name = name + (idx > 0 ? ', ' : '') + typ.getFullText();
136+
});
137+
}
138+
});
139+
}
140+
});
141+
118142
var exportDoc = {
119143
docType: getExportDocType(exportSymbol),
120144
name: name,
@@ -167,22 +191,29 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
167191
function getParameters(typeChecker, symbol) {
168192
var declaration = symbol.valueDeclaration || symbol.declarations[0];
169193
var sourceFile = ts.getSourceFileOfNode(declaration);
170-
if(!declaration.parameters) {
194+
if (!declaration.parameters) {
171195
var location = getLocation(symbol);
172196
throw new Error('missing declaration parameters for "' + symbol.name +
173197
'" in ' + sourceFile.fileName +
174198
' at line ' + location.start.line);
175199
}
176200
return declaration.parameters.map(function(parameter) {
177-
return getText(sourceFile, parameter).trim();
201+
var paramText = getText(sourceFile, parameter.name);
202+
if (parameter.questionToken || parameter.initializer) {
203+
paramText += '?';
204+
}
205+
if (parameter.type) {
206+
paramText += ':' + getType(sourceFile, parameter.type);
207+
}
208+
return paramText.trim();
178209
});
179210
}
180211

181212
function getReturnType(typeChecker, symbol) {
182213
var declaration = symbol.valueDeclaration || symbol.declarations[0];
183214
var sourceFile = ts.getSourceFileOfNode(declaration);
184215
if (declaration.type) {
185-
return getText(sourceFile, declaration.type).trim();
216+
return getType(sourceFile, declaration.type).trim();
186217
}
187218
}
188219

@@ -201,6 +232,18 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
201232
}
202233

203234

235+
// Strip any local renamed imports from the front of types
236+
function getType(sourceFile, type) {
237+
var text = getText(sourceFile, type);
238+
while (text.indexOf(".") >= 0) {
239+
// Keep namespaced symbols in Rx
240+
if (text.match(/^\s*Rx\./)) break;
241+
// handle the case List<thing.stuff> -> List<stuff>
242+
text = text.replace(/([^.<]*)\.([^>]*)/, "$2");
243+
}
244+
return text;
245+
}
246+
204247
function getLocation(symbol) {
205248
var node = symbol.valueDeclaration || symbol.declarations[0];
206249
var sourceFile = ts.getSourceFileOfNode(node);

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{$ '*/' | indent(level, true) | replace(r/\n$/, "") $}{% endif -%}
77
{%- endmacro -%}
88

9-
// Type definitions for Angular v2.0.0-alpha.22
9+
// Type definitions for Angular v2.0.0-alpha.26
1010
// Project: http://angular.io/
1111
// Definitions by: angular team <https://github.com/angular/>
1212
// Definitions: https://github.com/borisyankov/DefinitelyTyped
@@ -16,6 +16,29 @@
1616
// Please do not create manual edits or send pull requests
1717
// modifying this file.
1818
// ***********************************************************
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;
38+
stack;
39+
toString(): string;
40+
}
41+
}
1942
{% for module in doc.modules %}
2043
{$ commentBlock(module, 1) $}
2144
declare module "{$ module.id $}" {

modules/angular2/angular2.api.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library angular2.angular2.api;
2+
// Ignore this file for dart emit.
3+
// It is used only for generating the TypeScript .d.ts file.

modules/angular2/angular2.api.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This module is used by dgeni to produce the angular2.d.ts file.
2+
3+
// Re-export everything we export to the application runtime
4+
export * from './angular2';
5+
6+
// Horrible hack. See
7+
// https://docs.google.com/document/d/1nNebWTiLzz5ePcit_bjZPtaiSIFU4EsQKUlX7LX0c0A/edit
8+
// Exports needed to make angular2.d.ts work,
9+
// because these symbols are dependencies of other exports but are not otherwise exported.
10+
// This should be cleaned up in one of two ways:
11+
// 1) if the symbol is intended to be part of the public API, then re-export somewhere else
12+
// 2) if the symbol should be omitted from the public API, then the class exposing it should
13+
// not be exported, or should avoid exposing the symbol.
14+
export {AbstractChangeDetector} from './src/change_detection/abstract_change_detector';
15+
export {ProtoRecord} from './src/change_detection/proto_record';
16+
export * from './src/core/compiler/element_injector';
17+
// FIXME: this is a workaround for https://github.com/angular/angular/issues/2356
18+
// We export the Directive *annotation* instead of the *decorator*.
19+
// But it breaks the build.
20+
export {Directive, LifecycleEvent} from './src/core/annotations_impl/annotations';
21+
export {FormDirective} from './src/forms/directives/form_directive';
22+
export {ControlContainerDirective} from './src/forms/directives/control_container_directive';
23+
export {Injectable} from './src/di/annotations_impl';
24+
export {BaseQueryList} from './src/core/compiler/base_query_list';
25+
export {AppProtoView, AppView, AppViewContainer} from './src/core/compiler/view';
26+
export * from './src/change_detection/parser/ast';
27+
export {Visibility} from './src/core/annotations_impl/visibility';
28+
export {AppViewManager} from './src/core/compiler/view_manager';

0 commit comments

Comments
 (0)
X Tutup