<commit_before>#!/usr/bin/env node

"use strict";

const getStdin = require("get-stdin");
const pkg = require("../package.json");
const validators = require("./validators");

const SPECIAL_RULES_URL =
  "https://github.com/prettier/eslint-config-prettier#special-rules";

if (module === require.main) {
  if (process.argv.length > 2 || process.stdin.isTTY) {
    console.error(
      [
        "This tool checks whether an ESLint configuration contains rules that are",
        "unnecessary or conflict with Prettier. It’s supposed to be run like this:",
        "",
        "  eslint --print-config . | eslint-config-prettier-check",
        "  eslint --print-config test/index.js | eslint-config-prettier-check",
        "",
        "Exit codes:",
        "",
        "0: No automatically detectable problems found.",
        "1: Unexpected error.",
        "2: Conflicting rules found.",
        "",
        "For more information, see:",
        "https://github.com/prettier/eslint-config-prettier#cli-helper-tool"
      ].join("\n")
    );
    process.exit(1);
  }

  getStdin()
    .then(string => {
      const result = processString(string);
      if (result.stderr) {
        console.error(result.stderr);
      }
      if (result.stdout) {
        console.error(result.stdout);
      }
      process.exit(result.code);
    })
    .catch(error => {
      console.error("Unexpected error", error);
      process.exit(1);
    });
}

function processString(string) {
  let config;
  try {
    config = JSON.parse(string);
  } catch (error) {
    return {
      stderr: `Failed to parse JSON:\n${error.message}`,
      code: 1
    };
  }

  if (
    !(
      Object.prototype.toString.call(config) === "[object Object]" &&
      Object.prototype.toString.call(config.rules) === "[object Object]"
    )
  ) {
    return {
      stderr: `Expected a \`{"rules: {...}"}\` JSON object, but got:\n${string}`,
      code: 1
    };
  }

  const allRules = Object.assign(
    Object.create(null),
    ...pkg.files
      .filter(name => !name.includes("/"))
      .map(ruleFileName => require(`../${ruleFileName}`).rules)
  );

  const regularRules = filterRules(
    allRules,
    (ruleName, value) => value === "off"
  );
  const optionsRules = filterRules(
    allRules,
    (ruleName, value) => value === 0 && ruleName in validators
  );
  const specialRules = filterRules(
    allRules,
    (ruleName, value) => value === 0 && !(ruleName in validators)
  );

  const flaggedRules = Object.keys(config.rules)
    .map(ruleName => {
      const value = config.rules[ruleName];
      const arrayValue = Array.isArray(value) ? value : [value];
      const level = arrayValue[0];
      const options = arrayValue.slice(1);
      const isOff = level === "off" || level === 0;
      return !isOff && ruleName in allRules ? { ruleName, options } : null;
    })
    .filter(Boolean);<commit_msg>Work around an npm bug for Node.js 6<commit_after>#!/usr/bin/env node

"use strict";

const fs = require("fs");
const path = require("path");
const getStdin = require("get-stdin");
const validators = require("./validators");

const SPECIAL_RULES_URL =
  "https://github.com/prettier/eslint-config-prettier#special-rules";

if (module === require.main) {
  if (process.argv.length > 2 || process.stdin.isTTY) {
    console.error(
      [
        "This tool checks whether an ESLint configuration contains rules that are",
        "unnecessary or conflict with Prettier. It’s supposed to be run like this:",
        "",
        "  eslint --print-config . | eslint-config-prettier-check",
        "  eslint --print-config test/index.js | eslint-config-prettier-check",
        "",
        "Exit codes:",
        "",
        "0: No automatically detectable problems found.",
        "1: Unexpected error.",
        "2: Conflicting rules found.",
        "",
        "For more information, see:",
        "https://github.com/prettier/eslint-config-prettier#cli-helper-tool"
      ].join("\n")
    );
    process.exit(1);
  }

  getStdin()
    .then(string => {
      const result = processString(string);
      if (result.stderr) {
        console.error(result.stderr);
      }
      if (result.stdout) {
        console.error(result.stdout);
      }
      process.exit(result.code);
    })
    .catch(error => {
      console.error("Unexpected error", error);
      process.exit(1);
    });
}

function processString(string) {
  let config;
  try {
    config = JSON.parse(string);
  } catch (error) {
    return {
      stderr: `Failed to parse JSON:\n${error.message}`,
      code: 1
    };
  }

  if (
    !(
      Object.prototype.toString.call(config) === "[object Object]" &&
      Object.prototype.toString.call(config.rules) === "[object Object]"
    )
  ) {
    return {
      stderr: `Expected a \`{"rules: {...}"}\` JSON object, but got:\n${string}`,
      code: 1
    };
  }

  // This used to look at "files" in package.json, but that is not reliable due
  // to an npm bug. See:
  // https://github.com/prettier/eslint-config-prettier/issues/57
  const allRules = Object.assign(
    Object.create(null),
    ...fs
      .readdirSync(path.join(__dirname, ".."))
      .filter(name => !name.startsWith(".") && name.endsWith(".js"))
      .map(ruleFileName => require(`../${ruleFileName}`).rules)
  );

  const regularRules = filterRules(
    allRules,
    (ruleName, value) => value === "off"
  );
  const optionsRules = filterRules(
    allRules,
    (ruleName, value) => value === 0 && ruleName in validators
  );
  const specialRules = filterRules(
    allRules,
    (ruleName, value) => value === 0 && !(ruleName in validators)
  );

  const flaggedRules = Object.keys(config.rules)
    .map(ruleName => {
      const value = config.rules[ruleName];
      const arrayValue = Array.isArray(value) ? value : [value];
      const level = arrayValue[0];
      const options = arrayValue.slice(1);
      const isOff = level === "off" || level === 0;
      return !isOff && ruleName in allRules ? { ruleName, options } : null;
    })
    .filter(Boolean);
{'fraction_non_alphanumeric': 0.16027125372146875, 'fraction_numerical': 0.004796559708898445, 'mean_word_length': 2.5487089201877935, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 5, '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': '6359542', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1881, 'n_words': 431}