I have a block of code
import { IFilter } from '../pipelines/IFilter';
/**
* Bi-directional Value Converters
*
* @valueConverter RgbToHex - Red-Green-Blue to Hex converter.
* We want to bind this color object to an HTML5 color input.
*/
@valueConverter("RgbToHex")
export class RgbToHexValueConverter {
toView(rgb: IColor): string {
return "#" + (
(1 << 24) + (rgb.r << 16) + (rgb.g << 8) + rgb.b
).toString(16).slice(1);
}
/**
* A method
*
* @method fromView - get data from view?
* This method is fromView.
*/
fromView(hex: string): IColor {
let exp = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i,
result = exp.exec(hex);
return {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
};
}
get fullName(): string {
return "FullName";
}
}
How to get a related comment for each node?
I need whole comment text and the type: MultiLine or SingleLine.
For example:
ClassDeclaration
/**
* Bi-directional Value Converters
*
* @valueConverter RgbToHex - Red-Green-Blue to Hex converter.
* We want to bind this color object to an HTML5 color input.
*/

MethodDeclaration
/**
* A method
*
* @method fromView - get data from view?
* This method is fromView.
*/

With Typescript compiler API I am able to find any comment for each node by leadingComments.
Can anyone guide me?
@HamedFathi in this case, use classDeclaration.getJsDocs() and methodDeclaration.getJsDocs() as those are part of the AST (unlike when calling Node#getLeadingCommentRanges() which parses out the comments after the fact).
(See the jsDoc property in that screenshot)
@dsherret
Unfortunately, getJsDocs does not return whole the text. I need the text and type because I wrote my own JsDoc parser.

Any idea?
@HamedFathi what do you mean by "type"?
Unfortunately, getJsDocs does not return whole the text
But it does! 馃槂 If you're going to parse out the jsdoc from the text then you can get the entire jsdoc text by calling #getJsDocs().map(doc => doc.getText()).
what do you mean by "type"?
MultiLine or SingleLine
@HamedFathi jsdocs will always be multiline鈥攚on't be // text (single line). If you want to get the single line comments then use getLeadingCommentRanges() on the node you want to get the comments from.
@dsherret
You made my day. You are so amazing man :)
Please keep up your great work. 馃憤
@HamedFathi no problem! Glad to help.
By the way, there is also a thing called "comment nodes" that's specific to ts-morph: https://dsherret.github.io/ts-morph/details/comments#comment-nodes -- I'm not sure if it's helpful in this scenario though, but worth mentioning just in case (and might be useful for someone else reading this).
How can I replace the JSDoc text? I found there is no way to do it in ts-morph nor in typescript raw compiler itself
Most helpful comment
@HamedFathi jsdocs will always be multiline鈥攚on't be
// text(single line). If you want to get the single line comments then usegetLeadingCommentRanges()on the node you want to get the comments from.