Skip to content

Commit

Permalink
Fix: Ensure config extends reads from the right spot (fixes #5450)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Jun 18, 2016
1 parent 414206c commit aec339a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
37 changes: 32 additions & 5 deletions lib/config/config-file.js
Expand Up @@ -377,18 +377,35 @@ function applyExtends(config, filePath, relativeTo) {
parentPath = path.resolve(__dirname, "../../conf/eslint-all.js");
} else if (isFilePath(parentPath)) {

debug("parentPath before:" + parentPath);

/*
* If the `extends` path is relative, use the directory of the current configuration
* file as the reference point. Otherwise, use as-is.
*/
parentPath = (!isAbsolutePath(parentPath) ?
path.join(relativeTo || path.dirname(filePath), parentPath) :
parentPath
);
if (!isAbsolutePath(parentPath)) {
var dirname = path.dirname(filePath);

/*
* Special case: if filePath is a package name (such as "foo" for eslint-config-foo)
* then dirname will end up as just ".". When you pass "." into path.join(), it is
* treated differently so if parentPath is "./foo.js" it becomes "foo.js". When that
* happens, the rest of the logic breaks because "foo.js" looks like a package name.
* So, we have to just not do the join in that situation. See:
* https://github.com/eslint/eslint/issues/6450
* https://github.com/eslint/eslint/issues/6358
*/
if (dirname !== ".") {
parentPath = path.join(dirname, parentPath);
}

}

debug("parentPath after:" + parentPath);
}

try {
debug("Loading " + parentPath);
debug("Loading " + parentPath + " relative to " + relativeTo);
return ConfigOps.merge(load(parentPath, false, relativeTo), previousValue);
} catch (e) {

Expand Down Expand Up @@ -460,11 +477,16 @@ function normalizePackageName(name, prefix) {
* @private
*/
function resolve(filePath, relativeTo) {
debug("resolve(): " + filePath + "," + relativeTo);

if (isFilePath(filePath)) {
debug("Path to resolve is a file path:" + filePath);
return { filePath: path.resolve(relativeTo || "", filePath) };
} else {
var normalizedPackageName;

debug("filePath " + filePath + " is not a filepath, must be a package name");

if (filePath.indexOf("plugin:") === 0) {
var packagePath = filePath.substr(7, filePath.lastIndexOf("/") - 7);
var configName = filePath.substr(filePath.lastIndexOf("/") + 1, filePath.length - filePath.lastIndexOf("/") - 1);
Expand Down Expand Up @@ -493,12 +515,17 @@ function resolve(filePath, relativeTo) {
* @private
*/
function load(filePath, applyEnvironments, relativeTo) {

debug("load(): " + filePath + ", " + relativeTo);

var resolvedPath = resolve(filePath, relativeTo),
dirname = path.dirname(resolvedPath.filePath),
basedir = getBaseDir(dirname),
lookupPath = getLookupPath(dirname),
config = loadConfigFile(resolvedPath);

debug("load(): Resolved path is " + JSON.stringify(resolvedPath));

if (config) {

// ensure plugins are properly loaded first
Expand Down
@@ -0,0 +1,3 @@
{
"extends": "./node_modules/eslint-config-a/index.js"
}
17 changes: 16 additions & 1 deletion tests/lib/config/config-file.js
Expand Up @@ -642,7 +642,7 @@ describe("ConfigFile", function() {
});
});

it("should load information from `extends` chain with relative path.", function() {
it("should load information from `extends` chain in .eslintrc with relative path.", function() {
var config = ConfigFile.load(getFixturePath("extends-chain-2/.eslintrc.json"));

assert.deepEqual(config, {
Expand All @@ -657,6 +657,21 @@ describe("ConfigFile", function() {
});
});

it("should load information from `extends` chain in non-.eslintrc file with relative path.", function() {
var config = ConfigFile.load(getFixturePath("extends-chain-2/relative.eslintrc.json"));

assert.deepEqual(config, {
env: {},
extends: "./node_modules/eslint-config-a/index.js",
globals: {},
parserOptions: {},
rules: {
a: 2, // from node_modules/eslint-config-a/index.js
relative: 2 // from node_modules/eslint-config-a/relative.js
}
});
});

describe("Plugins", function() {

it("should load information from a YML file and load plugins", function() {
Expand Down

0 comments on commit aec339a

Please sign in to comment.