<commit_before>/*
 * @flow
 * @format
 */

import {join} from 'path';

import {getFlowErrorsWithWarnings} from './getFlowErrors';
import getAst from './getAst';

import {readFile, writeFile} from '../utils/async';
import {getNodeAtRange} from './getPathToLoc';

import type {Args} from './remove-commentsCommand';
import type {FlowLoc, FlowResult, FlowError, FlowMessage} from '../flowResult';
import type {Context} from './getContext';

async function getErrors(args: Args): Promise<Map<string, Array<FlowLoc>>> {
  const result: FlowResult = await getFlowErrorsWithWarnings(
    args.bin,
    args.errorCheckCommand,
    args.root,
    args.flowconfigName,
  );

  const errors = result.errors.filter(
    error =>
      (error.message[0].descr === 'Error suppressing comment' &&
        error.message[1].descr === 'Unused suppression') ||
      error.message[0].descr === 'Unused suppression comment.',
  );

  const errorsByFile = new Map();
  for (const error of errors) {
    const message = error.message[0];
    const loc = message.loc;
    if (loc) {
      const source = loc.source;
      if (source) {
        const file = join(args.root, source);
        const fileErrors: Array<FlowLoc> = errorsByFile.get(file) || [];
        fileErrors.push(loc);
        errorsByFile.set(file, fileErrors);
      }
    }
  }

  return errorsByFile;
}

async function removeUnusedErrorSuppressions(
  filename: string,
  errors: Array<FlowLoc>,
  flowBinPath: string,
): Promise<void> {
  const contentsString = await readFile(filename);
  const contents = await removeUnusedErrorSuppressionsFromText(
    Buffer.from(contentsString, 'utf8'),
    errors,
    flowBinPath,
  );
  await writeFile(filename, contents.toString('utf8'));
}

function bufferCharAt(buf: Buffer, pos: number): string {
  return buf.toString('utf8', pos, pos + 1);
}

const edible = /[\t ]/;
/* This is the most confusing part of this command. A simple version of this<commit_msg>Suppressions for 0.111<commit_after>/*
 * @flow
 * @format
 */

import {join} from 'path';

import {getFlowErrorsWithWarnings} from './getFlowErrors';
import getAst from './getAst';

import {readFile, writeFile} from '../utils/async';
import {getNodeAtRange} from './getPathToLoc';

import type {Args} from './remove-commentsCommand';
import type {FlowLoc, FlowResult, FlowError, FlowMessage} from '../flowResult';
import type {Context} from './getContext';

async function getErrors(args: Args): Promise<Map<string, Array<FlowLoc>>> {
  const result: FlowResult = await getFlowErrorsWithWarnings(
    args.bin,
    args.errorCheckCommand,
    args.root,
    args.flowconfigName,
  );

  const errors = result.errors.filter(
    error =>
      (error.message[0].descr === 'Error suppressing comment' &&
        error.message[1].descr === 'Unused suppression') ||
      error.message[0].descr === 'Unused suppression comment.',
  );

  const errorsByFile = new Map();
  for (const error of errors) {
    const message = error.message[0];
    const loc = message.loc;
    if (loc) {
      const source = loc.source;
      if (source) {
        const file = join(args.root, source);
        const fileErrors: Array<FlowLoc> = errorsByFile.get(file) || [];
        fileErrors.push(loc);
        errorsByFile.set(file, fileErrors);
      }
    }
  }

  return errorsByFile;
}

async function removeUnusedErrorSuppressions(
  filename: string,
  errors: Array<FlowLoc>,
  flowBinPath: string,
): Promise<void> {
  console.log(filename);
  const contentsString = await readFile(filename);
  const contents = await removeUnusedErrorSuppressionsFromText(
    Buffer.from(contentsString, 'utf8'),
    errors,
    flowBinPath,
  );
  await writeFile(filename, contents.toString('utf8'));
}

function bufferCharAt(buf: Buffer, pos: number): string {
  return buf.toString('utf8', pos, pos + 1);
}

const edible = /[\t ]/;
/* This is the most confusing part of this command. A simple version of this
{'fraction_non_alphanumeric': 0.13484002031488065, 'fraction_numerical': 0.005078720162519045, 'mean_word_length': 3.734375, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '372405', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1185, 'n_words': 322}