From b8be4bfaafbca2f8f30a500753d8bc5458065aa6 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 18 Aug 2015 15:37:27 -0700 Subject: [PATCH] fix(router): re-export of Type Closes #3632 Closes #3704 --- .../templates/type-definition.template.html | 72 ------------------- docs/typescript-definition-package/index.js | 1 + .../processors/code_gen.js | 40 +++++++++++ .../processors/createTypeDefinitionFile.js | 21 +++--- .../templates/type-definition.template.html | 21 ++---- modules/angular2/router.ts | 2 +- 6 files changed, 60 insertions(+), 97 deletions(-) delete mode 100644 docs/docs-package/templates/type-definition.template.html create mode 100644 docs/typescript-definition-package/processors/code_gen.js diff --git a/docs/docs-package/templates/type-definition.template.html b/docs/docs-package/templates/type-definition.template.html deleted file mode 100644 index 4ebceffaa0..0000000000 --- a/docs/docs-package/templates/type-definition.template.html +++ /dev/null @@ -1,72 +0,0 @@ - -{%- macro commentBlock(doc, level) -%} -{%- if doc.content | trim %} - -{% if level > 1 %}{$ '/**' | indent(level-1, true) | replace(r/\n$/, "") $}{% else %}/**{% endif %} -{$ 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 == 'Directive' %}: DirectiveAnnotation{%- elif 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 -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -// *********************************************************** -// This file is generated by the Angular build process. -// Please do not create manual edits or send pull requests -// modifying this file. -// *********************************************************** -{% block staticDeclarations %}{% endblock %} -{% for alias, module in doc.moduleDocs %} -{$ commentBlock(module.doc, 1) $} -declare module ng { - - {%- for export in module.doc.exports -%} - {%- if export.content -%} - {$ commentBlock(export, 3) $} - {%- endif %} - {$ export.docType $} {$ export.name $}{$ export.typeParams $}{%- if export.heritage == ' extends Directive' %} extends DirectiveAnnotation{% else %}{$ export.heritage $}{% endif %} - {%- if export.docType == 'class' or export.docType == 'interface' %} { - {%- if export.newMember %} - {$ memberInfo(export.newMember) $} - {% endif %} - {%- if export.callMember %} - {$ memberInfo(export.callMember) $} - {% endif -%} - {%- for member in export.members %} - {$ memberInfo(member) $} - {%- endfor %} - } - - {%- elif export.docType == 'enum' %} { - {%- for member in export.members %} - {$ member $}{% if not loop.last %}, - {%- endif -%} - {%- endfor %} - } - - {%- else -%} - {% if export.parameters %}({% for param in export.parameters %}{$ param $}{% if not loop.last %}, {% endif %}{% endfor %}){%- endif %} - {%- if export.returnType %} : {$ export.returnType $} {% endif -%} - ; - {%- endif %} - {% endfor %} -} - -{% endfor %} - -declare module "angular2/angular2" { - export = ng; -} diff --git a/docs/typescript-definition-package/index.js b/docs/typescript-definition-package/index.js index 73ab6815a7..b6262eea87 100644 --- a/docs/typescript-definition-package/index.js +++ b/docs/typescript-definition-package/index.js @@ -68,6 +68,7 @@ module.exports = new Package('angular-v2-docs', [jsdocPackage, nunjucksPackage, { id: 'angular2/router', references: ['./angular2.d.ts'], + remapTypes: {Type: 'ng.Type'}, modules: { 'angular2/router': { namespace: 'ngRouter', diff --git a/docs/typescript-definition-package/processors/code_gen.js b/docs/typescript-definition-package/processors/code_gen.js new file mode 100644 index 0000000000..4795aff310 --- /dev/null +++ b/docs/typescript-definition-package/processors/code_gen.js @@ -0,0 +1,40 @@ +module.exports = { + + signature: function(remap) { + return function(ast) { + try { + var text = []; + if (ast.isStatic) text.push('static '); + text.push(ast.name); + if (ast.optional) text.push('?'); + if (ast.typeParameters) { + text.push('<'); + text.push(ast.typeParameters.join(', ')); + text.push('>'); + } + if (ast.parameters) { + text.push('('); + text.push(ast.parameters.join(', ')); + text.push(')'); + } + if (ast.returnType) { + text.push(': ', ast.returnType); + } else if (ast.parameters) { + text.push(': void'); + } else { + text.push(': any'); + } + var string = text.join(''); + for (var key in remap) { + if (remap.hasOwnProperty(key)) { + string = string.replace(new RegExp('\\b' + key + '\\b', 'gm'), remap[key]); + } + } + return string; + } catch (e) { + console.log(e.toString(), e.stack); + return 'ERROR: ' + e.toString(); + } + } + } +}; diff --git a/docs/typescript-definition-package/processors/createTypeDefinitionFile.js b/docs/typescript-definition-package/processors/createTypeDefinitionFile.js index 78d196e7e0..b848020ac1 100644 --- a/docs/typescript-definition-package/processors/createTypeDefinitionFile.js +++ b/docs/typescript-definition-package/processors/createTypeDefinitionFile.js @@ -1,5 +1,6 @@ var _ = require('lodash'); var path = require('canonical-path'); +var codeGen = require('./code_gen.js'); module.exports = function createTypeDefinitionFile(log) { @@ -31,15 +32,17 @@ module.exports = function createTypeDefinitionFile(log) { path: docPath, outputPath: docPath, // A type definition may include a number of top level modules - // And those modules could be aliased (such as 'angular2/angular2.api' -> 'angular2/angular2') - moduleDocs: _.transform(def.modules, function(moduleDocs, props, alias) { - moduleDocs[props.id] = { - id: alias, - doc: null, - namespace: props.namespace, - references: def.references - }; - }) + // And those modules could be aliased (such as 'angular2/angular2.api' -> + // 'angular2/angular2') + moduleDocs: _.transform(def.modules, + function(moduleDocs, props, alias) { + moduleDocs[props.id] = { + id: alias, + doc: null, namespace: props.namespace, + references: def.references + }; + }), + signature: codeGen.signature(def.remapTypes) }; }); diff --git a/docs/typescript-definition-package/templates/type-definition.template.html b/docs/typescript-definition-package/templates/type-definition.template.html index b5860a1edc..b88294935c 100644 --- a/docs/typescript-definition-package/templates/type-definition.template.html +++ b/docs/typescript-definition-package/templates/type-definition.template.html @@ -8,18 +8,9 @@ {%- endmacro -%} -{%- macro memberInfo(member) -%} +{%- macro memberInfo(signature, member) -%} {$ commentBlock(member, 5) $} - {% if member.isStatic -%}static {% endif -%}{$ 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 $} -{%- elif member.parameters -%} - : void -{%- else -%} - : any -{%- endif -%}; + {$ signature(member) $}; {%- endmacro -%} @@ -53,16 +44,16 @@ declare module {$ module.namespace $} { {$ export.docType $} {$ export.name $}{$ export.typeParams $}{%- if export.heritage == ' extends Directive' %} extends DirectiveAnnotation{% else %}{$ export.heritage $}{% endif %} {%- if export.docType == 'class' or export.docType == 'interface' %} { {%- if export.newMember %} - {$ memberInfo(export.newMember) $} + {$ memberInfo(doc.signature, export.newMember) $} {% endif %} {%- if export.callMember %} - {$ memberInfo(export.callMember) $} + {$ memberInfo(doc.signature, export.callMember) $} {% endif -%} {%- for static in export.statics %} - {$ memberInfo(static) $} + {$ memberInfo(doc.signature, static) $} {%- endfor -%} {%- for member in export.members %} - {$ memberInfo(member) $} + {$ memberInfo(doc.signature, member) $} {%- endfor %} } diff --git a/modules/angular2/router.ts b/modules/angular2/router.ts index 5399e740b4..36baaff602 100644 --- a/modules/angular2/router.ts +++ b/modules/angular2/router.ts @@ -21,7 +21,7 @@ export {OnActivate, OnDeactivate, OnReuse, CanDeactivate, CanReuse} from './src/ export {CanActivate} from './src/router/lifecycle_annotations'; export {Instruction, ComponentInstruction} from './src/router/instruction'; export {Url} from './src/router/url_parser'; -export {OpaqueToken, Type} from 'angular2/angular2'; +export {OpaqueToken} from 'angular2/angular2'; export {ROUTE_DATA} from './src/router/route_data'; import {LocationStrategy} from './src/router/location_strategy';