<commit_before>/**
 * @fileoverview Responsible for loading config files
 * @author <NAME>
 */
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var fs = require("fs"),
    path = require("path"),
    environments = require("../conf/environments.json"),
    util = require("./util"),
    glob = require("glob");

var existsSync = fs.existsSync || path.existsSync;


//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var LOCAL_CONFIG_FILENAME = ".eslintrc";
var ESLINT_IGNORE_FILENAME = ".eslintignore";


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
 * Check if a given directory contains .eslintignore
 * files and if so, load and parse them
 * @param {string} directory path
 */
function loadIgnoreFile(directory) {
    var exclusions = [];
    if (existsSync(path.resolve(directory, ESLINT_IGNORE_FILENAME))) {
        //load .eslintignore file and JSON parse it
        try {
            exclusions = JSON.parse(fs.readFileSync(path.resolve(directory, ESLINT_IGNORE_FILENAME), "utf8"));
        } catch (e) {
            /* istanbul ignore next Error handling doesn't need tests*/
            throw new Error("Could not load local .eslintignore file: " + path.resolve(directory, ESLINT_IGNORE_FILENAME));
        }
    }

    return exclusions;
}

/**
 * Get a local config object.
 * @param {string} directory the directory to start looking for a local config
 * @return {object} the local config object (empty object if there is no local config)
 */
function getLocalConfig(helper, directory) {
    var localConfigFile = helper.findLocalConfigFile(directory),
        config = {};

    /* istanbul ignore else Too complicated to create unittest*/
    if (localConfigFile) {
        try {
            config = JSON.parse(fs.readFileSync(localConfigFile));
        } catch (e) {
            console.error("Cannot read config file:", localConfigFile);
            console.error("Error: ", e.message);
        }
    }

    return config;
}

//------------------------------------------------------------------------------
// API
//------------------------------------------------------------------------------

/**
 * Config
 * @constructor
 * @class Confi
 * @param {Object} options
 * @param {string} [cwd] current working directory. Defaults to process.cwd()
 */
function Config(options) {
    var useConfig;
    options = options || {};

    this.cache = {};
    this.exclusionsCache = {};
    this.baseConfig = require(path.resolve(__dirname, "..", "conf", "eslint.json"));
    this.baseConfig.format = options.format;
    useConfig = options.c || options.config;

    if (useConfig) {
        this.useSpecificConfig = require(path.resolve(process.cwd(), useConfig));
    }
}

/**
 * Build a config object merging the base config (conf/eslint.json), the
 * environments config (conf/environments.json) and eventually the user config.
 * @param {string} directory the directory to start looking for a local config
 * @return {object} config object
 */
Config.prototype.getConfig = function (directory) {
    var config,
        userConfig,
        envConfig = {};

    directory = path.dirname(directory);
    config    = this.cache[directory];

    if (config) {
        return config;
    }

    if (this.useSpecificConfig) {
        userConfig = this.useSpecificConfig;
    } else {
        userConfig = getLocalConfig(this, directory);
    }

    if (userConfig.env) {
        envConfig.rules = {};

        Object.keys(userConfig.env).forEach(function (name) {
            var environment = environments[name].rules;
            if (userConfig.env[name] && environment) {
                Object.keys(environment).forEach(function(name) {
                    envConfig.rules[name] = environment[name];
                });
            }
        });
    }

    config = this.mergeConfigs(Object.create(this.baseConfig), envConfig);
    config = this.mergeConfigs(config, userConfig);<commit_msg>Fixed: Config.getConfig is called either with a file path or with no args (fixes #520)<commit_after>/**
 * @fileoverview Responsible for loading config files
 * @author <NAME>
 */
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

var fs = require("fs"),
    path = require("path"),
    environments = require("../conf/environments.json"),
    util = require("./util"),
    glob = require("glob");

var existsSync = fs.existsSync || path.existsSync;


//------------------------------------------------------------------------------
// Constants
//------------------------------------------------------------------------------

var LOCAL_CONFIG_FILENAME = ".eslintrc";
var ESLINT_IGNORE_FILENAME = ".eslintignore";


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------

/**
 * Check if a given directory contains .eslintignore
 * files and if so, load and parse them
 * @param {string} directory path
 */
function loadIgnoreFile(directory) {
    var exclusions = [];
    if (existsSync(path.resolve(directory, ESLINT_IGNORE_FILENAME))) {
        //load .eslintignore file and JSON parse it
        try {
            exclusions = JSON.parse(fs.readFileSync(path.resolve(directory, ESLINT_IGNORE_FILENAME), "utf8"));
        } catch (e) {
            /* istanbul ignore next Error handling doesn't need tests*/
            throw new Error("Could not load local .eslintignore file: " + path.resolve(directory, ESLINT_IGNORE_FILENAME));
        }
    }

    return exclusions;
}

/**
 * Get a local config object.
 * @param {string} directory the directory to start looking for a local config
 * @return {object} the local config object (empty object if there is no local config)
 */
function getLocalConfig(helper, directory) {
    var localConfigFile = helper.findLocalConfigFile(directory),
        config = {};

    /* istanbul ignore else Too complicated to create unittest*/
    if (localConfigFile) {
        try {
            config = JSON.parse(fs.readFileSync(localConfigFile));
        } catch (e) {
            console.error("Cannot read config file:", localConfigFile);
            console.error("Error: ", e.message);
        }
    }

    return config;
}

//------------------------------------------------------------------------------
// API
//------------------------------------------------------------------------------

/**
 * Config
 * @constructor
 * @class Confi
 * @param {Object} options
 * @param {string} [cwd] current working directory. Defaults to process.cwd()
 */
function Config(options) {
    var useConfig;
    options = options || {};

    this.cache = {};
    this.exclusionsCache = {};
    this.baseConfig = require(path.resolve(__dirname, "..", "conf", "eslint.json"));
    this.baseConfig.format = options.format;
    useConfig = options.c || options.config;

    if (useConfig) {
        this.useSpecificConfig = require(path.resolve(process.cwd(), useConfig));
    }
}

/**
 * Build a config object merging the base config (conf/eslint.json), the
 * environments config (conf/environments.json) and eventually the user config.
 * @param {string} filePath a file in whose directory we start looking for a local config
 * @return {object} config object
 */
Config.prototype.getConfig = function (filePath) {
    var config,
        userConfig,
        envConfig = {},
        directory = filePath ? path.dirname(filePath) : process.eslintCwd || process.cwd();

    config = this.cache[directory];

    if (config) {
        return config;
    }

    if (this.useSpecificConfig) {
        userConfig = this.useSpecificConfig;
    } else {
        userConfig = getLocalConfig(this, directory);
    }

    if (userConfig.env) {
        envConfig.rules = {};

        Object.keys(userConfig.env).forEach(function (name) {
            var environment = environments[name].rules;
            if (userConfig.env[name] && environment) {
                Object.keys(environment).forEach(function(name) {
                    envConfig.rules[name] = environment[name];
                });
            }
        });
    }

    config = this.mergeConfigs(Object.create(this.baseConfig), envConfig);
    config = this.mergeConfigs(config, userConfig);
{'fraction_non_alphanumeric': 0.25105798924854167, 'fraction_numerical': 0.0005718860802928057, 'mean_word_length': 1.6987654320987655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1144781', 'n_tokens_mistral': 2198, 'n_tokens_neox': 2124, 'n_words': 565}