Skip to content

Commit

Permalink
fix: Use original plugin from disk in FlatCompat
Browse files Browse the repository at this point in the history
Fixes #135
  • Loading branch information
nzakas committed Nov 22, 2023
1 parent af564ff commit e109c00
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
2 changes: 2 additions & 0 deletions lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ class ConfigArrayFactory {
if (plugin) {
return new ConfigDependency({
definition: normalizePlugin(plugin),
original: plugin,
filePath: "", // It's unknown where the plugin came from.
id,
importerName: ctx.name,
Expand Down Expand Up @@ -1089,6 +1090,7 @@ class ConfigArrayFactory {

return new ConfigDependency({
definition: normalizePlugin(pluginDefinition),
original: pluginDefinition,
filePath,
id,
importerName: ctx.name,
Expand Down
8 changes: 8 additions & 0 deletions lib/config-array/config-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ConfigDependency {
* Initialize this instance.
* @param {Object} data The dependency data.
* @param {T} [data.definition] The dependency if the loading succeeded.
* @param {T} [data.original] The original, non-normalized dependency if the loading succeeded.
* @param {Error} [data.error] The error object if the loading failed.
* @param {string} [data.filePath] The actual path to the dependency if the loading succeeded.
* @param {string} data.id The ID of this dependency.
Expand All @@ -36,6 +37,7 @@ class ConfigDependency {
*/
constructor({
definition = null,
original = null,
error = null,
filePath = null,
id,
Expand All @@ -49,6 +51,12 @@ class ConfigDependency {
*/
this.definition = definition;

/**
* The original dependency as loaded directly from disk if the loading succeeded.
* @type {T|null}
*/
this.original = original;

/**
* The error object if the loading failed.
* @type {Error|null}
Expand Down
2 changes: 1 addition & 1 deletion lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function translateESLintRC(eslintrcConfig, {
debug(`Translating plugin: ${pluginName}`);
debug(`Resolving plugin '${pluginName} relative to ${resolvePluginsRelativeTo}`);

const { definition: plugin, error } = eslintrcConfig.plugins[pluginName];
const { original: plugin, error } = eslintrcConfig.plugins[pluginName];

if (error) {
throw error;
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/config-array-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ describe("ConfigArrayFactory", () => {
it("should have the path to the package at 'plugins[id].filePath' property.", () => {
assert.strictEqual(element.plugins.xxx.filePath, path.join(getPath(), "node_modules/custom-eslint-plugin-xxx/index.js"));
});

it("should have the original definition equal to the origina plugin object", () => {
assert.strictEqual(element.plugins.xxx.original, require(path.join(getPath(), "node_modules/custom-eslint-plugin-xxx/index.js")));
});
});

describe("if 'extends' property was 'foo', the returned value", () => {
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/config-array/config-dependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ describe("ConfigDependency", () => {
filePath: "filePath?",
id: "id?",
importerName: "importerName?",
importerPath: "importerPath?"
importerPath: "importerPath?",
original: null
}
);
});
Expand Down Expand Up @@ -105,6 +106,7 @@ describe("ConfigDependency", () => {
// Make expected output; no `definition` property.
output = "";
localConsole.log({
original: null,
error,
filePath: "filePath?",
id: "id?",
Expand Down
47 changes: 16 additions & 31 deletions tests/lib/flat-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { fileURLToPath, pathToFileURL } from "url";
import { assert } from "chai";
import { FlatCompat } from "../../lib/index.js";
import environments from "../../conf/environments.js";
import { createRequire } from "module";

const dirname = path.dirname(fileURLToPath(import.meta.url));
const require = createRequire(import.meta.url);

//-----------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -56,9 +58,9 @@ describe("FlatCompat", () => {

let compat;
const baseDirectory = getFixturePath("config");
const pluginFixture1 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default);
const pluginFixture2 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2.js")))).default);
const pluginFixture3 = normalizePlugin((await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3.js")))).default);
const pluginFixture1 = (await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default;
const pluginFixture2 = (await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture2.js")))).default;
const pluginFixture3 = (await import(pathToFileURL(path.join(baseDirectory, "node_modules/eslint-plugin-fixture3.js")))).default;

beforeEach(() => {
compat = new FlatCompat({
Expand Down Expand Up @@ -1059,13 +1061,7 @@ describe("FlatCompat", () => {
assert.strictEqual(result.length, 1);
assert.deepStrictEqual(result[0], {
plugins: {
fixture1: {
configs: {},
rules: {},
environments: {},
processors: {},
...(await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default
}
fixture1: (await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default
}
});
});
Expand All @@ -1087,13 +1083,7 @@ describe("FlatCompat", () => {
});
assert.deepStrictEqual(result[1], {
plugins: {
fixture2: {
configs: {},
rules: {},
environments: {},
processors: {},
...plugin
}
fixture2: plugin
}
});
});
Expand All @@ -1109,24 +1099,19 @@ describe("FlatCompat", () => {
});
assert.deepStrictEqual(result[1], {
plugins: {
fixture1: {
configs: {},
rules: {},
environments: {},
processors: {},
...(await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default
},
fixture2: {
configs: {},
rules: {},
environments: {},
processors: {},
...plugin
}
fixture1: (await import(pathToFileURL(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture1.js")))).default,
fixture2: plugin
}
});
});

it("should use the same plugin instance as require()", async () => {
const result = compat.config({ plugins: ["fixture2"]});

Check failure on line 1109 in tests/lib/flat-compat.js

View workflow job for this annotation

GitHub Actions / Verify Files

A space is required before '}'
const plugin = require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js"));

assert.strictEqual(result[1].plugins.fixture2, plugin);
});

});

});

0 comments on commit e109c00

Please sign in to comment.