Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ensure configs from a plugin are cached separately (fixes #8792) #8798

Merged
merged 3 commits into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/config/config-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ function hash(vector) {
class ConfigCache {

constructor() {
this.filePathCache = new Map();
this.configFullNameCache = new Map();
this.localHierarchyCache = new Map();
this.mergedVectorCache = new Map();
this.mergedCache = new Map();
}

/**
* Gets a config object from the cache for the specified config file path.
* @param {string} configFilePath the absolute path to the config file
* @param {string} configFullName the absolute path to the config file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this line be updated to match lib/config/config-file.js lines 491-492?

* @returns {Object|null} config object, if found in the cache, otherwise null
* @private
*/
getConfig(configFilePath) {
return this.filePathCache.get(configFilePath);
getConfig(configFullName) {
return this.configFullNameCache.get(configFullName);
}

/**
* Sets a config object in the cache for the specified config file path.
* @param {string} configFilePath the absolute path to the config file
* @param {string} configFullName the absolute path to the config file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this line be updated to match lib/config/config-file.js lines 491-492?

* @param {Object} config the config object to add to the cache
* @returns {void}
* @private
*/
setConfig(configFilePath, config) {
this.filePathCache.set(configFilePath, config);
setConfig(configFullName, config) {
this.configFullNameCache.set(configFullName, config);
}

/**
Expand Down
15 changes: 9 additions & 6 deletions lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,15 @@ function normalizePackageName(name, prefix) {
* @returns {Object} An object containing 3 properties:
* - 'filePath' (required) the resolved path that can be used directly to load the configuration.
* - 'configName' the name of the configuration inside the plugin.
* - 'configFullName' the name of the configuration as used in the eslint config (e.g. 'plugin:node/recommended').
* - 'configFullName' (required) the name of the configuration as used in the eslint config(e.g. 'plugin:node/recommended'),
* or the absolute path to a config file. This should uniquely identify a config.
* @private
*/
function resolve(filePath, relativeTo) {
if (isFilePath(filePath)) {
return { filePath: path.resolve(relativeTo || "", filePath) };
const fullPath = path.resolve(relativeTo || "", filePath);

return { filePath: fullPath, configFullName: fullPath };
}
let normalizedPackageName;

Expand All @@ -510,7 +513,7 @@ function resolve(filePath, relativeTo) {
normalizedPackageName = normalizePackageName(filePath, "eslint-config");
debug(`Attempting to resolve ${normalizedPackageName}`);
filePath = resolver.resolve(normalizedPackageName, getLookupPath(relativeTo));
return { filePath };
return { filePath, configFullName: filePath };


}
Expand All @@ -524,7 +527,7 @@ function resolve(filePath, relativeTo) {
function loadFromDisk(resolvedPath, configContext) {
const dirname = path.dirname(resolvedPath.filePath),
lookupPath = getLookupPath(dirname);
let config = loadConfigFile(resolvedPath);
let config = loadConfigFile(resolvedPath, configContext);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadConfigFile doesn't seem to take a second argument. Was this change deliberate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this was unintentional, thanks. While working on this I initially considered adding a second argument to loadConfigFile, but then I forgot to remove it here.


if (config) {

Expand Down Expand Up @@ -579,7 +582,7 @@ function loadObject(configObject) {
function load(filePath, configContext, relativeTo) {
const resolvedPath = resolve(filePath, relativeTo);

const cachedConfig = configContext.configCache.getConfig(resolvedPath.filePath);
const cachedConfig = configContext.configCache.getConfig(resolvedPath.configFullName);

if (cachedConfig) {
return cachedConfig;
Expand All @@ -590,7 +593,7 @@ function load(filePath, configContext, relativeTo) {
if (config) {
config.filePath = resolvedPath.filePath;
config.baseDirectory = path.dirname(resolvedPath.filePath);
configContext.configCache.setConfig(resolvedPath.filePath, config);
configContext.configCache.setConfig(resolvedPath.configFullName, config);
}

return config;
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/config-file/plugins/.eslintrc2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extends:
- plugin:test/foo
- plugin:test/bar
43 changes: 43 additions & 0 deletions tests/lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,49 @@ describe("ConfigFile", () => {
}
});
});

it("should load two separate configs from a plugin", () => {
const stubConfig = new Config({}, new Linter());
const resolvedPath = path.resolve(PROJECT_PATH, "./node_modules/eslint-plugin-test/index.js");

const configDeps = {
"../util/module-resolver": createStubModuleResolver({
"eslint-plugin-test": resolvedPath
}),
"require-uncached"(filename) {
return configDeps[filename];
}
};

configDeps[resolvedPath] = {
configs: {
foo: { rules: { semi: 2, quotes: 1 } },
bar: { rules: { quotes: 2, yoda: 2 } }
}
};

const StubbedConfigFile = proxyquire("../../../lib/config/config-file", configDeps);

const configFilePath = getFixturePath("plugins/.eslintrc2.yml");
const config = StubbedConfigFile.load(configFilePath, stubConfig);

assert.deepEqual(config, {
baseDirectory: path.dirname(configFilePath),
filePath: configFilePath,
parserOptions: {},
globals: {},
env: {},
rules: {
semi: 2,
quotes: 2,
yoda: 2
},
extends: [
"plugin:test/foo",
"plugin:test/bar"
]
});
});
});

describe("even if config files have Unicode BOM,", () => {
Expand Down