Skip to content

Commit

Permalink
refactor(valid-local-dependency): remove unreachable code (#771)
Browse files Browse the repository at this point in the history
<!-- πŸ‘‹ Hi, thanks for sending a PR to eslint-plugin-package-json! πŸ’–.
Please fill out all fields below and make sure each item is true and [x]
checked.
Otherwise we may not be able to review your PR. -->

## PR Checklist

-   [x] Addresses an existing open issue: fixes #118 
- [x] That issue was marked as [`status: accepting
prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22)
- [x] Steps in
[CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/blob/main/.github/CONTRIBUTING.md)
were taken

## Overview

This change removes an unreachable code block. `require.resolve` will
only return a string, if the file is resolvable, or throw an error if it
isn't. There's never a case in which `require.resolve` would return a
falsy value.

Closes #118
  • Loading branch information
michaelfaith authored Jan 27, 2025
1 parent 71f2e71 commit 2014634
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/rules/valid-local-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,19 @@ export const rule = createRule({
Object.entries(devDependencies ?? {}),
] as [string, string][][];

depObjs.forEach((obj) => {
obj.forEach(([key, value]) => {
for (const obj of depObjs) {
for (const [key, value] of obj) {
const response = (localPath: RegExp | string) => {
const filePath = path.join(
context.filename.replace(/package\.json/g, "/"),
value.replace(new RegExp(localPath, "g"), ""),
"/package.json",
);

// Attempt to resolve the file path, and if it fails
// and throws, then we know it's invalid.
try {
if (!require.resolve(filePath)) {
context.report({
data: {
package: key,
path: value,
},
messageId: "invalidPath",
node: context.sourceCode.ast,
});
}
require.resolve(filePath);
} catch {
context.report({
data: {
Expand All @@ -58,8 +51,8 @@ export const rule = createRule({
if (value.startsWith("file:")) {
response("file:");
}
});
});
}
}
},
};
},
Expand Down

0 comments on commit 2014634

Please sign in to comment.