Skip to content

Commit

Permalink
Chore: refactor populating configs with defaults in linter (#10006)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 24, 2018
1 parent aea07dc commit a1c3759
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 59 deletions.
29 changes: 0 additions & 29 deletions conf/default-config-options.js

This file was deleted.

49 changes: 22 additions & 27 deletions lib/linter.js
Expand Up @@ -14,7 +14,6 @@ const eslintScope = require("eslint-scope"),
levn = require("levn"),
lodash = require("lodash"),
blankScriptAST = require("../conf/blank-script.json"),
defaultConfig = require("../conf/default-config-options.js"),
CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"),
ConfigOps = require("./config/config-ops"),
validator = require("./config/config-validator"),
Expand All @@ -33,6 +32,7 @@ const eslintScope = require("eslint-scope"),

const debug = require("debug")("eslint:linter");
const MAX_AUTOFIX_PASSES = 10;
const DEFAULT_PARSER_NAME = "espree";

//------------------------------------------------------------------------------
// Typedefs
Expand Down Expand Up @@ -415,49 +415,44 @@ function normalizeEcmaVersion(ecmaVersion, isModule) {
}

/**
* Process initial config to make it safe to extend by file comment config
* Populates a config file with the default values, and merges any parserOptions stored in an env into
* the parserOptions of the populated config.
* @param {Object} config Initial config
* @param {Environments} envContext Env context
* @returns {Object} Processed config
* @returns {Object} Processed config
*/
function prepareConfig(config, envContext) {
config.globals = config.globals || {};
const copiedRules = {};
let parserOptions = {};

function populateConfig(config, envContext) {
if (typeof config.rules === "object") {
Object.keys(config.rules).forEach(k => {
const rule = config.rules[k];

if (rule === null) {
throw new Error(`Invalid config for rule '${k}'.`);
}
if (Array.isArray(rule)) {
copiedRules[k] = rule.slice();
} else {
copiedRules[k] = rule;
}
});
}

// merge in environment parserOptions
if (typeof config.env === "object") {
Object.keys(config.env).forEach(envName => {
const envNamesEnabledInConfig = typeof config.env === "object"
? Object.keys(config.env).filter(envName => config.env[envName])
: [];

const parserOptionsFromEnv = envNamesEnabledInConfig
.reduce((parserOptions, envName) => {
const env = envContext.get(envName);

if (config.env[envName] && env && env.parserOptions) {
parserOptions = ConfigOps.merge(parserOptions, env.parserOptions);
}
});
}
return env && env.parserOptions
? ConfigOps.merge(parserOptions, env.parserOptions)
: parserOptions;
}, {});

const preparedConfig = {
rules: copiedRules,
parser: config.parser || defaultConfig.parser,
globals: ConfigOps.merge(defaultConfig.globals, config.globals),
env: ConfigOps.merge(defaultConfig.env, config.env || {}),
settings: ConfigOps.merge(defaultConfig.settings, config.settings || {}),
parserOptions: ConfigOps.merge(parserOptions, config.parserOptions || {})
rules: config.rules || {},
parser: config.parser || DEFAULT_PARSER_NAME,
globals: config.globals || {},
env: config.env || {},
settings: config.settings || {},
parserOptions: ConfigOps.merge(parserOptionsFromEnv, config.parserOptions || {})
};
const isModule = preparedConfig.parserOptions.sourceType === "module";

Expand Down Expand Up @@ -810,7 +805,7 @@ module.exports = class Linter {
}

// process initial config to make it safe to extend
config = prepareConfig(config, this.environments);
config = populateConfig(config, this.environments);

if (!lastSourceCodes.get(this)) {

Expand Down
4 changes: 1 addition & 3 deletions tests/fixtures/config-file/js/.eslintrc.parser3.js
@@ -1,7 +1,5 @@
var defaultOptions = require("../../../../conf/default-config-options");

module.exports = {
parser: defaultOptions.parser,
parser: "espree",
rules: {
semi: [2, "always"]
}
Expand Down

0 comments on commit a1c3759

Please sign in to comment.