From d3a9358ab72fd6ee810551a1cd970ff7a91d2c9f Mon Sep 17 00:00:00 2001 From: Sebastian Good <2230835+scagood@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:07:01 +0100 Subject: [PATCH] fix: Resolve files relative to context.cwd --- lib/configs/recommended.js | 3 +-- lib/rules/hashbang.js | 7 +++++-- lib/rules/no-hide-core-modules.js | 2 +- lib/rules/no-unpublished-bin.js | 4 ++-- lib/util/check-extraneous.js | 2 +- lib/util/check-publish.js | 4 ++-- lib/util/get-configured-node-version.js | 2 +- lib/util/get-npmignore.js | 5 +++-- lib/util/get-package-json.js | 22 +++++++++++++++++++--- 9 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/configs/recommended.js b/lib/configs/recommended.js index 073c9102..194c5e73 100644 --- a/lib/configs/recommended.js +++ b/lib/configs/recommended.js @@ -4,11 +4,10 @@ const { getPackageJson } = require("../util/get-package-json") const moduleConfig = require("./recommended-module") const scriptConfig = require("./recommended-script") -const packageJson = getPackageJson() +const packageJson = getPackageJson({ cwd: process.cwd() }) const isModule = packageJson != null && - typeof packageJson === "object" && "type" in packageJson && packageJson.type === "module" const recommendedConfig = isModule ? moduleConfig : scriptConfig diff --git a/lib/rules/hashbang.js b/lib/rules/hashbang.js index 3b9a0fa1..cf5c96c7 100644 --- a/lib/rules/hashbang.js +++ b/lib/rules/hashbang.js @@ -125,7 +125,7 @@ module.exports = { return {} } - const packageJson = getPackageJson(filePath) + const packageJson = getPackageJson(context, filePath) if (typeof packageJson?.filePath !== "string") { return {} } @@ -139,7 +139,10 @@ module.exports = { const convertedRelativePath = getConvertPath(context)(originalRelativePath) + + const cwd = context.cwd ?? context.getCwd() ?? process.cwd() const convertedAbsolutePath = path.resolve( + cwd, packageDirectory, convertedRelativePath ) @@ -156,7 +159,7 @@ module.exports = { isExecutable.ignored === false) && context.options?.[0]?.ignoreUnpublished === true ) { - const npmignore = getNpmignore(convertedAbsolutePath) + const npmignore = getNpmignore(context, convertedAbsolutePath) if (npmignore.match(convertedRelativePath)) { return {} diff --git a/lib/rules/no-hide-core-modules.js b/lib/rules/no-hide-core-modules.js index 01e05b28..39574108 100644 --- a/lib/rules/no-hide-core-modules.js +++ b/lib/rules/no-hide-core-modules.js @@ -90,7 +90,7 @@ module.exports = { } const filePath = path.resolve(filename) const dirPath = path.dirname(filePath) - const packageJson = getPackageJson(filePath) + const packageJson = getPackageJson(context, filePath) /** @type {Set} */ const deps = new Set([ ...Object.keys(packageJson?.dependencies ?? {}), diff --git a/lib/rules/no-unpublished-bin.js b/lib/rules/no-unpublished-bin.js index f679758a..c8376fe6 100644 --- a/lib/rules/no-unpublished-bin.js +++ b/lib/rules/no-unpublished-bin.js @@ -45,7 +45,7 @@ module.exports = { rawFilePath = path.resolve(rawFilePath) // Find package.json - const packageJson = getPackageJson(rawFilePath) + const packageJson = getPackageJson(context, rawFilePath) if (typeof packageJson?.filePath !== "string") { return {} } @@ -63,7 +63,7 @@ module.exports = { } // Check ignored or not - const npmignore = getNpmignore(filePath) + const npmignore = getNpmignore(context, filePath) if (!npmignore.match(relativePath)) { return } diff --git a/lib/util/check-extraneous.js b/lib/util/check-extraneous.js index a41f11f1..b4e23f3d 100644 --- a/lib/util/check-extraneous.js +++ b/lib/util/check-extraneous.js @@ -18,7 +18,7 @@ const { getPackageJson } = require("./get-package-json") * @returns {void} */ exports.checkExtraneous = function checkExtraneous(context, filePath, targets) { - const packageInfo = getPackageJson(filePath) + const packageInfo = getPackageJson(context, filePath) if (!packageInfo) { return } diff --git a/lib/util/check-publish.js b/lib/util/check-publish.js index ff27486e..187acce0 100644 --- a/lib/util/check-publish.js +++ b/lib/util/check-publish.js @@ -27,7 +27,7 @@ exports.checkPublish = function checkPublish( targets, options ) { - const packageJson = getPackageJson(filePath) + const packageJson = getPackageJson(context, filePath) if (typeof packageJson?.filePath !== "string") { return } @@ -49,7 +49,7 @@ exports.checkPublish = function checkPublish( const retv = path.relative(basedir, fullPath).replace(/\\/gu, "/") return convertPath(retv) } - const npmignore = getNpmignore(filePath) + const npmignore = getNpmignore(context, filePath) const devDependencies = new Set( Object.keys(packageJson.devDependencies ?? {}) ) diff --git a/lib/util/get-configured-node-version.js b/lib/util/get-configured-node-version.js index 3a66b0af..c0ef85f4 100644 --- a/lib/util/get-configured-node-version.js +++ b/lib/util/get-configured-node-version.js @@ -28,7 +28,7 @@ function getVersionRange(option) { */ function getEnginesNode(context) { const filename = context.filename ?? context.getFilename() - const info = getPackageJson(filename) + const info = getPackageJson(context, filename) const engines = /** @type {Engines | undefined} */ (info?.engines) if (typeof engines?.node === "string") { return getSemverRange(engines?.node) diff --git a/lib/util/get-npmignore.js b/lib/util/get-npmignore.js index e9165084..169b81f0 100644 --- a/lib/util/get-npmignore.js +++ b/lib/util/get-npmignore.js @@ -151,16 +151,17 @@ function parseNpmignore(basedir, filesFieldExists) { * - `files` field of `package.json` * - `.npmignore` * + * @param {import('eslint').Rule.RuleContext} context - The eslint context for the file * @param {string} startPath - A file path to lookup. * @returns {{ match: (filePath: string) => boolean }} * An object to check whther or not a given path should be ignored. * The object has a method `match`. * `match` returns `true` if a given file path should be ignored. */ -module.exports = function getNpmignore(startPath) { +module.exports = function getNpmignore(context, startPath) { const retv = { match: isAncestorFiles } - const packageJson = getPackageJson(startPath) + const packageJson = getPackageJson(context, startPath) if (typeof packageJson?.filePath !== "string") { return retv } diff --git a/lib/util/get-package-json.js b/lib/util/get-package-json.js index 23433754..804e6405 100644 --- a/lib/util/get-package-json.js +++ b/lib/util/get-package-json.js @@ -19,7 +19,7 @@ const cache = new Cache() * @returns {import('type-fest').JsonObject|null} The read `package.json` data, or null. */ function readPackageJson(dir) { - const filePath = path.join(dir, "package.json") + const filePath = path.resolve(dir, "package.json") try { const text = fs.readFileSync(filePath, "utf8") const data = JSON.parse(text) @@ -38,17 +38,33 @@ function readPackageJson(dir) { return null } +/** + * @param {Pick | Pick} context - The eslint context for the file + * @returns {string} + */ +function getCwd(context) { + if ("cwd" in context && typeof context.cwd === "string") { + return context.cwd + } + + if ("getCwd" in context) { + return context.getCwd() + } + + return process.cwd() +} /** * Gets a `package.json` data. * The data is cached if found, then it's used after. * + * @param {Pick | Pick} context - The eslint context for the file * @param {string} [startPath] - A file path to lookup. * @returns {import('type-fest').JsonObject|null} A found `package.json` data or `null`. * This object have additional property `filePath`. */ -function getPackageJson(startPath = "a.js") { - const startDir = path.dirname(path.resolve(startPath)) +function getPackageJson(context, startPath = "a.js") { + const startDir = path.dirname(path.resolve(getCwd(context), startPath)) let dir = startDir let prevDir = "" let data = null