Skip to content

Commit

Permalink
Fix easy CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvan Mably committed Mar 6, 2017
1 parent 791bdbc commit be3539b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/config.js
Expand Up @@ -118,13 +118,13 @@ class Config {
};

this.cliConfig = {};
Object.keys(cliConfigOptions).forEach(function(configKey) {
Object.keys(cliConfigOptions).forEach(configKey => {
const value = cliConfigOptions[configKey];

if (value) {
this.cliConfig[configKey] = value;
}
}, this);
});
}

/**
Expand Down Expand Up @@ -290,7 +290,7 @@ class Config {
return;
}

const relativePath = (filePath || directory).substr(config.baseDirectory.length + 1);
const relativePath = path.relative(config.baseDirectory, filePath || directory);

overrides.forEach((override, i) => {
if (ConfigOps.pathMatchesGlobs(relativePath, override.files)) {
Expand Down
3 changes: 1 addition & 2 deletions lib/config/config-file.js
Expand Up @@ -567,13 +567,12 @@ function loadObject(configObject) {
*/
function loadCached(filePath) {
const cachedConfig = configCache.getConfig(filePath);
let config = {};

if (cachedConfig) {
return cachedConfig;
}

config = load(filePath);
const config = load(filePath);

if (config) {
config.filePath = filePath;
Expand Down
15 changes: 11 additions & 4 deletions lib/config/config-ops.js
Expand Up @@ -11,7 +11,8 @@

const Environments = require("./environments"),
configCache = require("./config-cache"),
minimatch = require("minimatch");
minimatch = require("minimatch"),
path = require("path");

const debug = require("debug")("eslint:config-ops");

Expand Down Expand Up @@ -289,12 +290,13 @@ module.exports = {
nearestCacheIndex = vector.length - 1;

// Check the merged vector config cache to try to find a merged config for the current config or a parent
for (; nearestCacheIndex >= 0; nearestCacheIndex--) {
while (nearestCacheIndex >= 0) {
config = configCache.getMergedVectorConfig(subvector);
if (config) {
break;
}
subvector.pop();
nearestCacheIndex--;
}

if (config) {
Expand Down Expand Up @@ -349,7 +351,12 @@ module.exports = {
* @returns {boolean} True if all the supplied patterns match the file path, false otherwise
*/
pathMatchesGlobs(filePath, patterns) {
patterns = [].concat(patterns);
return patterns.every(pattern => minimatch(filePath, pattern, { matchBase: true }));
const validPatterns = [].concat(patterns).filter(
pattern => !path.isAbsolute(pattern) && !pattern.includes("..")
);

return validPatterns.length > 0 && validPatterns.every(
pattern => minimatch(filePath, pattern, { matchBase: true })
);
}
};
26 changes: 26 additions & 0 deletions tests/lib/config.js
Expand Up @@ -1157,6 +1157,32 @@ describe("Config", () => {
assertConfigsEqual(actual, expected);
});

it("should not merge override config when the pattern traverses up the directory tree", () => {
const targetPath = getFakeFixturePath("overrides", "bar.js");
const parentPath = "overrides/../**/*.js";

const config = new Config({
cwd: process.cwd(),
baseConfig: {
overrides: [{
files: parentPath,
rules: {
quotes: [2, "single"]
}
}],
useEslintrc: false
}
});
const expected = {
rules: {
quotes: [2, "double"]
}
};
const actual = config.getConfig(targetPath);

assertConfigsEqual(actual, expected);
});

it("should merge all local configs (override and non-override) before non-local configs", () => {
const config = new Config({ cwd: process.cwd() });
const targetPath = getFakeFixturePath("overrides", "two", "child-two.js");
Expand Down

0 comments on commit be3539b

Please sign in to comment.