| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- import fs from 'fs';
- import path from 'path';
- import ts, { HeritageClause, JSDocTag, SyntaxKind } from 'typescript';
- import { notNullOrUndefined } from '../../packages/common/src/shared-utils';
- import {
- DocsPage,
- MemberInfo,
- MethodInfo,
- MethodParameterInfo,
- ParsedDeclaration,
- PropertyInfo,
- ValidDeclaration,
- } from './typescript-docgen-types';
- /**
- * Parses TypeScript source files into data structures which can then be rendered into
- * markdown for documentation.
- */
- export class TypescriptDocsParser {
- private readonly atTokenPlaceholder = '__EscapedAtToken__';
- /**
- * Parses the TypeScript files given by the filePaths array and returns the
- * parsed data structures ready for rendering.
- */
- parse(filePaths: string[]): DocsPage[] {
- const sourceFiles = filePaths.map(filePath => {
- return ts.createSourceFile(
- filePath,
- this.replaceEscapedAtTokens(fs.readFileSync(filePath).toString()),
- ts.ScriptTarget.ES2015,
- true,
- );
- });
- const statements = this.getStatementsWithSourceLocation(sourceFiles);
- const pageMap = statements
- .map(statement => {
- const info = this.parseDeclaration(
- statement.statement,
- statement.sourceFile,
- statement.sourceLine,
- );
- return info;
- })
- .filter(notNullOrUndefined)
- .reduce((pages, declaration) => {
- const pageTitle = declaration.page || declaration.title;
- const existingPage = pages.get(pageTitle);
- if (existingPage) {
- existingPage.declarations.push(declaration);
- } else {
- const normalizedTitle = this.kebabCase(pageTitle);
- const fileName = normalizedTitle === declaration.category ? '_index' : normalizedTitle;
- pages.set(pageTitle, {
- title: pageTitle,
- category: declaration.category,
- declarations: [declaration],
- fileName,
- });
- }
- return pages;
- }, new Map<string, DocsPage>());
- return Array.from(pageMap.values());
- }
- /**
- * Maps an array of parsed SourceFiles into statements, including a reference to the original file each statement
- * came from.
- */
- private getStatementsWithSourceLocation(
- sourceFiles: ts.SourceFile[],
- ): Array<{ statement: ts.Statement; sourceFile: string; sourceLine: number }> {
- return sourceFiles.reduce((st, sf) => {
- const statementsWithSources = sf.statements.map(statement => {
- const sourceFile = path.relative(path.join(__dirname, '..'), sf.fileName).replace(/\\/g, '/');
- const sourceLine = sf.getLineAndCharacterOfPosition(statement.getStart()).line + 1;
- return { statement, sourceFile, sourceLine };
- });
- return [...st, ...statementsWithSources];
- }, [] as Array<{ statement: ts.Statement; sourceFile: string; sourceLine: number }>);
- }
- /**
- * Parses an InterfaceDeclaration into a simple object which can be rendered into markdown.
- */
- private parseDeclaration(
- statement: ts.Statement,
- sourceFile: string,
- sourceLine: number,
- ): ParsedDeclaration | undefined {
- if (!this.isValidDeclaration(statement)) {
- return;
- }
- const category = this.getDocsCategory(statement);
- if (category === undefined) {
- return;
- }
- let title: string;
- if (ts.isVariableStatement(statement)) {
- title = statement.declarationList.declarations[0].name.getText();
- } else {
- title = statement.name ? statement.name.getText() : 'anonymous';
- }
- const fullText = this.getDeclarationFullText(statement);
- const weight = this.getDeclarationWeight(statement);
- const description = this.getDeclarationDescription(statement);
- const docsPage = this.getDocsPage(statement);
- const since = this.getSince(statement);
- const packageName = this.getPackageName(sourceFile);
- const info = {
- packageName,
- sourceFile,
- sourceLine,
- fullText,
- title,
- weight,
- category,
- description,
- page: docsPage,
- since,
- };
- if (ts.isInterfaceDeclaration(statement)) {
- return {
- ...info,
- kind: 'interface',
- extendsClause: this.getHeritageClause(statement, ts.SyntaxKind.ExtendsKeyword),
- members: this.parseMembers(statement.members),
- };
- } else if (ts.isTypeAliasDeclaration(statement)) {
- return {
- ...info,
- type: statement.type,
- kind: 'typeAlias',
- members: ts.isTypeLiteralNode(statement.type)
- ? this.parseMembers(statement.type.members)
- : undefined,
- };
- } else if (ts.isClassDeclaration(statement)) {
- return {
- ...info,
- kind: 'class',
- members: this.parseMembers(statement.members),
- extendsClause: this.getHeritageClause(statement, ts.SyntaxKind.ExtendsKeyword),
- implementsClause: this.getHeritageClause(statement, ts.SyntaxKind.ImplementsKeyword),
- };
- } else if (ts.isEnumDeclaration(statement)) {
- return {
- ...info,
- kind: 'enum' as 'enum',
- members: this.parseMembers(statement.members) as PropertyInfo[],
- };
- } else if (ts.isFunctionDeclaration(statement)) {
- const parameters = statement.parameters.map(p => ({
- name: p.name.getText(),
- type: p.type ? p.type.getText() : '',
- optional: !!p.questionToken,
- initializer: p.initializer && p.initializer.getText(),
- }));
- return {
- ...info,
- kind: 'function',
- parameters,
- type: statement.type,
- };
- } else if (ts.isVariableStatement(statement)) {
- return {
- ...info,
- kind: 'variable',
- };
- }
- }
- /**
- * Returns the text of any "extends" or "implements" clause of a class or interface.
- */
- private getHeritageClause(
- statement: ts.ClassDeclaration | ts.InterfaceDeclaration,
- kind: ts.SyntaxKind.ExtendsKeyword | ts.SyntaxKind.ImplementsKeyword,
- ): HeritageClause | undefined {
- const { heritageClauses } = statement;
- if (!heritageClauses) {
- return;
- }
- const clause = heritageClauses.find(cl => cl.token === kind);
- if (!clause) {
- return;
- }
- return clause;
- }
- /**
- * Returns the declaration name plus any type parameters.
- */
- private getDeclarationFullText(declaration: ValidDeclaration): string {
- let name: string;
- if (ts.isVariableStatement(declaration)) {
- name = declaration.declarationList.declarations[0].name.getText();
- } else {
- name = declaration.name ? declaration.name.getText() : 'anonymous';
- }
- let typeParams = '';
- if (
- !ts.isEnumDeclaration(declaration) &&
- !ts.isVariableStatement(declaration) &&
- declaration.typeParameters
- ) {
- typeParams = '<' + declaration.typeParameters.map(tp => tp.getText()).join(', ') + '>';
- }
- return name + typeParams;
- }
- private getPackageName(sourceFile: string): string {
- const matches = sourceFile.match(/\/packages\/([^/]+)\//);
- if (matches) {
- return `@vendure/${matches[1]}`;
- } else {
- return '';
- }
- }
- /**
- * Parses an array of inteface members into a simple object which can be rendered into markdown.
- */
- private parseMembers(
- members: ts.NodeArray<ts.TypeElement | ts.ClassElement | ts.EnumMember>,
- ): Array<PropertyInfo | MethodInfo> {
- const result: Array<PropertyInfo | MethodInfo> = [];
- for (const member of members) {
- const modifiers = member.modifiers ? member.modifiers.map(m => m.getText()) : [];
- const isPrivate = modifiers.includes('private');
- if (
- !isPrivate &&
- (ts.isPropertySignature(member) ||
- ts.isMethodSignature(member) ||
- ts.isPropertyDeclaration(member) ||
- ts.isMethodDeclaration(member) ||
- ts.isConstructorDeclaration(member) ||
- ts.isEnumMember(member) ||
- ts.isGetAccessorDeclaration(member) ||
- ts.isIndexSignatureDeclaration(member))
- ) {
- const name = member.name
- ? member.name.getText()
- : ts.isIndexSignatureDeclaration(member)
- ? '[index]'
- : 'constructor';
- let description = '';
- let type = '';
- let defaultValue = '';
- let parameters: MethodParameterInfo[] = [];
- let fullText = '';
- let isInternal = false;
- let since: string | undefined;
- if (ts.isConstructorDeclaration(member)) {
- fullText = 'constructor';
- } else if (ts.isMethodDeclaration(member)) {
- fullText = member.name.getText();
- } else if (ts.isGetAccessorDeclaration(member)) {
- fullText = `${member.name.getText()}: ${member.type ? member.type.getText() : 'void'}`;
- } else {
- fullText = member.getText();
- }
- this.parseTags(member, {
- description: comment => (description += comment || ''),
- example: comment => (description += this.formatExampleCode(comment)),
- default: comment => (defaultValue = comment || ''),
- internal: comment => (isInternal = true),
- since: comment => (since = comment || undefined),
- });
- if (isInternal) {
- continue;
- }
- if (!ts.isEnumMember(member) && member.type) {
- type = member.type.getText();
- }
- const memberInfo: MemberInfo = {
- fullText,
- name,
- description: this.restoreAtTokens(description),
- type,
- modifiers,
- since,
- };
- if (
- ts.isMethodSignature(member) ||
- ts.isMethodDeclaration(member) ||
- ts.isConstructorDeclaration(member)
- ) {
- parameters = member.parameters.map(p => ({
- name: p.name.getText(),
- type: p.type ? p.type.getText() : '',
- optional: !!p.questionToken,
- initializer: p.initializer && p.initializer.getText(),
- }));
- result.push({
- ...memberInfo,
- kind: 'method',
- parameters,
- });
- } else {
- result.push({
- ...memberInfo,
- kind: 'property',
- defaultValue,
- });
- }
- }
- }
- return result;
- }
- private tagCommentText(tag: JSDocTag): string {
- if (!tag.comment) {
- return '';
- }
- if (typeof tag.comment === 'string') {
- return tag.comment;
- }
- return tag.comment.map(t => (t.kind === SyntaxKind.JSDocText ? t.text : t.getText())).join('');
- }
- /**
- * Reads the @docsWeight JSDoc tag from the interface.
- */
- private getDeclarationWeight(statement: ValidDeclaration): number {
- let weight = 10;
- this.parseTags(statement, {
- docsWeight: comment => (weight = Number.parseInt(comment || '10', 10)),
- });
- return weight;
- }
- private getDocsPage(statement: ValidDeclaration): string | undefined {
- let docsPage: string | undefined;
- this.parseTags(statement, {
- docsPage: comment => (docsPage = comment),
- });
- return docsPage;
- }
- /**
- * Reads the @since JSDoc tag
- */
- private getSince(statement: ValidDeclaration): string | undefined {
- let since: string | undefined;
- this.parseTags(statement, {
- since: comment => (since = comment),
- });
- return since;
- }
- /**
- * Reads the @description JSDoc tag from the interface.
- */
- private getDeclarationDescription(statement: ValidDeclaration): string {
- let description = '';
- this.parseTags(statement, {
- description: comment => (description += comment),
- example: comment => (description += this.formatExampleCode(comment)),
- });
- return this.restoreAtTokens(description);
- }
- /**
- * Extracts the "@docsCategory" value from the JSDoc comments if present.
- */
- private getDocsCategory(statement: ValidDeclaration): string | undefined {
- let category: string | undefined;
- this.parseTags(statement, {
- docsCategory: comment => (category = comment || ''),
- });
- return this.kebabCase(category);
- }
- /**
- * Type guard for the types of statement which can ge processed by the doc generator.
- */
- private isValidDeclaration(statement: ts.Statement): statement is ValidDeclaration {
- return (
- ts.isInterfaceDeclaration(statement) ||
- ts.isTypeAliasDeclaration(statement) ||
- ts.isClassDeclaration(statement) ||
- ts.isEnumDeclaration(statement) ||
- ts.isFunctionDeclaration(statement) ||
- ts.isVariableStatement(statement)
- );
- }
- /**
- * Parses the Node's JSDoc tags and invokes the supplied functions against any matching tag names.
- */
- private parseTags<T extends ts.Node>(
- node: T,
- tagMatcher: { [tagName: string]: (tagComment: string) => void },
- ): void {
- const jsDocTags = ts.getJSDocTags(node);
- for (const tag of jsDocTags) {
- const tagName = tag.tagName.text;
- if (tagMatcher[tagName]) {
- tagMatcher[tagName](this.tagCommentText(tag));
- }
- }
- }
- /**
- * Ensure all the code examples use the unix-style line separators.
- */
- private formatExampleCode(example: string = ''): string {
- return '\n\n*Example*\n\n' + example.replace(/\r/g, '');
- }
- private kebabCase<T extends string | undefined>(input: T): T {
- if (input == null) {
- return input;
- }
- return input
- .replace(/([a-z])([A-Z])/g, '$1-$2')
- .replace(/\s+/g, '-')
- .toLowerCase() as T;
- }
- /**
- * TypeScript from v3.5.1 interprets all '@' tokens in a tag comment as a new tag. This is a problem e.g.
- * when a plugin includes in it's description some text like "install the @vendure/some-plugin package". Here,
- * TypeScript will interpret "@vendure" as a JSDoc tag and remove it and all remaining text from the comment.
- *
- * The solution is to replace all escaped @ tokens ("\@") with a replacer string so that TypeScript treats them
- * as regular comment text, and then once it has parsed the statement, we replace them with the "@" character.
- */
- private replaceEscapedAtTokens(content: string): string {
- return content.replace(/\\@/g, this.atTokenPlaceholder);
- }
- /**
- * Restores "@" tokens which were replaced by the replaceEscapedAtTokens() method.
- */
- private restoreAtTokens(content: string): string {
- return content.replace(new RegExp(this.atTokenPlaceholder, 'g'), '@');
- }
- }
|