All files parseText.ts

0% Statements 0/10
0% Branches 0/2
0% Functions 0/4
0% Lines 0/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40                                                                               
import * as leasot from "leasot";
import { codeFrameColumns } from "@babel/code-frame";
import { AnnotatedComment } from "./AnnotatedComment";
import { Plugin } from "./Plugin";
 
export function parseText<T>(
  content: string,
  filename: string,
  extension: string,
  bodyParser: Plugin<any, any>["parse"]
): Promise<AnnotatedComment<T>[]> {
  if (!leasot.isExtensionSupported(extension)) {
    return Promise.resolve([]);
  }
 
  const promises = leasot
    .parse(content, {
      extension,
      filename,
      withInlineFiles: true
    })
    .map(
      async (todoComment): Promise<AnnotatedComment<T>> => ({
        kind: todoComment.tag,
        file: todoComment.file,
        line: todoComment.line,
        text: todoComment.text,
        codeFrame: codeFrameColumns(content, {
          start: { line: todoComment.line }
        }),
        // @ts-ignore Filter annotation is missing immediately
        annotation: await bodyParser(todoComment.text)
      })
    );
 
  return Promise.all(promises).then(comments =>
    comments.filter(comment => !!comment.annotation)
  );
}