From 21d7561ecd961737b07b7e9cb5ee941c8665cb9c Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Sun, 15 Oct 2017 18:32:16 +1100 Subject: [PATCH] Resolve file paths relative to config file (#3037) * Add currently passing test case for #3005 * Fix test to fail without code change --- src/resolve-config.js | 21 ++++++++++++++----- .../__tests__/config-resolution.js | 8 +++++++ .../cli/config/filepath/.prettierrc | 11 ++++++++++ .../cli/config/filepath/subfolder/file.js | 0 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 tests_integration/cli/config/filepath/.prettierrc create mode 100644 tests_integration/cli/config/filepath/subfolder/file.js diff --git a/src/resolve-config.js b/src/resolve-config.js index 72b9ea85..4eb3e74f 100644 --- a/src/resolve-config.js +++ b/src/resolve-config.js @@ -2,6 +2,7 @@ const cosmiconfig = require("cosmiconfig"); const minimatch = require("minimatch"); +const path = require("path"); const mem = require("mem"); const getExplorerMemoized = mem(opts => @@ -23,7 +24,7 @@ function resolveConfig(filePath, opts) { opts = Object.assign({ useCache: true }, opts); const load = getLoadFunction({ cache: !!opts.useCache, sync: false }); return load(filePath, opts.config).then(result => { - return !result ? null : mergeOverrides(result.config, filePath); + return !result ? null : mergeOverrides(result, filePath); }); } @@ -31,7 +32,7 @@ resolveConfig.sync = (filePath, opts) => { opts = Object.assign({ useCache: true }, opts); const load = getLoadFunction({ cache: !!opts.useCache, sync: true }); const result = load(filePath, opts.config); - return !result ? null : mergeOverrides(result.config, filePath); + return !result ? null : mergeOverrides(result, filePath); }; function clearCache() { @@ -51,11 +52,21 @@ resolveConfigFile.sync = filePath => { return result ? result.filepath : null; }; -function mergeOverrides(config, filePath) { - const options = Object.assign({}, config); +function mergeOverrides(configResult, filePath) { + const options = Object.assign({}, configResult.config); if (filePath && options.overrides) { + const relativeFilePath = path.relative( + path.dirname(configResult.filepath), + filePath + ); for (const override of options.overrides) { - if (pathMatchesGlobs(filePath, override.files, override.excludeFiles)) { + if ( + pathMatchesGlobs( + relativeFilePath, + override.files, + override.excludeFiles + ) + ) { Object.assign(options, override.options); } } diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index dcd3e48c..4048caad 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -101,3 +101,11 @@ test("API resolveConfig.sync with file arg and extension override", () => { semi: true }); }); + +test("API resolveConfig.sync overrides work with absolute paths", () => { + // Absolute path + const file = path.join(__dirname, "../cli/config/filepath/subfolder/file.js"); + expect(prettier.resolveConfig.sync(file)).toMatchObject({ + tabWidth: 6 + }); +}); diff --git a/tests_integration/cli/config/filepath/.prettierrc b/tests_integration/cli/config/filepath/.prettierrc new file mode 100644 index 00000000..91bebcd8 --- /dev/null +++ b/tests_integration/cli/config/filepath/.prettierrc @@ -0,0 +1,11 @@ +{ + "tabWidth": 3, + "overrides": [ + { + "files": "subfolder/file.js", + "options": { + "tabWidth": 6 + } + } + ] +} diff --git a/tests_integration/cli/config/filepath/subfolder/file.js b/tests_integration/cli/config/filepath/subfolder/file.js new file mode 100644 index 00000000..e69de29b