diff --git a/docs/typescript-definition-package/processors/createTypeDefinitionFile.js b/docs/typescript-definition-package/processors/createTypeDefinitionFile.js index 58b6520ee6..af3f56b121 100644 --- a/docs/typescript-definition-package/processors/createTypeDefinitionFile.js +++ b/docs/typescript-definition-package/processors/createTypeDefinitionFile.js @@ -2,7 +2,7 @@ var _ = require('lodash'); var path = require('canonical-path'); var codeGen = require('./code_gen.js'); -module.exports = function createTypeDefinitionFile(log) { +module.exports = function createTypeDefinitionFile(log, convertPrivateClassesToInterfaces) { return { $runAfter: ['processing-docs'], @@ -63,29 +63,7 @@ module.exports = function createTypeDefinitionFile(log) { doc = null; return; } - _.forEach(modDoc.doc.exports, function(exportDoc) { - - // Search for classes with a constructor marked as `@private` - if (exportDoc.docType === 'class' && exportDoc.constructorDoc && exportDoc.constructorDoc.private) { - - // Convert this class to an interface with no constructor - exportDoc.docType = 'interface'; - exportDoc.constructorDoc = null; - - if (exportDoc.heritage) { - // convert the heritage since interfaces use `extends` not `implements` - exportDoc.heritage = exportDoc.heritage.replace('implements', 'extends'); - } - - // Add the `declare var SomeClass extends InjectableReference` construct - modDoc.doc.exports.push({ - docType: 'var', - name: exportDoc.name, - id: exportDoc.id, - returnType: 'InjectableReference' - }); - } - }); + convertPrivateClassesToInterfaces(modDoc.doc.exports, true); }); return !!doc; }); diff --git a/docs/typescript-package/index.js b/docs/typescript-package/index.js index 165c654640..1e35385349 100644 --- a/docs/typescript-package/index.js +++ b/docs/typescript-package/index.js @@ -15,6 +15,8 @@ module.exports = new Package('typescript-parsing', [basePackage]) .factory(require('./services/tsParser/getExportDocType')) .factory(require('./services/tsParser/getContent')) +.factory(require('./services/convertPrivateClassesToInterfaces')) + .factory('EXPORT_DOC_TYPES', function() { return [ 'class', diff --git a/docs/typescript-package/services/convertPrivateClassesToInterfaces.js b/docs/typescript-package/services/convertPrivateClassesToInterfaces.js new file mode 100644 index 0000000000..9dbe32a544 --- /dev/null +++ b/docs/typescript-package/services/convertPrivateClassesToInterfaces.js @@ -0,0 +1,31 @@ +var _ = require('lodash'); + +module.exports = function convertPrivateClassesToInterfaces() { + return function(exportDocs, addInjectableReference) { + _.forEach(exportDocs, function(exportDoc) { + + // Search for classes with a constructor marked as `@private` + if (exportDoc.docType === 'class' && exportDoc.constructorDoc && exportDoc.constructorDoc.private) { + + // Convert this class to an interface with no constructor + exportDoc.docType = 'interface'; + exportDoc.constructorDoc = null; + + if (exportDoc.heritage) { + // convert the heritage since interfaces use `extends` not `implements` + exportDoc.heritage = exportDoc.heritage.replace('implements', 'extends'); + } + + if (addInjectableReference) { + // Add the `declare var SomeClass extends InjectableReference` construct + exportDocs.push({ + docType: 'var', + name: exportDoc.name, + id: exportDoc.id, + returnType: 'InjectableReference' + }); + } + } + }); + }; +}; diff --git a/docs/typescript-package/services/convertPrivateClassesToInterfaces.spec.js b/docs/typescript-package/services/convertPrivateClassesToInterfaces.spec.js new file mode 100644 index 0000000000..1292f30cc6 --- /dev/null +++ b/docs/typescript-package/services/convertPrivateClassesToInterfaces.spec.js @@ -0,0 +1,76 @@ +var mockPackage = require('../mocks/mockPackage'); +var Dgeni = require('dgeni'); +var _ = require('lodash'); + +describe('readTypeScriptModules', function() { + var dgeni, injector, convertPrivateClassesToInterfaces; + + beforeEach(function() { + dgeni = new Dgeni([mockPackage()]); + injector = dgeni.configureInjector(); + convertPrivateClassesToInterfaces = injector.get('convertPrivateClassesToInterfaces'); + }); + + it('should convert private class docs to interface docs', function() { + var docs = [ + { + docType: 'class', + name: 'privateClass', + id: 'privateClass', + constructorDoc: { private: true } + } + ]; + convertPrivateClassesToInterfaces(docs, false); + expect(docs[0].docType).toEqual('interface'); + }); + + + it('should not touch non-private class docs', function() { + var docs = [ + { + docType: 'class', + name: 'privateClass', + id: 'privateClass', + constructorDoc: { } + } + ]; + convertPrivateClassesToInterfaces(docs, false); + expect(docs[0].docType).toEqual('class'); + }); + + + it('should convert the heritage since interfaces use `extends` not `implements`', function() { + var docs = [ + { + docType: 'class', + name: 'privateClass', + id: 'privateClass', + constructorDoc: { private: true }, + heritage: 'implements parentInterface' + } + ]; + convertPrivateClassesToInterfaces(docs, false); + expect(docs[0].heritage).toEqual('extends parentInterface'); + }); + + + it('should add new injectable reference types, if specified, to the passed in collection', function() { + var docs = [ + { + docType: 'class', + name: 'privateClass', + id: 'privateClass', + constructorDoc: { private: true }, + heritage: 'implements parentInterface' + } + ]; + convertPrivateClassesToInterfaces(docs, true); + expect(docs[1]).toEqual({ + docType : 'var', + name : 'privateClass', + id : 'privateClass', + returnType : 'InjectableReference' + }); + }); + +});