<commit_before>import * as tsc from 'typescript';
import * as path from 'path';

export function getTSConfig(globals) {
  let config = (globals && globals.__TS_CONFIG__) ? globals.__TS_CONFIG__ : undefined;
  if (config === undefined) {
    config = 'tsconfig.json';
  }
  if (typeof config === 'string') {
    config = require(path.resolve(config)).compilerOptions;
  }
  config.module = config.module || tsc.ModuleKind.CommonJS;
  config.jsx = config.jsx || tsc.JsxEmit.React;
  return tsc.convertCompilerOptionsFromJson(config, undefined).options;
}<commit_msg>Read coverage configuration from jest<commit_after>import * as tsc from 'typescript';
import * as path from 'path';
import * as fs from 'fs';

const assign = require('lodash.assign');
const normalize = require('jest-config').normalize;
const setFromArgv = require('jest-config/build/setFromArgv');

function parseConfig(argv) {
  if (argv.config && typeof argv.config === 'string') {
    // If the passed in value looks like JSON, treat it as an object.
    if (argv.config[0] === '{' && argv.config[argv.config.length - 1] === '}') {
      return JSON.parse(argv.config);
    }
  }
  return argv.config;
}

function loadJestConfigFromFile(filePath, argv) {
  const config = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
  config.rootDir = config.rootDir ?
    path.resolve(path.dirname(filePath), config.rootDir) :
    process.cwd();
    return normalize(config, argv);
}

function loadJestConfigFromPackage(filePath, argv) {
  try {
    fs.accessSync(filePath, fs.constants.R_OK);
  } catch (e) {
    return null;
  }

  const packageData = require(filePath);
  const config = packageData.jest || {};
  const root = path.dirname(filePath);
  config.rootDir = config.rootDir ? path.resolve(root, config.rootDir) : root;
  return normalize(config, argv);
}

function readRawConfig(argv, root) {
  const rawConfig = parseConfig(argv);

  if (typeof rawConfig === 'string') {
    return loadJestConfigFromFile(path.resolve(process.cwd(), rawConfig), argv);
  }

  if (typeof rawConfig === 'object') {
    const config = assign({}, rawConfig);
    config.rootDir = config.rootDir || root;
    return normalize(config, argv);
  }

  const packageConfig = loadJestConfigFromPackage(path.join(root, 'package.json'), argv);
  return packageConfig || normalize({ rootDir: root }, argv);
}

export function getJestConfig(root) {
  try {
    const yargs = require('yargs');
    const argv = yargs(process.argv.slice(2)).argv;
    const rawConfig = readRawConfig(argv, root);
    return Object.freeze(setFromArgv(rawConfig, argv));
  } catch (e) {
    return {};
  }
}

export function getTSConfig(globals, collectCoverage: boolean = false) {
  let config = (globals && globals.__TS_CONFIG__) ? globals.__TS_CONFIG__ : undefined;
  if (config === undefined) {
    config = 'tsconfig.json';
  }
  if (typeof config === 'string') {
    config = require(path.resolve(config)).compilerOptions;
  }
  config.module = config.module || tsc.ModuleKind.CommonJS;
  config.jsx = config.jsx || tsc.JsxEmit.React;

  //inline source with source map for remapping coverage
  if (collectCoverage) {
    if (config.sourceMap) {
      delete config.sourceMap;
    }

    config.inlineSourceMap = true;
    config.inlineSources = true;
  }

  return tsc.convertCompilerOptionsFromJson(config, undefined).options;
}
{'fraction_non_alphanumeric': 0.13752983293556087, 'fraction_numerical': 0.0011933174224343676, 'mean_word_length': 4.158461538461538, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7005069', 'n_tokens_mistral': 1066, 'n_tokens_neox': 1048, 'n_words': 246}