Reputation: 4110
I'm looking for a way to get comments from class and function at runtime in an Angular app in purpose to generate a custom doc. Using an external tool to generate the documentation doesn't fit my requirements. I really need to generate the documentation by myself.
I've made some trials and, for now, I have two possible cases:
Loading the concerned classes as a string and extract the comments with string manipulations (not the best case in my opinion).
Importing the classes and use ts to extract the comments. At this point I encounter some errors in my Angular app when I try to import ts: import * as ts from 'typescript'
. It obviously causes a circular reference. This code shows how to extract comments from a class but it seems it cannot be used in an Angular app at runtime : comment factory from TypeDoc sources
Does someone have an idea of a way to extract comments from a class reference or from a string representing the class?
Upvotes: 2
Views: 578
Reputation: 1988
What you are asking for is an implementation of metaprogramming in TypeScript, which is not available at the moment. You need to understand that the browser does not run TS code directly. Instead, code gets compiled to JavaScript, and only then does the runtime happen. And comments are there just for the programmer to see - it does not need to be transferred to the resulting JS transpiled code.
Typedoc only works at compile time, so it has access to all of the TS source files. If you want to create your own documentation parsing tool, that's fine, but you would be needed to learn a lot about how programming language parsing works. Search for "dragon book", "Let's build a simple interpreter", and other sources.
Upvotes: 3