Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(compiler): extract docs info for enums, pipes, and NgModules (#5…
…1733) Based on top of #51717 This commit adds extraction for enums, pipes, and NgModules. It also adds a couple of tests for JsDoc extraction that weren't covered in the previous commit. PR Close #51733
- Loading branch information
1 parent
e0b1bb3
commit 2e41488
Showing
8 changed files
with
388 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/compiler-cli/src/ngtsc/docs/src/enum_extractor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {EntryType, EnumEntry, EnumMemberEntry, MemberType} from '@angular/compiler-cli/src/ngtsc/docs/src/entities'; | ||
import {extractJsDocDescription, extractJsDocTags, extractRawJsDoc} from '@angular/compiler-cli/src/ngtsc/docs/src/jsdoc_extractor'; | ||
import {extractResolvedTypeString} from '@angular/compiler-cli/src/ngtsc/docs/src/type_extractor'; | ||
import ts from 'typescript'; | ||
|
||
|
||
/** Extracts documentation entry for an enum. */ | ||
export function extractEnum( | ||
declaration: ts.EnumDeclaration, typeChecker: ts.TypeChecker): EnumEntry { | ||
return { | ||
name: declaration.name.getText(), | ||
entryType: EntryType.Enum, | ||
members: extractEnumMembers(declaration, typeChecker), | ||
rawComment: extractRawJsDoc(declaration), | ||
description: extractJsDocDescription(declaration), | ||
jsdocTags: extractJsDocTags(declaration), | ||
}; | ||
} | ||
|
||
/** Extracts doc info for an enum's members. */ | ||
function extractEnumMembers( | ||
declaration: ts.EnumDeclaration, checker: ts.TypeChecker): EnumMemberEntry[] { | ||
return declaration.members.map(member => ({ | ||
name: member.name.getText(), | ||
type: extractResolvedTypeString(member, checker), | ||
value: getEnumMemberValue(member), | ||
memberType: MemberType.EnumItem, | ||
jsdocTags: extractJsDocTags(member), | ||
description: extractJsDocDescription(member), | ||
memberTags: [], | ||
})); | ||
} | ||
|
||
/** Gets the explicitly assigned value for an enum member, or an empty string if there is none. */ | ||
function getEnumMemberValue(memberNode: ts.EnumMember): string { | ||
// If the enum member has a child number literal or string literal, | ||
// we use that literal as the "value" of the member. | ||
const literal = | ||
memberNode.getChildren().find(n => ts.isNumericLiteral(n) || ts.isStringLiteral(n)); | ||
return literal?.getText() ?? ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/compiler-cli/test/ngtsc/doc_extraction/enum_doc_extraction_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {DocEntry} from '@angular/compiler-cli/src/ngtsc/docs'; | ||
import {ClassEntry, DirectiveEntry, EntryType, EnumEntry, MemberTags, PropertyEntry} from '@angular/compiler-cli/src/ngtsc/docs/src/entities'; | ||
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing'; | ||
import {loadStandardTestFiles} from '@angular/compiler-cli/src/ngtsc/testing'; | ||
|
||
import {NgtscTestEnvironment} from '../env'; | ||
|
||
const testFiles = loadStandardTestFiles({fakeCore: true, fakeCommon: true}); | ||
|
||
runInEachFileSystem(os => { | ||
let env!: NgtscTestEnvironment; | ||
|
||
describe('ngtsc enum docs extraction', () => { | ||
beforeEach(() => { | ||
env = NgtscTestEnvironment.setup(testFiles); | ||
env.tsconfig(); | ||
}); | ||
|
||
it('should extract enum info without explicit values', () => { | ||
env.write('test.ts', ` | ||
export enum PizzaTopping { | ||
/** It is cheese */ | ||
Cheese, | ||
/** Or "tomato" if you are British */ | ||
Tomato, | ||
} | ||
`); | ||
|
||
const docs: DocEntry[] = env.driveDocsExtraction(); | ||
|
||
expect(docs.length).toBe(1); | ||
expect(docs[0].entryType).toBe(EntryType.Enum); | ||
|
||
const enumEntry = docs[0] as EnumEntry; | ||
expect(enumEntry.name).toBe('PizzaTopping'); | ||
expect(enumEntry.members.length).toBe(2); | ||
|
||
const [cheeseEntry, tomatoEntry] = enumEntry.members; | ||
|
||
expect(cheeseEntry.name).toBe('Cheese'); | ||
expect(cheeseEntry.description).toBe('It is cheese'); | ||
expect(cheeseEntry.value).toBe(''); | ||
|
||
expect(tomatoEntry.name).toBe('Tomato'); | ||
expect(tomatoEntry.description).toBe('Or "tomato" if you are British'); | ||
expect(tomatoEntry.value).toBe(''); | ||
}); | ||
|
||
it('should extract enum info with explicit values', () => { | ||
env.write('test.ts', ` | ||
export enum PizzaTopping { | ||
/** It is cheese */ | ||
Cheese = 0, | ||
/** Or "tomato" if you are British */ | ||
Tomato = "tomato", | ||
} | ||
`); | ||
|
||
const docs: DocEntry[] = env.driveDocsExtraction(); | ||
|
||
expect(docs.length).toBe(1); | ||
expect(docs[0].entryType).toBe(EntryType.Enum); | ||
|
||
const enumEntry = docs[0] as EnumEntry; | ||
expect(enumEntry.name).toBe('PizzaTopping'); | ||
expect(enumEntry.members.length).toBe(2); | ||
|
||
const [cheeseEntry, tomatoEntry] = enumEntry.members; | ||
|
||
expect(cheeseEntry.name).toBe('Cheese'); | ||
expect(cheeseEntry.description).toBe('It is cheese'); | ||
expect(cheeseEntry.value).toBe('0'); | ||
expect(cheeseEntry.type).toBe('PizzaTopping.Cheese'); | ||
|
||
expect(tomatoEntry.name).toBe('Tomato'); | ||
expect(tomatoEntry.description).toBe('Or "tomato" if you are British'); | ||
expect(tomatoEntry.value).toBe('"tomato"'); | ||
expect(tomatoEntry.type).toBe('PizzaTopping.Tomato'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.