<commit_before>'use strict';

var process = global.process;
var assert = require('assert');
var path = require('path');
var TermColor = require('term-color');

var serializeType = require('../../serialize.js');

var STRAIGHT_LINE = '-----------------------------------------' +
    '------------------------';

module.exports = {
    prettyPrintAllErrors: prettyPrintAllErrors,
    prettyPrintTraces: prettyPrintTraces
};

function prettyPrintAllErrors(checker) {
    var parts = [];
    parts.push('');

    for (var i = 0; i < checker.errors.length; i++) {
        var error = checker.errors[i];

        assert(error.fileName, 'error must have fileName');
        var relativePath = path.relative(process.cwd(), error.fileName);

        parts.push(TermColor.underline(relativePath));

        parts.push(prettyPrintError(checker, error, {
            prefix: ''
        }));
    }

    var word = checker.errors.length === 1 ? 'error' : 'errors';
    parts.push(TermColor.red('Found (' + checker.errors.length + ') ' + word));
    parts.push('');

    return parts.join('\n');
}

function prettyPrintError(checker, error, opts) {
    var parts = [];

    parts.push(opts.prefix + 'Found error: ' + TermColor.cyan(error.type));
    parts.push(opts.prefix + error.message);
    parts.push('');

    if (opts.showLoc !== false && error.loc) {
        var meta = checker.getOrCreateMeta(error.fileName);
        var failingLines = getFailingLines(meta.sourceLines, error.loc);

        parts.push(opts.prefix + failingLines);
        parts.push('');
    }

    if (opts.showLoc !== false &&
        (
            error.type === 'jsig.checker.could-not-parse-javascript' ||
            error.type === 'jsig.parser.cannot-parse-header-file'
        )
    ) {
        var sourceLines = error.source.split('\n');
        var loc = {
            start: {
                column: 0,
                line: error.line
            },
            end: {
                line: error.line,
                column: sourceLines[error.line].length
            }
        };
        failingLines = getFailingLines(sourceLines, loc);

        parts.push(opts.prefix + failingLines);
        parts.push('');
    }

    if (error.expected && error.actual) {
        parts.push(opts.prefix +
            'Expected : ' + TermColor.green(error.expected));
        parts.push(opts.prefix +
            'Actual   : ' + TermColor.red(error.actual));<commit_msg>pretty-error: fixup pretty error logic with generalization<commit_after>'use strict';

var process = global.process;
var assert = require('assert');
var path = require('path');
var TermColor = require('term-color');

var serializeType = require('../../serialize.js');

var STRAIGHT_LINE = '-----------------------------------------' +
    '------------------------';

module.exports = {
    prettyPrintAllErrors: prettyPrintAllErrors,
    prettyPrintTraces: prettyPrintTraces
};

function prettyPrintAllErrors(checker) {
    var parts = [];
    parts.push('');

    for (var i = 0; i < checker.errors.length; i++) {
        var error = checker.errors[i];

        assert(error.fileName, 'error must have fileName');
        var relativePath = path.relative(process.cwd(), error.fileName);

        parts.push(TermColor.underline(relativePath));

        parts.push(prettyPrintError(checker, error, {
            prefix: ''
        }));
    }

    var word = checker.errors.length === 1 ? 'error' : 'errors';
    parts.push(TermColor.red('Found (' + checker.errors.length + ') ' + word));
    parts.push('');

    return parts.join('\n');
}

function prettyPrintError(checker, error, opts) {
    var parts = [];

    parts.push(opts.prefix + 'Found error: ' + TermColor.cyan(error.type));
    parts.push(opts.prefix + error.message);
    parts.push('');

    if (opts.showLoc !== false && error.loc) {
        var sourceLines;
        if (error.source) {
            sourceLines = error.source.split('\n');
        } else {
            var meta = checker.getOrCreateMeta(error.fileName);
            sourceLines = meta.sourceLines;
        }

        var failingLines = getFailingLines(sourceLines, error.loc);

        parts.push(opts.prefix + failingLines);
        parts.push('');
    } else if (opts.showLoc !== false &&
        (
            error.type === 'jsig.checker.could-not-parse-javascript' ||
            error.type === 'jsig.parser.cannot-parse-header-file'
        )
    ) {
        sourceLines = error.source.split('\n');
        var loc = {
            start: {
                column: 0,
                line: error.line
            },
            end: {
                line: error.line,
                column: sourceLines[error.line].length
            }
        };
        failingLines = getFailingLines(sourceLines, loc);

        parts.push(opts.prefix + failingLines);
        parts.push('');
    }

    if (error.expected && error.actual) {
        parts.push(opts.prefix +
            'Expected : ' + TermColor.green(error.expected));
        parts.push(opts.prefix +
            'Actual   : ' + TermColor.red(error.actual));
{'fraction_non_alphanumeric': 0.17122075879693335, 'fraction_numerical': 0.0011794770984863377, 'mean_word_length': 2.0706095353047678, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5276844', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1449, 'n_words': 270}