Skip to content

Commit

Permalink
Fix: extends chain (fixes #5411)
Browse files Browse the repository at this point in the history
- I added a parameter `relativeTo` to `load` and `applyExtends`, then
set the directory of the config file to the parameter.
  • Loading branch information
mysticatea committed Mar 3, 2016
1 parent 2225616 commit 50f4d5a
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 11 deletions.
20 changes: 9 additions & 11 deletions lib/config/config-file.js
Expand Up @@ -304,7 +304,6 @@ function write(config, filePath) {
* @private
*/
function getLookupPath(configFilePath) {

// calculates the path of the project including ESLint as dependency
var projectPath = path.resolve(__dirname, "../../../");
if (configFilePath && pathIsInside(configFilePath, projectPath)) {
Expand All @@ -322,11 +321,12 @@ function getLookupPath(configFilePath) {
* @param {Object} config The configuration information.
* @param {string} filePath The file path from which the configuration information
* was loaded.
* @param {string} [relativeTo] The path to resolve relative to.
* @returns {Object} A new configuration object with all of the "extends" fields
* loaded and merged.
* @private
*/
function applyExtends(config, filePath) {
function applyExtends(config, filePath, relativeTo) {
var configExtends = config.extends;

// normalize into an array for easier handling
Expand All @@ -352,7 +352,7 @@ function applyExtends(config, filePath) {

try {
debug("Loading " + parentPath);
return ConfigOps.merge(load(parentPath), previousValue);
return ConfigOps.merge(load(parentPath, false, relativeTo), previousValue);
} catch (e) {
// If the file referenced by `extends` failed to load, add the path to the
// configuration file that referenced it to the error message so the user is
Expand Down Expand Up @@ -402,7 +402,6 @@ function normalizePackageName(name, prefix) {
* @private
*/
function resolve(filePath, relativeTo) {

if (isFilePath(filePath)) {
return { filePath: path.resolve(relativeTo || "", filePath) };
} else {
Expand All @@ -428,12 +427,13 @@ function resolve(filePath, relativeTo) {
* @param {string} filePath The filename or package name to load the configuration
* information from.
* @param {boolean} [applyEnvironments=false] Set to true to merge in environment settings.
* @param {string} [relativeTo] The path to resolve relative to.
* @returns {Object} The configuration information.
* @private
*/
function load(filePath, applyEnvironments) {

var resolvedPath = resolve(filePath),
function load(filePath, applyEnvironments, relativeTo) {
var resolvedPath = resolve(filePath, relativeTo),
basedir = getLookupPath(path.dirname(resolvedPath.filePath)),
config = loadConfigFile(resolvedPath);

if (config) {
Expand All @@ -450,9 +450,7 @@ function load(filePath, applyEnvironments) {

// include full path of parser if present
if (config.parser) {
config.parser = resolveModule.sync(config.parser, {
basedir: getLookupPath(path.dirname(path.resolve(filePath)))
});
config.parser = resolveModule.sync(config.parser, {basedir: basedir});
}

// validate the configuration before continuing
Expand All @@ -461,7 +459,7 @@ function load(filePath, applyEnvironments) {
// If an `extends` property is defined, it represents a configuration file to use as
// a "parent". Load the referenced file and merge the configuration recursively.
if (config.extends) {
config = applyExtends(config, filePath);
config = applyExtends(config, filePath, basedir);
}

if (config.env && applyEnvironments) {
Expand Down
@@ -0,0 +1,3 @@
{
"extends": "a"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/fixtures/config-file/extends-chain/.eslintrc.json
@@ -0,0 +1,3 @@
{
"extends": "a"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/lib/config/config-file.js
Expand Up @@ -608,6 +608,20 @@ describe("ConfigFile", function() {
});
});

it("should load information from `extends` chain.", function() {
var config = ConfigFile.load(getFixturePath("extends-chain/.eslintrc.json"));
assert.deepEqual(config, {
env: {},
extends: "a",
globals: {},
parserOptions: {},
rules: {
a: 2, // from node_modules/eslint-config-a
b: 2, // from node_modules/eslint-config-a/node_modules/eslint-config-b
c: 2 // from node_modules/eslint-config-a/node_modules/eslint-config-b/node_modules/eslint-config-c
}
});
});

describe("Plugins", function() {

Expand Down

0 comments on commit 50f4d5a

Please sign in to comment.