Resolve file paths relative to config file (#3037)
* Add currently passing test case for #3005 * Fix test to fail without code changemaster
parent
c27cc7ff45
commit
21d7561ecd
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"tabWidth": 3,
|
||||
"overrides": [
|
||||
{
|
||||
"files": "subfolder/file.js",
|
||||
"options": {
|
||||
"tabWidth": 6
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue