From e109c009528af16efc1e4f61cea46fe79a6f00d9 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 22 Nov 2023 11:22:47 -0500 Subject: [PATCH 1/3] fix: Use original plugin from disk in FlatCompat Fixes #135 --- lib/config-array-factory.js | 2 + lib/config-array/config-dependency.js | 8 ++++ lib/flat-compat.js | 2 +- tests/lib/config-array-factory.js | 4 ++ tests/lib/config-array/config-dependency.js | 4 +- tests/lib/flat-compat.js | 47 +++++++-------------- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/lib/config-array-factory.js b/lib/config-array-factory.js index 99851e15..58c1e808 100644 --- a/lib/config-array-factory.js +++ b/lib/config-array-factory.js @@ -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, @@ -1089,6 +1090,7 @@ class ConfigArrayFactory { return new ConfigDependency({ definition: normalizePlugin(pluginDefinition), + original: pluginDefinition, filePath, id, importerName: ctx.name, diff --git a/lib/config-array/config-dependency.js b/lib/config-array/config-dependency.js index 2883c3a2..6b0e1797 100644 --- a/lib/config-array/config-dependency.js +++ b/lib/config-array/config-dependency.js @@ -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. @@ -36,6 +37,7 @@ class ConfigDependency { */ constructor({ definition = null, + original = null, error = null, filePath = null, id, @@ -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} diff --git a/lib/flat-compat.js b/lib/flat-compat.js index e0d7ab66..6c307a99 100644 --- a/lib/flat-compat.js +++ b/lib/flat-compat.js @@ -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; diff --git a/tests/lib/config-array-factory.js b/tests/lib/config-array-factory.js index 6b991381..a92443f6 100644 --- a/tests/lib/config-array-factory.js +++ b/tests/lib/config-array-factory.js @@ -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", () => { diff --git a/tests/lib/config-array/config-dependency.js b/tests/lib/config-array/config-dependency.js index 3087b8ca..c049b99f 100644 --- a/tests/lib/config-array/config-dependency.js +++ b/tests/lib/config-array/config-dependency.js @@ -68,7 +68,8 @@ describe("ConfigDependency", () => { filePath: "filePath?", id: "id?", importerName: "importerName?", - importerPath: "importerPath?" + importerPath: "importerPath?", + original: null } ); }); @@ -105,6 +106,7 @@ describe("ConfigDependency", () => { // Make expected output; no `definition` property. output = ""; localConsole.log({ + original: null, error, filePath: "filePath?", id: "id?", diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index 2afb38a0..36d9ec4a 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -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 @@ -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({ @@ -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 } }); }); @@ -1087,13 +1083,7 @@ describe("FlatCompat", () => { }); assert.deepStrictEqual(result[1], { plugins: { - fixture2: { - configs: {}, - rules: {}, - environments: {}, - processors: {}, - ...plugin - } + fixture2: plugin } }); }); @@ -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"]}); + const plugin = require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js")); + + assert.strictEqual(result[1].plugins.fixture2, plugin); + }); + }); }); From 9450dcebf6f59b294d547164421fe89cfe47aa1a Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Wed, 22 Nov 2023 11:26:51 -0500 Subject: [PATCH 2/3] Clean up lint errors --- tests/lib/flat-compat.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/tests/lib/flat-compat.js b/tests/lib/flat-compat.js index 36d9ec4a..6bba95a0 100644 --- a/tests/lib/flat-compat.js +++ b/tests/lib/flat-compat.js @@ -23,22 +23,6 @@ const require = createRequire(import.meta.url); const FIXTURES_BASE_PATH = path.resolve(dirname, "../fixtures/flat-compat/"); -/** - * Normalizes a plugin object to have all available keys. This matches what - * ConfigArrayFactory does. - * @param {Object} plugin The plugin object to normalize. - * @returns {Object} The normalized plugin object. - */ -function normalizePlugin(plugin) { - return { - configs: {}, - rules: {}, - environments: {}, - processors: {}, - ...plugin - }; -} - /** * Returns the full directory path for a fixture directory. * @param {string} dirName The directory name to resolve. @@ -1106,7 +1090,7 @@ describe("FlatCompat", () => { }); it("should use the same plugin instance as require()", async () => { - const result = compat.config({ plugins: ["fixture2"]}); + const result = compat.config({ plugins: ["fixture2"] }); const plugin = require(path.join(compat.baseDirectory, "node_modules/eslint-plugin-fixture2.js")); assert.strictEqual(result[1].plugins.fixture2, plugin); From d3d57e45522b9d06d6ced125e9c24e620a918f6e Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Mon, 27 Nov 2023 11:12:19 -0500 Subject: [PATCH 3/3] Hide original field from serialization --- lib/config-array/config-dependency.js | 3 ++- tests/lib/config-array/config-dependency.js | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/config-array/config-dependency.js b/lib/config-array/config-dependency.js index 6b0e1797..080e6405 100644 --- a/lib/config-array/config-dependency.js +++ b/lib/config-array/config-dependency.js @@ -109,7 +109,8 @@ class ConfigDependency { */ [util.inspect.custom]() { const { - definition: _ignore, // eslint-disable-line no-unused-vars + definition: _ignore1, // eslint-disable-line no-unused-vars + original: _ignore2, // eslint-disable-line no-unused-vars ...obj } = this; diff --git a/tests/lib/config-array/config-dependency.js b/tests/lib/config-array/config-dependency.js index c049b99f..3087b8ca 100644 --- a/tests/lib/config-array/config-dependency.js +++ b/tests/lib/config-array/config-dependency.js @@ -68,8 +68,7 @@ describe("ConfigDependency", () => { filePath: "filePath?", id: "id?", importerName: "importerName?", - importerPath: "importerPath?", - original: null + importerPath: "importerPath?" } ); }); @@ -106,7 +105,6 @@ describe("ConfigDependency", () => { // Make expected output; no `definition` property. output = ""; localConsole.log({ - original: null, error, filePath: "filePath?", id: "id?",