All files / istanbul-lib-instrument/src ignored-lines.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2

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 626x 6x                                                                                                                        
const IGNORE_LINES_PATTERN = /\s*istanbul\s+ignore\s+(start|stop)/;
const EOL_PATTERN = /\r?\n/g;
 
/**
 * Parse ignore start/stop hints from **text file** based on regular expressions
 * - Does not understand what a comment is in Javascript (or JSX, Vue, Svelte)
 * - Parses source code (JS, TS, Vue, Svelte, anything) based on text search by
 *   matching for `/* istanbul ignore start *\/` pattern - not by looking for real comments
 *
 * ```js
 * /* istanbul ignore start *\/
 * <!-- /* istanbul ignore start *\/ -->
 * <SomeFrameworkComment content="/* istanbul ignore start *\/">
 * ```
 */
function getIgnoredLines(text: string | null | undefined): Set<number> {
  if (!text) {
    return new Set();
  }
 
  const ranges: { start: number; stop: number }[] = [];
  let lineNumber = 0;
 
  for (const line of text.split(EOL_PATTERN)) {
    lineNumber++;
 
    const match = line.match(IGNORE_LINES_PATTERN);
    if (match) {
      const type = match[1];
 
      if (type === "stop") {
        const previous = ranges[ranges.length - 1];
 
        // Ignore whole "ignore stop" if no previous start was found
        if (previous && previous.stop === Infinity) {
          previous.stop = lineNumber;
        }
 
        continue;
      }
 
      ranges.push({ start: lineNumber, stop: Infinity });
    }
  }
 
  const ignoredLines = new Set<number>();
 
  for (const range of ranges) {
    for (let line = range.start; line <= range.stop; line++) {
      ignoredLines.add(line);
 
      if (line >= lineNumber) {
        break;
      }
    }
  }
 
  return ignoredLines;
}
 
export { getIgnoredLines };