Skip to content

Commit 1934524

Browse files
jelbournatscott
authored andcommitted
feat(compiler): add docs extraction for type aliases (#52118)
This commit adds support for extracting type alises. It currently extracts the raw written type from the source without performing any resolution, such as for resolving `typeof` queries, as current Angular public APIs do not rely on this. PR Close #52118
1 parent 75d610d commit 1934524

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

packages/compiler-cli/src/ngtsc/docs/src/entities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export interface ConstantEntry extends DocEntry {
6262
type: string;
6363
}
6464

65+
/** Documentation entity for a type alias. */
66+
export type TypeAliasEntry = ConstantEntry;
67+
6568
/** Documentation entity for a TypeScript class. */
6669
export interface ClassEntry extends DocEntry {
6770
isAbstract: boolean;

packages/compiler-cli/src/ngtsc/docs/src/extractor.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {extractClass, extractInterface} from './class_extractor';
1717
import {extractConstant, isSyntheticAngularConstant} from './constant_extractor';
1818
import {DocEntry} from './entities';
1919
import {isAngularPrivateName} from './filters';
20+
import {extractTypeAlias} from './type_alias_extractor';
2021

2122
type DeclarationWithExportName = readonly[string, ts.Declaration];
2223

@@ -72,6 +73,10 @@ export class DocsExtractor {
7273
return extractConstant(node, this.typeChecker);
7374
}
7475

76+
if (ts.isTypeAliasDeclaration(node)) {
77+
return extractTypeAlias(node);
78+
}
79+
7580
if (ts.isEnumDeclaration(node)) {
7681
return extractEnum(node, this.typeChecker);
7782
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import ts from 'typescript';
9+
10+
import {EntryType} from './entities';
11+
import {extractJsDocDescription, extractJsDocTags, extractRawJsDoc} from './jsdoc_extractor';
12+
13+
/** Extract the documentation entry for a type alias. */
14+
export function extractTypeAlias(declaration: ts.TypeAliasDeclaration) {
15+
// TODO: this does not yet resolve type queries (`typeof`). We may want to
16+
// fix this eventually, but for now it does not appear that any type aliases in
17+
// Angular's public API rely on this.
18+
19+
return {
20+
name: declaration.name.getText(),
21+
type: declaration.type.getText(),
22+
entryType: EntryType.TypeAlias,
23+
rawComment: extractRawJsDoc(declaration),
24+
description: extractJsDocDescription(declaration),
25+
jsdocTags: extractJsDocTags(declaration),
26+
};
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {DocEntry} from '@angular/compiler-cli/src/ngtsc/docs';
10+
import {EntryType, TypeAliasEntry} from '@angular/compiler-cli/src/ngtsc/docs/src/entities';
11+
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
12+
import {loadStandardTestFiles} from '@angular/compiler-cli/src/ngtsc/testing';
13+
14+
import {NgtscTestEnvironment} from '../env';
15+
16+
const testFiles = loadStandardTestFiles({fakeCore: true, fakeCommon: true});
17+
18+
runInEachFileSystem(os => {
19+
let env!: NgtscTestEnvironment;
20+
21+
describe('ngtsc type alias docs extraction', () => {
22+
beforeEach(() => {
23+
env = NgtscTestEnvironment.setup(testFiles);
24+
env.tsconfig();
25+
});
26+
27+
it('should extract type aliases based on primitives', () => {
28+
env.write('index.ts', `
29+
export type SuperNumber = number | string;
30+
`);
31+
32+
const docs: DocEntry[] = env.driveDocsExtraction('index.ts');
33+
expect(docs.length).toBe(1);
34+
35+
const typeAliasEntry = docs[0] as TypeAliasEntry;
36+
expect(typeAliasEntry.name).toBe('SuperNumber');
37+
expect(typeAliasEntry.entryType).toBe(EntryType.TypeAlias);
38+
expect(typeAliasEntry.type).toBe('number | string');
39+
});
40+
41+
it('should extract type aliases for objects', () => {
42+
env.write('index.ts', `
43+
export type UserProfile = {
44+
name: string;
45+
age: number;
46+
};
47+
`);
48+
49+
const docs: DocEntry[] = env.driveDocsExtraction('index.ts');
50+
expect(docs.length).toBe(1);
51+
52+
const typeAliasEntry = docs[0] as TypeAliasEntry;
53+
expect(typeAliasEntry.name).toBe('UserProfile');
54+
expect(typeAliasEntry.entryType).toBe(EntryType.TypeAlias);
55+
expect(typeAliasEntry.type).toBe(`{
56+
name: string;
57+
age: number;
58+
}`);
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)