Pete Bacon Darwin a4f3f3f81d build(docs-infra): support doc aliases via @alias dgeni tag (#29673)
Now, one can add an `@alias` tag to API docs, which tells dgeni that this
API element (usually a `const`) is really just an alias for some API element
defined elsewhere.

Dgeni will then look up this API element and copy over the properties from
the alias to the current doc.

For example, we would like to privately export an Enum from `@angular/core`
but then publicly export this from `@angular/common`:

**packages/core/private_exports.ts**

```ts
/**
 * Description of this document.
 */
export enum ɵSomeEnum { ... }
```

**packages/common/public_api.ts**

```ts
import {ɵSomeEnum} from '@angular/core';

 /**
 * @alias core/ɵSomeEnum
 */
export const SomeEnum = ɵSomeEnum;
```

In the generated docs there will be a page for `common/SomeEnum`, which
will be rendered as an enum, rather than a const, showing the description
extracted from the `core/ɵSomeEnum`.

---

The implementation of this feature required some refactoring of the other
processing:

1. Previously `ɵ` prefixed exports were not even considered.
2. Due to 1. some processors needed to have guards added to ignore such
   private exports (`addMetadataAliases` and `checkContentRules`).
3. The processing of package pages had to be reworked (and split) so that
   it picked up the aliased export docs after their alias proeprties had
   been copied.

See FW-1207, FW-632, #29249

PR Close #29673
2019-04-04 10:52:36 -07:00

136 lines
5.2 KiB
JavaScript

var testPackage = require('../../helpers/test-package');
var Dgeni = require('dgeni');
describe('checkContentRules processor', function() {
let processor, logger;
beforeEach(function() {
const dgeni = new Dgeni([testPackage('angular-base-package')]);
const injector = dgeni.configureInjector();
processor = injector.get('checkContentRules');
logger = injector.get('log');
});
it('should exist on the injector', () => {
expect(processor).toBeDefined();
expect(processor.$process).toEqual(jasmine.any(Function));
});
it('shpuld run at the right time', () => {
expect(processor.$runAfter).toEqual(['tags-extracted']);
expect(processor.$runBefore).toEqual([]);
});
it('should do nothing if not configured', () => {
const docs = [{ docType: 'test', description: '## heading 2' }];
processor.$process(docs);
expect(docs).toEqual([{ docType: 'test', description: '## heading 2' }]);
expect(logger.error).not.toHaveBeenCalled();
});
it('should run configured rules against matching docs', () => {
const nameSpy1 = jasmine.createSpy('name 1');
const nameSpy2 = jasmine.createSpy('name 2');
const nameSpy3 = jasmine.createSpy('name 3');
const descriptionSpy1 = jasmine.createSpy('description 1');
const descriptionSpy2 = jasmine.createSpy('description 2');
const descriptionSpy3 = jasmine.createSpy('description 3');
processor.docTypeRules = {
'test1': {
name: [nameSpy1, nameSpy3],
description: [descriptionSpy1, descriptionSpy3]
},
'test2': {
name: [nameSpy2],
description: [descriptionSpy2]
}
};
const docs = [
{ docType: 'test1', description: 'test doc 1', name: 'test-1' },
{ docType: 'test2', description: 'test doc 2', name: 'test-2' },
];
processor.$process(docs);
expect(nameSpy1).toHaveBeenCalledTimes(1);
expect(nameSpy1).toHaveBeenCalledWith(docs[0], 'name', 'test-1');
expect(nameSpy2).toHaveBeenCalledTimes(1);
expect(nameSpy2).toHaveBeenCalledWith(docs[1], 'name', 'test-2');
expect(nameSpy3).toHaveBeenCalledTimes(1);
expect(nameSpy3).toHaveBeenCalledWith(docs[0], 'name', 'test-1');
expect(descriptionSpy1).toHaveBeenCalledTimes(1);
expect(descriptionSpy1).toHaveBeenCalledWith(docs[0], 'description', 'test doc 1');
expect(descriptionSpy2).toHaveBeenCalledTimes(1);
expect(descriptionSpy2).toHaveBeenCalledWith(docs[1], 'description', 'test doc 2');
expect(descriptionSpy3).toHaveBeenCalledTimes(1);
expect(descriptionSpy3).toHaveBeenCalledWith(docs[0], 'description', 'test doc 1');
});
it('should log warnings if the rule returns error messages and `failOnContentErrors` is false', () => {
const nameSpy1 = jasmine.createSpy('name 1').and.returnValue('name error message');
const descriptionSpy1 = jasmine.createSpy('description 1').and.returnValue('description error message');
processor.failOnContentErrors = false;
processor.docTypeRules = {
'test1': {
name: [nameSpy1],
description: [descriptionSpy1]
}
};
const docs = [
{ docType: 'test1', description: 'test doc 1', name: 'test-1' },
{ docType: 'test2', description: 'test doc 2', name: 'test-2' }
];
processor.$process(docs);
expect(logger.warn).toHaveBeenCalledTimes(2);
expect(logger.warn).toHaveBeenCalledWith('Content contains errors');
expect(logger.warn).toHaveBeenCalledWith(`name error message
description error message
- doc "test-1" (test1) `);
});
it('should log errors and then throw if `failOnContentErrors` is true and errors are found', () => {
const nameSpy1 = jasmine.createSpy('name 1').and.returnValue('name error message');
const descriptionSpy1 = jasmine.createSpy('description 1').and.returnValue('description error message');
processor.failOnContentErrors = true;
processor.docTypeRules = {
'test1': {
name: [nameSpy1],
description: [descriptionSpy1]
}
};
const docs = [
{ docType: 'test1', description: 'test doc 1', name: 'test-1' },
{ docType: 'test2', description: 'test doc 2', name: 'test-2' }
];
expect(() => processor.$process(docs)).toThrowError('Stopping due to content errors.');
expect(logger.error).toHaveBeenCalledTimes(2);
expect(logger.error).toHaveBeenCalledWith('Content contains errors');
expect(logger.error).toHaveBeenCalledWith(`name error message
description error message
- doc "test-1" (test1) `);
});
it('should ignore docs whose id contains a barred-o', () => {
const nameSpy1 = jasmine.createSpy('name 1');
processor.docTypeRules = { 'doc-type': { name: [nameSpy1] } };
const docs = [
{ docType: 'doc-type', id: 'package/class/property/param', name: 'name-1' },
{ docType: 'doc-type', id: 'package/class/property/ɵparam', name: 'name-2' },
{ docType: 'doc-type', id: 'package/class/ɵproperty/param', name: 'name-3' },
{ docType: 'doc-type', id: 'package/ɵclass/property/param', name: 'name-4' },
];
processor.$process(docs);
expect(nameSpy1).toHaveBeenCalledTimes(1);
expect(nameSpy1).toHaveBeenCalledWith(docs[0], 'name', 'name-1');
});
});