export function getContent()

in typescript/src/services/TsParser/getContent.ts [21:72]


export function getContent(node: Node | undefined, concatLeadingComments = true) {

  let content = '';

  if (!node) return content;

  if (node.kind === SyntaxKind.ModuleDeclaration) {
    let moduleDeclaration = node as ModuleDeclaration;
    // If this is left side of dotted module declaration, there is no doc comment associated with this declaration
    if (moduleDeclaration.body && moduleDeclaration.body.kind === SyntaxKind.ModuleDeclaration) {
      return content;
    }

    // If this is dotted module name, get the doc comments from the parent
    while (moduleDeclaration.parent && moduleDeclaration.parent.kind === SyntaxKind.ModuleDeclaration) {
      moduleDeclaration = moduleDeclaration.parent;
    }
    node = moduleDeclaration;
  }

  // If this is a variable declaration then we get the doc comments from the grand parent
  if (node.kind === SyntaxKind.VariableDeclaration) {
    node = node.parent && node.parent.parent || node;
  }

  // Get the source file of this node
  const sourceFile = node.getSourceFile();
  const {leading, trailing} = getJSDocCommentRanges(node, sourceFile.text);
  const commentRanges = [];

  if (concatLeadingComments) {
    commentRanges.push(...leading);
  } else if (leading.length) {
    commentRanges.push(leading[leading.length - 1]);
  }

  if (syntaxKindsWithTrailingComments.includes(node.kind)) {
    commentRanges.push(...trailing);
  }

  commentRanges.forEach(commentRange => {
    content += sourceFile.text
        .substring(commentRange.pos + '/**'.length, commentRange.end - '*/'.length)
        .replace(LEADING_STAR, '')
        .trim();
    if (commentRange.hasTrailingNewLine) {
      content += '\n';
    }
  });

  return content.trim();
}