@qodly/cli
Version:
Qodly CLI
49 lines (48 loc) • 3.7 kB
JavaScript
import { TextRange } from "@microsoft/tsdoc";
import ts from "typescript";
/**
* Returns true if the specified SyntaxKind is part of a declaration form.
*
* Based on ts.isDeclarationKind() from the compiler.
* https://github.com/microsoft/TypeScript/blob/v3.0.3/src/compiler/utilities.ts#L6382
*/ export function isDeclarationKind(kind) {
return kind === ts.SyntaxKind.ArrowFunction || kind === ts.SyntaxKind.BindingElement || kind === ts.SyntaxKind.ClassDeclaration || kind === ts.SyntaxKind.ClassExpression || kind === ts.SyntaxKind.Constructor || kind === ts.SyntaxKind.EnumDeclaration || kind === ts.SyntaxKind.EnumMember || kind === ts.SyntaxKind.ExportSpecifier || kind === ts.SyntaxKind.FunctionDeclaration || kind === ts.SyntaxKind.FunctionExpression || kind === ts.SyntaxKind.GetAccessor || kind === ts.SyntaxKind.ImportClause || kind === ts.SyntaxKind.ImportEqualsDeclaration || kind === ts.SyntaxKind.ImportSpecifier || kind === ts.SyntaxKind.InterfaceDeclaration || kind === ts.SyntaxKind.JsxAttribute || kind === ts.SyntaxKind.MethodDeclaration || kind === ts.SyntaxKind.MethodSignature || kind === ts.SyntaxKind.ModuleDeclaration || kind === ts.SyntaxKind.NamespaceExportDeclaration || kind === ts.SyntaxKind.NamespaceImport || kind === ts.SyntaxKind.Parameter || kind === ts.SyntaxKind.PropertyAssignment || kind === ts.SyntaxKind.PropertyDeclaration || kind === ts.SyntaxKind.PropertySignature || kind === ts.SyntaxKind.SetAccessor || kind === ts.SyntaxKind.ShorthandPropertyAssignment || kind === ts.SyntaxKind.TypeAliasDeclaration || kind === ts.SyntaxKind.TypeParameter || kind === ts.SyntaxKind.VariableDeclaration || kind === ts.SyntaxKind.JSDocTypedefTag || kind === ts.SyntaxKind.JSDocCallbackTag || kind === ts.SyntaxKind.JSDocPropertyTag;
}
/**
* Retrieves the JSDoc-style comments associated with a specific AST node.
*
* Based on ts.getJSDocCommentRanges() from the compiler.
* https://github.com/microsoft/TypeScript/blob/v3.0.3/src/compiler/utilities.ts#L924
*/ export function getJSDocCommentRanges(node, text) {
const commentRanges = [];
switch(node.kind){
case ts.SyntaxKind.Parameter:
case ts.SyntaxKind.TypeParameter:
case ts.SyntaxKind.FunctionExpression:
case ts.SyntaxKind.ArrowFunction:
case ts.SyntaxKind.ParenthesizedExpression:
commentRanges.push(...ts.getTrailingCommentRanges(text, node.pos) || []);
break;
}
commentRanges.push(...ts.getLeadingCommentRanges(text, node.pos) || []);
// True if the comment starts with '/**' but not if it is '/**/'
return commentRanges.filter((comment)=>text.charCodeAt(comment.pos + 1) === 0x2a /* ts.CharacterCodes.asterisk */ && text.charCodeAt(comment.pos + 2) === 0x2a /* ts.CharacterCodes.asterisk */ && text.charCodeAt(comment.pos + 3) !== 0x2f /* ts.CharacterCodes.slash */ );
}
export function walkCompilerAstAndFindComments(node, indent, foundComments) {
const buffer = node.getSourceFile().getFullText();
if (isDeclarationKind(node.kind)) {
// Find "/** */" style comments associated with this node.
// Note that this reinvokes the compiler's scanner -- the result is not cached.
const comments = getJSDocCommentRanges(node, buffer);
if (comments.length > 0) {
for (const comment of comments){
foundComments.push({
compilerNode: node,
textRange: TextRange.fromStringRange(buffer, comment.pos, comment.end)
});
}
}
}
return node.forEachChild((child)=>walkCompilerAstAndFindComments(child, indent + ' ', foundComments));
}
//# sourceMappingURL=utils.js.map