<commit_before>declare const global: any;

import * as path from 'path';

const partition = require('lodash.partition');
const loadCoverage = require('remap-istanbul/lib/loadCoverage');
const remap = require('remap-istanbul/lib/remap');
const writeReport = require('remap-istanbul/lib/writeReport');
const istanbulInstrument = require('istanbul-lib-instrument');

function processResult(result: any): void {
  const { coverageConfig, sourceCache, coverageCollectFiles } = global.__ts_coverage__cache__;
  if (!coverageConfig.collectCoverage) {
    return;
  }

  const coverage = result.testResults.map(value => value.coverage);
  const coveredFiles = coverage.reduce((acc, x) => acc.concat(Object.keys(x)), []);
  const uncoveredFiles = partition(coverageCollectFiles, x => coveredFiles.includes(x))[1];
  const coverageOutputPath = path.join(coverageConfig.coverageDirectory || 'coverage', 'remapped');

  //generate 'empty' coverage against uncovered files
  const emptyCoverage = uncoveredFiles.map(x => {
    const instrumenter = istanbulInstrument.createInstrumenter();
    instrumenter.instrumentSync(sourceCache[x], x);
    const ret = {};
    ret[x] = instrumenter.fileCoverage;
    return ret;
  });

  const mergedCoverage = loadCoverage(coverage.concat(emptyCoverage), { readJSON: (t) => t });
  const coverageCollector = remap(mergedCoverage, {
    readFile: (x) => {
      const key = path.normalize(x);
      const source = sourceCache[key];
      delete global.__ts_coverage__cache__.sourceCache[key];
      return source;
    }
  });

  writeReport(coverageCollector, 'html', {}, path.join(coverageOutputPath, 'html'));
  writeReport(coverageCollector, 'lcovonly', {}, path.join(coverageOutputPath, 'lcov.info'));
  writeReport(coverageCollector, 'json', {}, path.join(coverageOutputPath, 'coverage.json'));
  writeReport(coverageCollector, 'text', {}, path.join(coverageOutputPath, 'coverage.txt'));
}

module.exports = processResult;<commit_msg>Prevent failure when unexpected value passed<commit_after>declare const global: any;

import * as path from 'path';

const partition = require('lodash.partition');
const loadCoverage = require('remap-istanbul/lib/loadCoverage');
const remap = require('remap-istanbul/lib/remap');
const writeReport = require('remap-istanbul/lib/writeReport');
const istanbulInstrument = require('istanbul-lib-instrument');

function processResult(result: any): void {
  const { coverageConfig, sourceCache, coverageCollectFiles } = global.__ts_coverage__cache__;
  if (!coverageConfig.collectCoverage) {
    return;
  }

  const coverage = result.testResults.map(value => value.coverage);
  const coveredFiles = coverage.reduce((acc, x) => x ? acc.concat(Object.keys(x)) : acc, []);
  const uncoveredFiles = partition(coverageCollectFiles, x => coveredFiles.includes(x))[1];
  const coverageOutputPath = path.join(coverageConfig.coverageDirectory || 'coverage', 'remapped');

  //generate 'empty' coverage against uncovered files
  const emptyCoverage = uncoveredFiles.map(x => {
    const instrumenter = istanbulInstrument.createInstrumenter();
    instrumenter.instrumentSync(sourceCache[x], x);
    const ret = {};
    ret[x] = instrumenter.fileCoverage;
    return ret;
  });

  const mergedCoverage = loadCoverage(coverage.concat(emptyCoverage), { readJSON: (t) => t ? t : {} });
  const coverageCollector = remap(mergedCoverage, {
    readFile: (x) => {
      const key = path.normalize(x);
      const source = sourceCache[key];
      delete global.__ts_coverage__cache__.sourceCache[key];
      return source;
    }
  });

  writeReport(coverageCollector, 'html', {}, path.join(coverageOutputPath, 'html'));
  writeReport(coverageCollector, 'lcovonly', {}, path.join(coverageOutputPath, 'lcov.info'));
  writeReport(coverageCollector, 'json', {}, path.join(coverageOutputPath, 'coverage.json'));
  writeReport(coverageCollector, 'text', {}, path.join(coverageOutputPath, 'coverage.txt'));
}

module.exports = processResult;
{'fraction_non_alphanumeric': 0.138763197586727, 'fraction_numerical': 0.0005027652086475615, 'mean_word_length': 5.501633986928105, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1274955', 'n_tokens_mistral': 1220, 'n_tokens_neox': 1184, 'n_words': 235}