private async function shouldFixLintAsync()

in src/__Private/LintRunCLIEventHandler.hack [143:218]


  private async function shouldFixLintAsync<Terror as SingleRuleLintError>(
    AutoFixingLinter<Terror> $linter,
    Terror $error,
  ): Awaitable<bool> {
    $old = $linter->getFile()->getContents();
    $new = $linter->getFixedFile(vec[$error])->getContents();
    if ($old === $new) {
      await $this->renderLintBlameAsync($error);
      return false;
    }

    if ($this->terminal->supportsColors()) {
      await $this->terminal
        ->getStdout()
        ->writeAllAsync(CLIColoredUnifiedDiff::create($old, $new));
    } else {
      await $this->terminal
        ->getStdout()
        ->writeAllAsync(StringDiff::lines($old, $new)->getUnifiedDiff());
    }

    if (!$this->terminal->isInteractive()) {
      return false;
    }

    $cache_key = \get_class($error->getLinter());
    if (C\contains_key($this->userResponseCache, $cache_key)) {
      $should_fix = $this->userResponseCache[$cache_key];
      await $this->terminal
        ->getStdout()
        ->writeAllAsync(Str\format(
          "Would you like to apply this fix?\n  <%s to all>\n",
          $should_fix ? 'yes' : 'no',
        ));
      return $should_fix;
    }

    $response = null;
    do {
      /* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
      await $this->terminal
        ->getStdout()
        ->writeAllAsync(
          "\e[94mWould you like to apply this fix?\e[0m\n".
          "  \e[37m[y]es/[n]o/yes to [a]ll/n[o] to all:\e[0m ",
        );
      /* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
      $response = await $this->input->readLineAsync();
      if ($response === null) {
        break; // EOF
      }
      $response = Str\trim($response);
      switch ($response) {
        case 'a':
          $this->userResponseCache[$cache_key] = true;
          // FALLTHROUGH
        case 'y':
          return true;
        case 'o':
          $this->userResponseCache[$cache_key] = false;
          // FALLTHROUGH
        case 'n':
        case '':
          return false;
        default:
          /* HHAST_IGNORE_ERROR[DontAwaitInALoop] */
          await $this->terminal
            ->getStderr()
            ->writeAllAsync(
              Str\format("'%s' is not a valid response.\n", $response),
            );
          $response = null;
      }
    } while ($response === null);
    return false;
  }