Skip to content

Commit

Permalink
Fix: allow baseConfig to extend preloaded plugin config (fixes #15079)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Oct 19, 2021
1 parent 35f3254 commit 31e7939
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
11 changes: 10 additions & 1 deletion lib/cli-engine/cli-engine.js
Expand Up @@ -570,8 +570,10 @@ class CLIEngine {
/**
* Creates a new instance of the core CLI engine.
* @param {CLIEngineOptions} providedOptions The options for this instance.
* @param {Object} [additionalData] Additional settings that are not CLIEngineOptions.
* @param {Record<string,Plugin>|null} [additionalData.preloadedPlugins] Preloaded plugins.
*/
constructor(providedOptions) {
constructor(providedOptions, { preloadedPlugins } = {}) {
const options = Object.assign(
Object.create(null),
defaultOptions,
Expand All @@ -584,6 +586,13 @@ class CLIEngine {
}

const additionalPluginPool = new Map();

if (preloadedPlugins) {
for (const [id, plugin] of Object.entries(preloadedPlugins)) {
additionalPluginPool.set(id, plugin);
}
}

const cacheFilePath = getCacheFile(
options.cacheLocation || options.cacheFile,
options.cwd
Expand Down
17 changes: 2 additions & 15 deletions lib/eslint/eslint.js
Expand Up @@ -54,7 +54,7 @@ const { version } = require("../../package.json");
* @property {string} [ignorePath] The ignore file to use instead of .eslintignore.
* @property {ConfigData} [overrideConfig] Override config object, overrides all configs used with this instance
* @property {string} [overrideConfigFile] The configuration file to use.
* @property {Record<string,Plugin>} [plugins] An array of plugin implementations.
* @property {Record<string,Plugin>|null} [plugins] Preloaded plugins. This is a map-like object, keys are plugin IDs and each value is implementation.
* @property {"error" | "warn" | "off"} [reportUnusedDisableDirectives] the severity to report unused eslint-disable directives.
* @property {string} [resolvePluginsRelativeTo] The folder where plugins should be resolved from, defaulting to the CWD.
* @property {string[]} [rulePaths] An array of directories to load custom rules from.
Expand Down Expand Up @@ -433,26 +433,13 @@ class ESLint {
*/
constructor(options = {}) {
const processedOptions = processOptions(options);
const cliEngine = new CLIEngine(processedOptions);
const cliEngine = new CLIEngine(processedOptions, { preloadedPlugins: options.plugins });
const {
additionalPluginPool,
configArrayFactory,
lastConfigArrays
} = getCLIEngineInternalSlots(cliEngine);
let updated = false;

/*
* Address `plugins` to add plugin implementations.
* Operate the `additionalPluginPool` internal slot directly to avoid
* using `addPlugin(id, plugin)` method that resets cache everytime.
*/
if (options.plugins) {
for (const [id, plugin] of Object.entries(options.plugins)) {
additionalPluginPool.set(id, plugin);
updated = true;
}
}

/*
* Address `overrideConfig` to set override config.
* Operate the `configArrayFactory` internal slot directly because this
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/eslint/eslint.js
Expand Up @@ -2095,6 +2095,36 @@ describe("ESLint", () => {
assert.strictEqual(results[0].messages[0].ruleId, "test/example-rule");
});

it("should return two messages when executing with `baseConfig` that extends preloaded plugin config", async () => {
eslint = new ESLint({
cwd: path.join(fixtureDir, ".."),
useEslintrc: false,
baseConfig: {
extends: ["plugin:test/preset"]
},
plugins: {
test: {
rules: {
"example-rule": require("../../fixtures/rules/custom-rule")
},
configs: {
preset: {
rules: {
"test/example-rule": 1
},
plugins: ["test"]
}
}
}
}
});
const results = await eslint.lintFiles([fs.realpathSync(getFixturePath("rules", "test", "test-custom-rule.js"))]);

assert.strictEqual(results.length, 1);
assert.strictEqual(results[0].messages.length, 2);
assert.strictEqual(results[0].messages[0].ruleId, "test/example-rule");
});

it("should load plugins from the `loadPluginsRelativeTo` directory, if specified", async () => {
eslint = new ESLint({
resolvePluginsRelativeTo: getFixturePath("plugins"),
Expand Down

0 comments on commit 31e7939

Please sign in to comment.