Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
return {
$runAfter: ['extractDecoratedClassesProcessor', 'computeIdsProcessor'],
$runBefore: ['createSitemap'],
exportDocTypes: ['directive', 'pipe'],
$process(docs) {
docs.forEach(doc => {
if (doc.docType === 'ngmodule') {
Object.keys(doc.ngmoduleOptions).forEach(key => {
const value = doc.ngmoduleOptions[key];
if (value && !Array.isArray(value)) {
doc.ngmoduleOptions[key] = [value];
}
});
}
});

// Match all the directives/pipes to their module
const errors = [];
docs.forEach(doc => {
if (['directive', 'pipe'].indexOf(doc.docType) !== -1) {
if (this.exportDocTypes.indexOf(doc.docType) !== -1) {
if (!doc.ngModules || doc.ngModules.length === 0) {
errors.push(createDocMessage(`"${doc.id}" has no @ngModule tag. Docs of type "${doc.docType}" must have this tag.`, doc));
return;
Expand All @@ -38,7 +28,8 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
}

const ngModuleDoc = ngModuleDocs[0];
const container = ngModuleDoc[doc.docType + 's'] = ngModuleDoc[doc.docType + 's'] || [];
const containerName = getContainerName(doc.docType);
const container = ngModuleDoc[containerName] = ngModuleDoc[containerName] || [];
container.push(doc);

doc.ngModules[index] = ngModuleDoc;
Expand All @@ -50,6 +41,32 @@ module.exports = function processNgModuleDocs(getDocFromAlias, createDocMessage,
errors.forEach(error => log.error(error));
throw new Error('Failed to process NgModule relationships.');
}

docs.forEach(doc => {
if (doc.docType === 'ngmodule') {
Object.keys(doc.ngmoduleOptions).forEach(key => {
const value = doc.ngmoduleOptions[key];
if (value && !Array.isArray(value)) {
doc.ngmoduleOptions[key] = [value];
}
});
this.exportDocTypes.forEach(type => {
const containerName = getContainerName(type);
const container = doc[containerName];
if (container) {
container.sort(byId);
}
});
}
});
}
};
};

function getContainerName(docType) {
return docType + 's';
}

function byId(a, b) {
return a.id > b.id ? 1 : -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('processNgModuleDocs processor', () => {
expect(docs[0].ngmoduleOptions.c).toEqual([42]);
});

it('should link directive/pipe docs with their NgModule docs', () => {
it('should link directive/pipe docs with their NgModule docs (sorted by id)', () => {
const aliasMap = injector.get('aliasMap');
const ngModule1 = { docType: 'ngmodule', id: 'NgModule1', aliases: ['NgModule1'], ngmoduleOptions: {}};
const ngModule2 = { docType: 'ngmodule', id: 'NgModule2', aliases: ['NgModule2'], ngmoduleOptions: {}};
Expand All @@ -50,7 +50,7 @@ describe('processNgModuleDocs processor', () => {

aliasMap.addDoc(ngModule1);
aliasMap.addDoc(ngModule2);
processor.$process([ngModule1, ngModule2, directive1, directive2, directive3, pipe1, pipe2, pipe3]);
processor.$process([ngModule1, pipe2, directive2, directive3, pipe1, ngModule2, directive1, pipe3]);

expect(ngModule1.directives).toEqual([directive1, directive3]);
expect(ngModule1.pipes).toEqual([pipe1, pipe3]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ module.exports = function processPackages() {

// Partition the exports into groups by type
if (doc.exports) {
doc.ngmodules = doc.exports.filter(doc => doc.docType === 'ngmodule');
doc.classes = doc.exports.filter(doc => doc.docType === 'class');
doc.decorators = doc.exports.filter(doc => doc.docType === 'decorator');
doc.functions = doc.exports.filter(doc => doc.docType === 'function');
doc.structures = doc.exports.filter(doc => doc.docType === 'enum' || doc.docType === 'interface');
doc.directives = doc.exports.filter(doc => doc.docType === 'directive');
doc.pipes = doc.exports.filter(doc => doc.docType === 'pipe');
doc.types = doc.exports.filter(doc => doc.docType === 'type-alias' || doc.docType === 'const');
doc.ngmodules = doc.exports.filter(doc => doc.docType === 'ngmodule').sort(byId);
doc.classes = doc.exports.filter(doc => doc.docType === 'class').sort(byId);
doc.decorators = doc.exports.filter(doc => doc.docType === 'decorator').sort(byId);
doc.functions = doc.exports.filter(doc => doc.docType === 'function').sort(byId);
doc.structures = doc.exports.filter(doc => doc.docType === 'enum' || doc.docType === 'interface').sort(byId);
doc.directives = doc.exports.filter(doc => doc.docType === 'directive').sort(byId);
doc.pipes = doc.exports.filter(doc => doc.docType === 'pipe').sort(byId);
doc.types = doc.exports.filter(doc => doc.docType === 'type-alias' || doc.docType === 'const').sort(byId);
if (doc.exports.every(doc => !!doc.deprecated)) {
doc.deprecated = 'all exports of this entry point are deprecated.';
}
Expand Down Expand Up @@ -71,3 +71,8 @@ module.exports = function processPackages() {
}
};
};


function byId(a, b) {
return a.id > b.id ? 1 : -1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,26 @@ describe('processPackages processor', () => {
expect(newDocs[2].packageInfo.secondary).toEqual([newDocs[1], newDocs[2]]);
});

it('should partition the exports of packages into groups', () => {
it('should partition the exports of packages into groups, sorted by id', () => {
const docs = [
{
fileInfo: { filePath: 'some/x' },
docType: 'module',
id: 'x',
exports: [
{ docType: 'directive', id: 'directive-1' },
{ docType: 'function', id: 'function-1' },
{ docType: 'directive', id: 'directive-2' },
{ docType: 'decorator', id: 'decorator-1' },
{ docType: 'class', id: 'class-1' },
{ docType: 'directive', id: 'directive-1' },
{ docType: 'type-alias', id: 'type-alias-1' },
{ docType: 'class', id: 'class-2' },
{ docType: 'pipe', id: 'pipe-1' },
{ docType: 'const', id: 'const-1' },
{ docType: 'interface', id: 'interface-2' },
{ docType: 'const', id: 'const-2' },
{ docType: 'enum', id: 'enum-1' },
{ docType: 'interface', id: 'interface-1' },
{ docType: 'interface', id: 'interface-2' },
]
},
];
Expand All @@ -172,9 +172,9 @@ describe('processPackages processor', () => {
{ docType: 'pipe', id: 'pipe-1' },
]);
expect(newDocs[0].types).toEqual([
{ docType: 'type-alias', id: 'type-alias-1' },
{ docType: 'const', id: 'const-1' },
{ docType: 'const', id: 'const-2' },
{ docType: 'type-alias', id: 'type-alias-1' },
]);
});

Expand Down