Skip to content

fix: Resolve files relative to context.cwd #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/hashbang.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module.exports = {
return {}
}

const packageJson = getPackageJson(filePath)
const packageJson = getPackageJson(context, filePath)
if (typeof packageJson?.filePath !== "string") {
return {}
}
Expand All @@ -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
)
Expand All @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-hide-core-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|undefined>} */
const deps = new Set([
...Object.keys(packageJson?.dependencies ?? {}),
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unpublished-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand All @@ -63,7 +63,7 @@ module.exports = {
}

// Check ignored or not
const npmignore = getNpmignore(filePath)
const npmignore = getNpmignore(context, filePath)
if (!npmignore.match(relativePath)) {
return
}
Expand Down
2 changes: 1 addition & 1 deletion lib/util/check-extraneous.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions lib/util/check-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 ?? {})
)
Expand Down
2 changes: 1 addition & 1 deletion lib/util/get-configured-node-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/util/get-npmignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 19 additions & 3 deletions lib/util/get-package-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,17 +38,33 @@ function readPackageJson(dir) {

return null
}
/**
* @param {Pick<import('eslint').Rule.RuleContext, 'cwd'> | Pick<import('eslint').Rule.RuleContext, 'getCwd'>} 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<import('eslint').Rule.RuleContext, 'cwd'> | Pick<import('eslint').Rule.RuleContext, 'getCwd'>} 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
Expand Down