Skip to content
Permalink
Browse files

Update: config extends dependency lookup (fixes #5023)

  • Loading branch information...
nzakas committed Jan 22, 2016
1 parent dc07d4e commit e3f2683e3698f2be248ef5b1a2508b988203a875
Showing with 182 additions and 571 deletions.
  1. +29 −45 lib/config/config-file.js
  2. +0 −36 tests/lib/cli.js
  3. +0 −466 tests/lib/config.js
  4. +153 −24 tests/lib/config/config-file.js
@@ -114,7 +114,7 @@ function loadLegacyConfigFile(filePath) {
var yaml = require("js-yaml");

try {
return yaml.safeLoad(stripComments(readFile(filePath))) || {};
return yaml.safeLoad(stripComments(readFile(filePath))) || /* istanbul ignore next */ {};
} catch (e) {
debug("Error reading YAML file: " + filePath);
e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
@@ -158,24 +158,6 @@ function loadPackageJSONConfigFile(filePath) {
}
}

/**
* Loads a JavaScript configuration from a package.
* @param {string} filePath The package name to load.
* @returns {Object} The configuration object from the package.
* @throws {Error} If the package cannot be read.
* @private
*/
function loadPackage(filePath) {
debug("Loading config package: " + filePath);
try {
return require(filePath);
} catch (e) {
debug("Error reading package: " + filePath);
e.message = "Cannot read config package: " + filePath + "\nError: " + e.message;
throw e;
}
}

/**
* Loads a configuration file regardless of the source. Inspects the file path
* to determine the correctly way to load the config file.
@@ -186,33 +168,29 @@ function loadPackage(filePath) {
function loadConfigFile(filePath) {
var config;

if (isFilePath(filePath)) {
switch (path.extname(filePath)) {
case ".js":
config = loadJSConfigFile(filePath);
break;

case ".json":
if (path.basename(filePath) === "package.json") {
config = loadPackageJSONConfigFile(filePath);
if (config === null) {
return null;
}
} else {
config = loadJSONConfigFile(filePath);
switch (path.extname(filePath)) {
case ".js":
config = loadJSConfigFile(filePath);
break;

case ".json":
if (path.basename(filePath) === "package.json") {
config = loadPackageJSONConfigFile(filePath);
if (config === null) {
return null;
}
break;
} else {
config = loadJSONConfigFile(filePath);
}
break;

case ".yaml":
case ".yml":
config = loadYAMLConfigFile(filePath);
break;
case ".yaml":
case ".yml":
config = loadYAMLConfigFile(filePath);
break;

default:
config = loadLegacyConfigFile(filePath);
}
} else {
config = loadPackage(filePath);
default:
config = loadLegacyConfigFile(filePath);
}

return ConfigOps.merge(ConfigOps.createEmptyConfig(), config);
@@ -344,13 +322,14 @@ function applyExtends(config, filePath) {
* Resolves a configuration file path into the fully-formed path, whether filename
* or package name.
* @param {string} filePath The filepath to resolve.
* @param {string} [relativeTo] The path to resolve relative to.
* @returns {string} A path that can be used directly to load the configuration.
* @private
*/
function resolve(filePath) {
function resolve(filePath, relativeTo) {

if (isFilePath(filePath)) {
return path.resolve(filePath);
return path.resolve(relativeTo || "", filePath);
} else {

// it's a package
@@ -371,6 +350,11 @@ function resolve(filePath) {
filePath = "eslint-config-" + filePath;
}

// some additional information processing is necessary
filePath = resolveModule.sync(filePath, {
basedir: relativeTo || process.cwd()
});

return filePath;
}

@@ -234,42 +234,6 @@ describe("cli", function() {
});
});

describe("when given a config that is a sharable config", function() {
it("should execute without any errors", function() {

var configDeps = {
"is-resolvable": function() {
return true;
}
};
configDeps["./config/config-file"] = proxyquire("../../lib/config/config-file", {
"eslint-config-xo": {
rules: {
"no-var": 2
}
}
});

var StubbedConfig = proxyquire("../../lib/config", configDeps);

var stubbedCLIEngine = proxyquire("../../lib/cli-engine", {
"./config": StubbedConfig
});
var stubCli = proxyquire("../../lib/cli", {
"./cli-engine": stubbedCLIEngine,
"./logging": log
});
var configPath = "xo";
var filePath = getFixturePath("passing.js");
var code = "--no-ignore --config " + configPath + " " + filePath;

var exit = stubCli.execute(code);

assert.equal(exit, 1);
assert.isTrue(log.info.called);
});
});

describe("when given a valid built-in formatter name", function() {
it("should execute without any errors", function() {
var filePath = getFixturePath("passing.js");
Oops, something went wrong.

0 comments on commit e3f2683

Please sign in to comment.
You can’t perform that action at this time.