<commit_before>"use strict";

var _ = require("underscore"),
    JSHINT = require("jshint").JSHINT,
    fs = require("fs"),
    path = require("path"),
    fsHelper = require("./fs-helper"),
    fileFilters = require("./file-filters"),
    argv = {};

exports.argv = function (argv_) {
    if (argv_) {
        argv = argv_;
        return exports;
    } else {
        return argv;
    }
};

exports._lintPackage = function lintPackage (package_, options) {
    // if options haven't been supplied, then we could look them up.
    if (!options || !_.isObject(options)) {
        var configFile;
        if (argv.all || argv.lintAll) {
            configFile = fsHelper.findFileSuffix(package_.directory, ".jshintrc");
        } else {
            var libFile = path.resolve(package_.directory, ".jshintrc");
            var rootFile = path.resolve(package_.packageRoot, ".jshintrc");
            if (fs.existsSync(rootFile)) {
                configFile = rootFile;
            } else {
                configFile = libFile;
            }
            if (!fs.existsSync(configFile)) {
                // console.warn("# Can't find " + rootFile + " file. Skipping jshint");
                return;
            }
        }
        
        if (configFile) {
            options = fsHelper.loadJson(configFile);
        }
    }
    if (package_.isEmpty()) {
        package_.crawl();
    }
    var jsOnly = package_.filter(fileFilters.handwrittenJavascript);
    
    
    
    var files = jsOnly.filesOnDisk();
    
    
    
    
    if (argv.dryRun) {        
        _.each(files, function (file) {
            console.log("jshint " + file);
        });
        return;
    }

    var filesWithErrors = [];
    _.each(files, function (filePath) {
        
        var fileErrors = [];
        
        var errors = exports._lintFile(filePath, options);
        if (errors) {
            var filename = path.relative(package_.directory, filePath);
            filesWithErrors.push({
                filename: filename,
                errors: errors
            });
        }
        
    });
    if (!_.isEmpty(filesWithErrors)) {
        return {
            name: package_.name,
            directory: package_.directory,
            files: filesWithErrors
        };
    }
};

exports._lintFile = function (filename, options) {
    var buffer;
    try {
        buffer = fs.readFileSync(filename, 'utf-8');
    } catch (e) {
        process.stdout.write("Error: Cant open: " + filename);
        process.stdout.write(e + '\n');
        return;
    }

    // Remove potential Unicode Byte Order Mark.
    buffer = buffer.replace(/^\uFEFF/, '');
    var errors = exports._lintString(buffer, options);
    _.each(errors, function (e) {
        if (e) {
            e.file = filename;
        }
    });
    return errors;
};

exports._lintString = function (string, options) {
    if (!JSHINT(string, options)) {
        return JSHINT.errors;
    }
};

exports.display = function (packageErrors) {
    if (!packageErrors || _.isEmpty(packageErrors)) {
        return;
    }
    var displayError;
    if (argv.platform === "ios" && argv.buildType === "dev") {
        displayError = function (error) {
            if (error) {            
                // <filename>:<linenumber>: error | warn | note : <message>n
                // http://phonegap.com/2010/12/04/xcode-shell-build-phase-reporting-of-errors/<commit_msg>Behaviour change of JSHint. Not sure I believe this.<commit_after>"use strict";

var _ = require("underscore"),
    JSHINT = require("jshint").JSHINT,
    fs = require("fs"),
    path = require("path"),
    fsHelper = require("./fs-helper"),
    fileFilters = require("./file-filters"),
    argv = {};

exports.argv = function (argv_) {
    if (argv_) {
        argv = argv_;
        return exports;
    } else {
        return argv;
    }
};

exports._lintPackage = function lintPackage (package_, options) {
    // if options haven't been supplied, then we could look them up.
    if (!options || !_.isObject(options)) {
        var configFile;
        if (argv.all || argv.lintAll) {
            configFile = fsHelper.findFileSuffix(package_.directory, ".jshintrc");
        } else {
            var libFile = path.resolve(package_.directory, ".jshintrc");
            var rootFile = path.resolve(package_.packageRoot, ".jshintrc");
            if (fs.existsSync(rootFile)) {
                configFile = rootFile;
            } else {
                configFile = libFile;
            }
            if (!fs.existsSync(configFile)) {
                // console.warn("# Can't find " + rootFile + " file. Skipping jshint");
                return;
            }
        }
        
        if (configFile) {
            options = fsHelper.loadJson(configFile);
        }
    }
    if (package_.isEmpty()) {
        package_.crawl();
    }
    var jsOnly = package_.filter(fileFilters.handwrittenJavascript);
    
    
    
    var files = jsOnly.filesOnDisk();
    
    
    
    
    if (argv.dryRun) {        
        _.each(files, function (file) {
            console.log("jshint " + file);
        });
        return;
    }

    var filesWithErrors = [];
    _.each(files, function (filePath) {
        
        var fileErrors = [];
        
        var errors = exports._lintFile(filePath, options);
        if (errors) {
            var filename = path.relative(package_.directory, filePath);
            filesWithErrors.push({
                filename: filename,
                errors: errors
            });
        }
        
    });
    if (!_.isEmpty(filesWithErrors)) {
        return {
            name: package_.name,
            directory: package_.directory,
            files: filesWithErrors
        };
    }
};

exports._lintFile = function (filename, options) {
    var buffer;
    try {
        buffer = fs.readFileSync(filename, 'utf-8');
    } catch (e) {
        process.stdout.write("Error: Cant open: " + filename);
        process.stdout.write(e + '\n');
        return;
    }

    // Remove potential Unicode Byte Order Mark.
    buffer = buffer.replace(/^\uFEFF/, '');
    var errors = exports._lintString(buffer, options);
    if (errors) {
	    _.each(errors, function (e) {
	        if (e) {
	            e.file = filename;
	        }
	    });
    }
    return errors;
};

exports._lintString = function (string, options) {
    if (!JSHINT(string, options)) {
        return JSHINT.errors;
    }
};

exports.display = function (packageErrors) {
    if (!packageErrors || _.isEmpty(packageErrors)) {
        return;
    }
    var displayError;
    if (argv.platform === "ios" && argv.buildType === "dev") {
        displayError = function (error) {
            if (error) {            
                // <filename>:<linenumber>: error | warn | note : <message>n
                // http://phonegap.com/2010/12/04/xcode-shell-build-phase-reporting-of-errors/
{'fraction_non_alphanumeric': 0.13310185185185186, 'fraction_numerical': 0.0026041666666666665, 'mean_word_length': 1.7740770465489566, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '6097271', 'n_tokens_mistral': 2011, 'n_tokens_neox': 1960, 'n_words': 421}