All files / src/services eslint.ts

100% Statements 29/29
83.33% Branches 20/24
100% Functions 11/11
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65        2x         5x 1x 1x     1x       36x   5x     8x       8x 28x 28x   28x   1x   28x 1x     28x 1x       1x       28x 28x     28x       5x 8x 8x 1x     7x    
import { SourceCode } from "eslint";
import { Node, Comment } from "estree";
 
function getFullCommentText(comment: Comment) {
  return comment.type === "Line"
    ? "//" + comment.value
    : `/*${comment.value}*/`;
}
 
const wrapSourceWithLeadingComments = (source: string, comments: Comment[]) => {
  const commentText = comments.reduce((value, comment) => {
    return value + getFullCommentText(comment) + "\n";
  }, "");
 
  return commentText + source;
};
 
function getTrailingCommentsFromSameLine(sourceCode: SourceCode, node: Node) {
  return sourceCode
    .getCommentsAfter(node)
    .filter(comment => comment.loc?.start.line === node.loc?.start.line);
}
 
export const nodesArrayToText = (sourceCode: SourceCode) => (
  nodes: Node[],
  pipe?: (source: string, index: number) => string
) => {
  return nodes.reduce((value, node) => {
    let astSource = sourceCode.getText(node);
    const trailingComments = getTrailingCommentsFromSameLine(sourceCode, node);
 
    const leadingComments = sourceCode
      .getCommentsBefore(node)
      .filter(comment => comment.loc?.start.line !== 1);
 
    if (leadingComments.length) {
      astSource = wrapSourceWithLeadingComments(astSource, leadingComments);
    }
 
    if (trailingComments.length) {
      trailingComments
        // Eslint treats block comments (/* */) as trailing even if they are on the previous line
        // .filter(trailing => trailing.loc!.start.column > 0)
        .forEach(comment => {
          astSource = astSource + " " + getFullCommentText(comment);
        });
    }
 
    Eif (pipe) {
      astSource = pipe(astSource, nodes.indexOf(node));
    }
 
    return value + astSource;
  }, "");
};
 
export const getNodeEndPosition = (sourceCode: SourceCode, node: Node) => {
  const trailingComments = getTrailingCommentsFromSameLine(sourceCode, node);
  if (trailingComments && trailingComments.length) {
    return trailingComments[trailingComments.length - 1]!.range![1];
  }
 
  return node.range![1];
};