chore(doc-gen): allow class member sorting to be configured

This commit is contained in:
Peter Bacon Darwin 2015-05-17 21:45:04 +01:00
parent 9407c12923
commit 398e70ad39
3 changed files with 63 additions and 9 deletions

View File

@ -0,0 +1,12 @@
export class Test {
firstItem;
constructor() {
this.doStuff();
}
otherMethod() {
}
doStuff() {
}
}

View File

@ -13,6 +13,7 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
sourceFiles: {presence: true},
basePath: {presence: true},
hidePrivateMembers: { inclusion: [true, false] },
sortClassMembers: { inclusion: [true, false] },
ignoreExportsMatching: {}
},
@ -22,6 +23,8 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
basePath: '.',
// We can ignore members of classes that are private
hidePrivateMembers: false,
// We can sort class members alphabetically
sortClassMembers: true,
// We can provide a collection of strings or regexes to ignore exports whose export names match
ignoreExportsMatching: ['___esModule'],
@ -31,6 +34,9 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
var ignoreExportsMatching = convertToRegexCollection(this.ignoreExportsMatching);
var hidePrivateMembers = this.hidePrivateMembers;
var addMember = this.sortClassMembers ? insertSorted : function(collection, item) {
collection.push(item);
};
var basePath = path.resolve(readFilesProcessor.basePath, this.basePath);
var filesPaths = expandSourceFiles(this.sourceFiles, basePath);
@ -73,7 +79,7 @@ module.exports = function readTypeScriptModules(tsParser, readFilesProcessor, mo
docs.push(memberDoc);
} else if (!hidePrivateMembers || memberSymbol.name.charAt(0) !== '_') {
docs.push(memberDoc);
insertSorted(exportDoc.members, memberDoc, 'name');
addMember(exportDoc.members, memberDoc, 'name');
}
}
}

View File

@ -1,6 +1,7 @@
var mockPackage = require('../mocks/mockPackage');
var Dgeni = require('dgeni');
var path = require('canonical-path');
var _ = require('lodash');
describe('readTypeScriptModules', function() {
var dgeni, injector, processor;
@ -9,13 +10,13 @@ describe('readTypeScriptModules', function() {
dgeni = new Dgeni([mockPackage()]);
injector = dgeni.configureInjector();
processor = injector.get('readTypeScriptModules');
processor.basePath = path.resolve(__dirname, '../mocks/');
processor.basePath = path.resolve(__dirname, '../mocks/readTypeScriptModules');
});
describe('ignoreExportsMatching', function() {
it('should ignore exports that match items in the `ignoreExportsMatching` property', function() {
processor.sourceFiles = [ 'readTypeScriptModules/ignoreExportsMatching.ts'];
processor.sourceFiles = [ 'ignoreExportsMatching.ts'];
processor.ignoreExportsMatching = [/^_/];
var docs = [];
processor.$process(docs);
@ -29,17 +30,52 @@ describe('readTypeScriptModules', function() {
});
it('should only ignore `___esModule` exports by default', function() {
processor.sourceFiles = [ 'readTypeScriptModules/ignoreExportsMatching.ts'];
processor.sourceFiles = [ 'ignoreExportsMatching.ts'];
var docs = [];
processor.$process(docs);
var moduleDoc = docs[0];
expect(moduleDoc.docType).toEqual('module');
expect(moduleDoc.exports).toEqual([
jasmine.objectContaining({ name: 'OKToExport' }),
jasmine.objectContaining({ name: '_thisIsPrivate' }),
jasmine.objectContaining({ name: 'thisIsOK' })
expect(getNames(moduleDoc.exports)).toEqual([
'OKToExport',
'_thisIsPrivate',
'thisIsOK'
]);
});
});
});
describe('ordering of members', function() {
it('should order class members alphabetically (by default)', function() {
processor.sourceFiles = ['orderingOfMembers.ts'];
var docs = [];
processor.$process(docs);
var classDoc = _.find(docs, { docType: 'class' });
expect(classDoc.docType).toEqual('class');
expect(getNames(classDoc.members)).toEqual([
'doStuff',
'firstItem',
'otherMethod'
]);
});
it('should not order class members if not sortClassMembers is false', function() {
processor.sourceFiles = ['orderingOfMembers.ts'];
processor.sortClassMembers = false;
var docs = [];
processor.$process(docs);
var classDoc = _.find(docs, { docType: 'class' });
expect(classDoc.docType).toEqual('class');
expect(getNames(classDoc.members)).toEqual([
'firstItem',
'otherMethod',
'doStuff'
]);
});
});
});
function getNames(collection) {
return collection.map(function(item) { return item.name; });
}