chore(doc-gen): move the "private class" converter into a helper service
This will allow it to be reused in other doc gen configurations
This commit is contained in:
parent
be954115f8
commit
4bffd97edd
@ -2,7 +2,7 @@ var _ = require('lodash');
|
|||||||
var path = require('canonical-path');
|
var path = require('canonical-path');
|
||||||
var codeGen = require('./code_gen.js');
|
var codeGen = require('./code_gen.js');
|
||||||
|
|
||||||
module.exports = function createTypeDefinitionFile(log) {
|
module.exports = function createTypeDefinitionFile(log, convertPrivateClassesToInterfaces) {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
$runAfter: ['processing-docs'],
|
$runAfter: ['processing-docs'],
|
||||||
@ -63,29 +63,7 @@ module.exports = function createTypeDefinitionFile(log) {
|
|||||||
doc = null;
|
doc = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_.forEach(modDoc.doc.exports, function(exportDoc) {
|
convertPrivateClassesToInterfaces(modDoc.doc.exports, true);
|
||||||
|
|
||||||
// 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'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return !!doc;
|
return !!doc;
|
||||||
});
|
});
|
||||||
|
@ -15,6 +15,8 @@ module.exports = new Package('typescript-parsing', [basePackage])
|
|||||||
.factory(require('./services/tsParser/getExportDocType'))
|
.factory(require('./services/tsParser/getExportDocType'))
|
||||||
.factory(require('./services/tsParser/getContent'))
|
.factory(require('./services/tsParser/getContent'))
|
||||||
|
|
||||||
|
.factory(require('./services/convertPrivateClassesToInterfaces'))
|
||||||
|
|
||||||
.factory('EXPORT_DOC_TYPES', function() {
|
.factory('EXPORT_DOC_TYPES', function() {
|
||||||
return [
|
return [
|
||||||
'class',
|
'class',
|
||||||
|
@ -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'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
};
|
@ -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'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user