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 doc info for JsDoc (#51733)
Based on top of #51713 This commit adds docs extraction for information provided in JsDoc comments, including descriptions and Jsdoc tags. PR Close #51733
- Loading branch information
1 parent
a24ae99
commit e0b1bb3
Showing
7 changed files
with
335 additions
and
8 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
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
45 changes: 45 additions & 0 deletions
45
packages/compiler-cli/src/ngtsc/docs/src/jsdoc_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,45 @@ | ||
/** | ||
* @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 ts from 'typescript'; | ||
|
||
import {JsDocTagEntry} from './entities'; | ||
|
||
|
||
/** Gets the set of JsDoc tags applied to a node. */ | ||
export function extractJsDocTags(node: ts.HasJSDoc): JsDocTagEntry[] { | ||
return ts.getJSDocTags(node).map(t => ({ | ||
name: t.tagName.getText(), | ||
comment: ts.getTextOfJSDocComment(t.comment) ?? '', | ||
})); | ||
} | ||
|
||
/** | ||
* Gets the JsDoc description for a node. If the node does not have | ||
* a description, returns the empty string. | ||
*/ | ||
export function extractJsDocDescription(node: ts.HasJSDoc): string { | ||
// If the node is a top-level statement (const, class, function, etc.), we will get | ||
// a `ts.JSDoc` here. If the node is a `ts.ParameterDeclaration`, we will get | ||
// a `ts.JSDocParameterTag`. | ||
const commentOrTag = ts.getJSDocCommentsAndTags(node).find(d => { | ||
return ts.isJSDoc(d) || ts.isJSDocParameterTag(d); | ||
}); | ||
|
||
const comment = commentOrTag?.comment ?? ''; | ||
return typeof comment === 'string' ? comment : ts.getTextOfJSDocComment(comment) ?? ''; | ||
} | ||
|
||
/** | ||
* Gets the raw JsDoc applied to a node. If the node does not have a JsDoc block, | ||
* returns the empty string. | ||
*/ | ||
export function extractRawJsDoc(node: ts.HasJSDoc): string { | ||
// Assume that any node has at most one JsDoc block. | ||
return ts.getJSDocCommentsAndTags(node).find(ts.isJSDoc)?.getFullText() ?? ''; | ||
} |
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.