<commit_before>import path from 'path';
import escape from 'js-string-escape';
import Filter from 'broccoli-filter';
import { CLIEngine } from 'eslint';

const IGNORED_FILE_MESSAGE_REGEXP = /(?:File ignored by default\.)|(?:File ignored because of a matching ignore pattern\.)/;

export default class LintTree extends Filter {

  extensions = [ 'js' ];
  targetExtension = 'js';

  constructor(inputNode, options = {}) {
    super(inputNode, options);

    this.rootDir = options.rootDir;
    this.generateTests = options.generateTests;
    if (this.generateTests) {
      this.targetExtension = 'lint-test.js';
    }

    this.cli = new CLIEngine();
  }

  processString(content, relativePath) {
    let report = this.cli.executeOnText(content, path.join(this.rootDir, relativePath));
    let result = report.results[0] || {};
    let errors = result.messages || [];

    errors = errors.filter((error) => !IGNORED_FILE_MESSAGE_REGEXP.test(error.message));

    if (this.generateTests) {
      return this.testGenerator(relativePath, errors);
    }

    errors.forEach((error) => console.log(error)); // eslint-disable-line no-console
    return content;
  }

  testGenerator(relativePath, errors) {
    let passed = errors.length === 0;
    let messages = `${ relativePath } should pass ESLint`;
    if (!passed) {
      messages += '\n\n';
      messages += errors.map((error) => {
        return `${ error.line }:${ error.column } - ${ error.message } (${ error.ruleId })`;
      }).join('\n');
    }
    let output = "import test from 'ava';\ntest((t) => {";
    if (passed) {
      output += "  t.pass('Linting passed.')\n";
    } else {
      output += `  t.fail('${ escape(messages) }');\n`;
    }
    output += '});\n';
    return output;
  }

}<commit_msg>style(cli): improve output of linting errors<commit_after>import path from 'path';
import escape from 'js-string-escape';
import Filter from 'broccoli-filter';
import { CLIEngine } from 'eslint';
import dedent from 'dedent-js';
import ui from './ui';

const IGNORED_FILE_MESSAGE_REGEXP = /(?:File ignored by default\.)|(?:File ignored because of a matching ignore pattern\.)/;

export default class LintTree extends Filter {

  extensions = [ 'js' ];
  targetExtension = 'js';

  constructor(inputNode, options = {}) {
    super(inputNode, options);

    this.rootDir = options.rootDir;
    this.generateTests = options.generateTests;
    if (this.generateTests) {
      this.targetExtension = 'lint-test.js';
    }

    this.cli = new CLIEngine();
  }

  processString(content, relativePath) {
    let report = this.cli.executeOnText(content, path.join(this.rootDir, relativePath));
    let result = report.results[0] || {};
    let errors = result.messages || [];

    errors = errors.filter((error) => !IGNORED_FILE_MESSAGE_REGEXP.test(error.message));

    if (this.generateTests) {
      return this.testGenerator(relativePath, errors);
    }

    errors.forEach((error) => {
      ui.warn(dedent`
        ${ error.source }
        ${ error.message } (${ error.ruleId })
      `);
    });
    return content;
  }

  testGenerator(relativePath, errors) {
    let passed = errors.length === 0;
    let messages = `${ relativePath } should pass ESLint`;
    if (!passed) {
      messages += '\n\n';
      messages += errors.map((error) => {
        return `${ error.line }:${ error.column } - ${ error.message } (${ error.ruleId })`;
      }).join('\n');
    }
    let output = "import test from 'ava';\ntest((t) => {";
    if (passed) {
      output += "  t.pass('Linting passed.')\n";
    } else {
      output += `  t.fail('${ escape(messages) }');\n`;
    }
    output += '});\n';
    return output;
  }

}
{'fraction_non_alphanumeric': 0.16476345840130505, 'fraction_numerical': 0.001087547580206634, 'mean_word_length': 3.096881959910913, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6911575', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1143, 'n_words': 264}