Skip to content

Commit 4bffd97

Browse files
chore(doc-gen): move the "private class" converter into a helper service
This will allow it to be reused in other doc gen configurations
1 parent be95411 commit 4bffd97

File tree

4 files changed

+111
-24
lines changed

4 files changed

+111
-24
lines changed

docs/typescript-definition-package/processors/createTypeDefinitionFile.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var _ = require('lodash');
22
var path = require('canonical-path');
33
var codeGen = require('./code_gen.js');
44

5-
module.exports = function createTypeDefinitionFile(log) {
5+
module.exports = function createTypeDefinitionFile(log, convertPrivateClassesToInterfaces) {
66

77
return {
88
$runAfter: ['processing-docs'],
@@ -63,29 +63,7 @@ module.exports = function createTypeDefinitionFile(log) {
6363
doc = null;
6464
return;
6565
}
66-
_.forEach(modDoc.doc.exports, function(exportDoc) {
67-
68-
// Search for classes with a constructor marked as `@private`
69-
if (exportDoc.docType === 'class' && exportDoc.constructorDoc && exportDoc.constructorDoc.private) {
70-
71-
// Convert this class to an interface with no constructor
72-
exportDoc.docType = 'interface';
73-
exportDoc.constructorDoc = null;
74-
75-
if (exportDoc.heritage) {
76-
// convert the heritage since interfaces use `extends` not `implements`
77-
exportDoc.heritage = exportDoc.heritage.replace('implements', 'extends');
78-
}
79-
80-
// Add the `declare var SomeClass extends InjectableReference` construct
81-
modDoc.doc.exports.push({
82-
docType: 'var',
83-
name: exportDoc.name,
84-
id: exportDoc.id,
85-
returnType: 'InjectableReference'
86-
});
87-
}
88-
});
66+
convertPrivateClassesToInterfaces(modDoc.doc.exports, true);
8967
});
9068
return !!doc;
9169
});

docs/typescript-package/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ module.exports = new Package('typescript-parsing', [basePackage])
1515
.factory(require('./services/tsParser/getExportDocType'))
1616
.factory(require('./services/tsParser/getContent'))
1717

18+
.factory(require('./services/convertPrivateClassesToInterfaces'))
19+
1820
.factory('EXPORT_DOC_TYPES', function() {
1921
return [
2022
'class',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var _ = require('lodash');
2+
3+
module.exports = function convertPrivateClassesToInterfaces() {
4+
return function(exportDocs, addInjectableReference) {
5+
_.forEach(exportDocs, function(exportDoc) {
6+
7+
// Search for classes with a constructor marked as `@private`
8+
if (exportDoc.docType === 'class' && exportDoc.constructorDoc && exportDoc.constructorDoc.private) {
9+
10+
// Convert this class to an interface with no constructor
11+
exportDoc.docType = 'interface';
12+
exportDoc.constructorDoc = null;
13+
14+
if (exportDoc.heritage) {
15+
// convert the heritage since interfaces use `extends` not `implements`
16+
exportDoc.heritage = exportDoc.heritage.replace('implements', 'extends');
17+
}
18+
19+
if (addInjectableReference) {
20+
// Add the `declare var SomeClass extends InjectableReference` construct
21+
exportDocs.push({
22+
docType: 'var',
23+
name: exportDoc.name,
24+
id: exportDoc.id,
25+
returnType: 'InjectableReference'
26+
});
27+
}
28+
}
29+
});
30+
};
31+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var mockPackage = require('../mocks/mockPackage');
2+
var Dgeni = require('dgeni');
3+
var _ = require('lodash');
4+
5+
describe('readTypeScriptModules', function() {
6+
var dgeni, injector, convertPrivateClassesToInterfaces;
7+
8+
beforeEach(function() {
9+
dgeni = new Dgeni([mockPackage()]);
10+
injector = dgeni.configureInjector();
11+
convertPrivateClassesToInterfaces = injector.get('convertPrivateClassesToInterfaces');
12+
});
13+
14+
it('should convert private class docs to interface docs', function() {
15+
var docs = [
16+
{
17+
docType: 'class',
18+
name: 'privateClass',
19+
id: 'privateClass',
20+
constructorDoc: { private: true }
21+
}
22+
];
23+
convertPrivateClassesToInterfaces(docs, false);
24+
expect(docs[0].docType).toEqual('interface');
25+
});
26+
27+
28+
it('should not touch non-private class docs', function() {
29+
var docs = [
30+
{
31+
docType: 'class',
32+
name: 'privateClass',
33+
id: 'privateClass',
34+
constructorDoc: { }
35+
}
36+
];
37+
convertPrivateClassesToInterfaces(docs, false);
38+
expect(docs[0].docType).toEqual('class');
39+
});
40+
41+
42+
it('should convert the heritage since interfaces use `extends` not `implements`', function() {
43+
var docs = [
44+
{
45+
docType: 'class',
46+
name: 'privateClass',
47+
id: 'privateClass',
48+
constructorDoc: { private: true },
49+
heritage: 'implements parentInterface'
50+
}
51+
];
52+
convertPrivateClassesToInterfaces(docs, false);
53+
expect(docs[0].heritage).toEqual('extends parentInterface');
54+
});
55+
56+
57+
it('should add new injectable reference types, if specified, to the passed in collection', function() {
58+
var docs = [
59+
{
60+
docType: 'class',
61+
name: 'privateClass',
62+
id: 'privateClass',
63+
constructorDoc: { private: true },
64+
heritage: 'implements parentInterface'
65+
}
66+
];
67+
convertPrivateClassesToInterfaces(docs, true);
68+
expect(docs[1]).toEqual({
69+
docType : 'var',
70+
name : 'privateClass',
71+
id : 'privateClass',
72+
returnType : 'InjectableReference'
73+
});
74+
});
75+
76+
});

0 commit comments

Comments
 (0)