diff --git a/.gitignore b/.gitignore index 1717d22a..5423bd14 100644 --- a/.gitignore +++ b/.gitignore @@ -107,3 +107,6 @@ dist yarn.lock package-lock.json pnpm-lock.yaml + +# used in tests +/tmp/ diff --git a/package.json b/package.json index 892991a7..58b8f4c7 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "eslint-release": "^3.2.0", "fs-teardown": "^0.1.3", "mocha": "^9.0.3", - "rollup": "^2.54.0", + "rollup": "^2.70.1", "shelljs": "^0.8.4", "sinon": "^11.1.2", "temp-dir": "^2.0.0" diff --git a/rollup.config.js b/rollup.config.js index 3ada0c1c..c8f1a330 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,3 +1,22 @@ +/** + * Custom Rollup plugin for `import.meta.url` transformation to commonjs. + * The default transformation ('file:' + __filename) does not check characters in __filename, + * and thus can produce invalid URLs, like in https://github.com/eslint/eslint/issues/15766 + * See https://github.com/eslint/eslint/issues/15766#issuecomment-1093941321 + * @returns {Object} Rollup plugin object. + */ +function importMetaURLPlugin() { + return { + name: "custom-import-meta-url", + resolveImportMeta(property) { + if (property === "url") { + return "require('url').pathToFileURL(__filename).toString()"; + } + return null; + } + }; +} + export default [ { input: "./lib/index.js", @@ -12,7 +31,8 @@ export default [ file: "dist/eslintrc.cjs", sourcemap: true, freeze: false - } + }, + plugins: [importMetaURLPlugin()] }, { input: "./lib/index-universal.js", diff --git a/tests/lib/commonjs.cjs b/tests/lib/commonjs.cjs index 830d2ebc..a6840cde 100644 --- a/tests/lib/commonjs.cjs +++ b/tests/lib/commonjs.cjs @@ -12,7 +12,8 @@ const assert = require("assert"); const eslintrc = require("../../dist/eslintrc.cjs"); const universal = require("../../dist/eslintrc-universal.cjs"); - +const path = require("path"); +const sh = require("shelljs"); //------------------------------------------------------------------------------ // Tests @@ -73,3 +74,21 @@ describe("eslintrc CommonJS Universal", () => { }); }); }); + +// https://github.com/eslint/eslint/issues/15766 +describe("eslintrc CommonJS loading", () => { + const testDir = path.resolve(__dirname, "../../tmp/my%2Fproject"); + + before(() => { + sh.mkdir("-p", testDir); + sh.cp("", "./dist/eslintrc.cjs", testDir); + }); + + after(() => { + sh.rm("-r", testDir); + }); + + it("eslintrc.cjs module successfully loads when it is in a path that contains URL-encoded characters", () => { + require(path.resolve(testDir, "eslintrc.cjs")); + }); +});