From 434de7f5999bab18b3f53eb321b29eea9f62ea59 Mon Sep 17 00:00:00 2001 From: Toru Nagashima Date: Tue, 21 Jun 2016 01:20:12 +0900 Subject: [PATCH] Fix: wrong baseDir (fixes #6450) (#6457) --- lib/config/config-file.js | 5 +- .../extends-chain-2/parser.eslintrc.json | 3 + .../extends-chain-2/relative.eslintrc.json | 3 + tests/lib/config/config-file.js | 73 +++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/config-file/extends-chain-2/parser.eslintrc.json create mode 100644 tests/fixtures/config-file/extends-chain-2/relative.eslintrc.json diff --git a/lib/config/config-file.js b/lib/config/config-file.js index e68591874c0..4e25887fd47 100644 --- a/lib/config/config-file.js +++ b/lib/config/config-file.js @@ -495,7 +495,6 @@ function resolve(filePath, relativeTo) { function load(filePath, applyEnvironments, relativeTo) { var resolvedPath = resolve(filePath, relativeTo), dirname = path.dirname(resolvedPath.filePath), - basedir = getBaseDir(dirname), lookupPath = getLookupPath(dirname), config = loadConfigFile(resolvedPath); @@ -514,7 +513,7 @@ function load(filePath, applyEnvironments, relativeTo) { // include full path of parser if present if (config.parser) { if (isFilePath(config.parser)) { - config.parser = path.resolve(basedir || "", config.parser); + config.parser = path.resolve(dirname || "", config.parser); } else { config.parser = resolver.resolve(config.parser, lookupPath); } @@ -528,7 +527,7 @@ function load(filePath, applyEnvironments, relativeTo) { * a "parent". Load the referenced file and merge the configuration recursively. */ if (config.extends) { - config = applyExtends(config, filePath, basedir); + config = applyExtends(config, filePath, dirname); } if (config.env && applyEnvironments) { diff --git a/tests/fixtures/config-file/extends-chain-2/parser.eslintrc.json b/tests/fixtures/config-file/extends-chain-2/parser.eslintrc.json new file mode 100644 index 00000000000..649fbc6c3d0 --- /dev/null +++ b/tests/fixtures/config-file/extends-chain-2/parser.eslintrc.json @@ -0,0 +1,3 @@ +{ + "parser": "./parser.js" +} diff --git a/tests/fixtures/config-file/extends-chain-2/relative.eslintrc.json b/tests/fixtures/config-file/extends-chain-2/relative.eslintrc.json new file mode 100644 index 00000000000..ad711c30b2c --- /dev/null +++ b/tests/fixtures/config-file/extends-chain-2/relative.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": "./node_modules/eslint-config-a/index.js" +} diff --git a/tests/lib/config/config-file.js b/tests/lib/config/config-file.js index bfb80f562c8..06a35d778d0 100644 --- a/tests/lib/config/config-file.js +++ b/tests/lib/config/config-file.js @@ -17,6 +17,7 @@ var assert = require("chai").assert, yaml = require("js-yaml"), userHome = require("user-home"), proxyquire = require("proxyquire"), + shell = require("shelljs"), environments = require("../../../conf/environments"), ConfigFile = require("../../../lib/config/config-file"); @@ -657,6 +658,78 @@ describe("ConfigFile", function() { }); }); + it("should load information from `extends` chain in .eslintrc 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 + } + }); + }); + + it("should load information from `parser` in .eslintrc with relative path.", function() { + var config = ConfigFile.load(getFixturePath("extends-chain-2/parser.eslintrc.json")); + var parserPath = getFixturePath("extends-chain-2/parser.js"); + + assert.deepEqual(config, { + env: {}, + globals: {}, + parser: parserPath, + parserOptions: {}, + rules: {} + }); + }); + + describe("even if it's in another directory,", function() { + var fixturePath = ""; + + before(function() { + var tempDir = temp.mkdirSync("eslint-test-chain"); + var chain2 = getFixturePath("extends-chain-2"); + + fixturePath = path.join(tempDir, "extends-chain-2"); + shell.cp("-r", chain2, fixturePath); + }); + + after(function() { + temp.cleanupSync(); + }); + + it("should load information from `extends` chain in .eslintrc with relative path.", function() { + var config = ConfigFile.load(path.join(fixturePath, "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 + } + }); + }); + + it("should load information from `parser` in .eslintrc with relative path.", function() { + var config = ConfigFile.load(path.join(fixturePath, "parser.eslintrc.json")); + var parserPath = path.join(fixturePath, "parser.js"); + + assert.deepEqual(config, { + env: {}, + globals: {}, + parser: parserPath, + parserOptions: {}, + rules: {} + }); + }); + }); + describe("Plugins", function() { it("should load information from a YML file and load plugins", function() {