Skip to content

Commit 4c38160

Browse files
alan-agius4thePunderWoman
authored andcommitted
fix(compiler-cli): correct extraction of generics from type aliases (#58548)
**Before:** ```ts type HttpEvent = | HttpSentEvent | HttpHeaderResponse | HttpResponse<T> | HttpProgressEvent | HttpUserEvent<T> ``` **After:** ```ts type HttpEvent<T> = | HttpSentEvent | HttpHeaderResponse | HttpResponse<T> | HttpProgressEvent | HttpUserEvent<T> ``` PR Close #58548
1 parent 9199dd9 commit 4c38160

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

adev/shared-docs/pipeline/api-gen/rendering/entities.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ export interface ConstantEntry extends DocEntry {
9494
}
9595

9696
/** Documentation entity for a type alias. */
97-
export type TypeAliasEntry = ConstantEntry;
97+
export interface TypeAliasEntry extends ConstantEntry {
98+
generics: GenericEntry[];
99+
}
98100

99101
/** Documentation entity for a TypeScript class. */
100102
export interface ClassEntry extends DocEntry {

adev/shared-docs/pipeline/api-gen/rendering/transforms/code-transforms.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ export function mapDocEntryToCode(entry: DocEntry): CodeTableOfContentsData {
243243
}
244244

245245
if (isTypeAliasEntry(entry)) {
246-
const contents = `type ${entry.name} = ${entry.type}`;
246+
const generics = makeGenericsText(entry.generics);
247+
const contents = `type ${entry.name}${generics} = ${entry.type}`;
247248

248249
if (isDeprecated) {
249250
const numberOfLinesOfCode = getNumberOfLinesOfCode(contents);
@@ -446,7 +447,6 @@ function appendPrefixAndSuffix(entry: DocEntry, codeTocData: CodeTableOfContents
446447

447448
if (isClassEntry(entry) || isInterfaceEntry(entry)) {
448449
const generics = makeGenericsText(entry.generics);
449-
450450
const extendsStr = entry.extends ? ` extends ${entry.extends}` : '';
451451
// TODO: remove the ? when we distinguish Class & Decorator entries
452452
const implementsStr =

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,16 @@ export interface ConstantEntry extends DocEntry {
100100
}
101101

102102
/** Documentation entity for a type alias. */
103-
export type TypeAliasEntry = ConstantEntry;
103+
export interface TypeAliasEntry extends ConstantEntry {
104+
generics: GenericEntry[];
105+
}
104106

105107
/** Documentation entity for a TypeScript class. */
106108
export interface ClassEntry extends DocEntry {
107109
isAbstract: boolean;
108110
members: MemberEntry[];
109-
generics: GenericEntry[];
110111
extends?: string;
112+
generics: GenericEntry[];
111113
implements: string[];
112114
}
113115

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import ts from 'typescript';
99

1010
import {EntryType} from './entities';
1111
import {extractJsDocDescription, extractJsDocTags, extractRawJsDoc} from './jsdoc_extractor';
12+
import {extractGenerics} from './generics_extractor';
1213

1314
/** Extract the documentation entry for a type alias. */
1415
export function extractTypeAlias(declaration: ts.TypeAliasDeclaration) {
@@ -20,6 +21,7 @@ export function extractTypeAlias(declaration: ts.TypeAliasDeclaration) {
2021
name: declaration.name.getText(),
2122
type: declaration.type.getText(),
2223
entryType: EntryType.TypeAlias,
24+
generics: extractGenerics(declaration),
2325
rawComment: extractRawJsDoc(declaration),
2426
description: extractJsDocDescription(declaration),
2527
jsdocTags: extractJsDocTags(declaration),

packages/compiler-cli/test/ngtsc/doc_extraction/type_alias_doc_extraction_spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,26 @@ runInEachFileSystem(() => {
6363
age: number;
6464
}`);
6565
});
66+
67+
it('should extract type aliases based with generics', () => {
68+
env.write(
69+
'index.ts',
70+
`
71+
type Foo<T> = undefined;
72+
export type Bar<T extends string> = Foo<T>;
73+
`,
74+
);
75+
76+
const docs: DocEntry[] = env.driveDocsExtraction('index.ts');
77+
expect(docs.length).toBe(1);
78+
79+
const typeAliasEntry = docs[0] as TypeAliasEntry;
80+
expect(typeAliasEntry.name).toBe('Bar');
81+
expect(typeAliasEntry.entryType).toBe(EntryType.TypeAlias);
82+
expect(typeAliasEntry.type).toBe('Foo<T>');
83+
expect(typeAliasEntry.generics).toEqual([
84+
{name: 'T', constraint: 'string', default: undefined},
85+
]);
86+
});
6687
});
6788
});

0 commit comments

Comments
 (0)