Skip to content

Commit

Permalink
fix(no-redundant-files): detect variations of README.md as redundant (#…
Browse files Browse the repository at this point in the history
…765)

<!-- πŸ‘‹ 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 #763 
- [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 detects additional variations of `README` and `LICENSE`
files. Apparently, npm will include any `README` followed by any
extension (e.g. `README.fi.md`)

Closes #763
  • Loading branch information
michaelfaith authored Jan 24, 2025
1 parent b3165c1 commit 7453095
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/rules/no-redundant-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@ import { isJSONStringLiteral, isNotNullish } from "../utils/predicates.js";

const defaultFiles = [
/* cspell:disable-next-line */
"LICENCE",
/* cspell:disable-next-line */
"LICENCE.md",
"LICENSE",
"LICENSE.md",
"package.json",
"README.md",
] as const;
/^(\.\/)?LICEN(C|S)E(\.|$)/i,
/^(\.\/)?README(\.|$)/i,
/^(\.\/)?package\.json$/i,
];

const cachedRegex = new Map<string, RegExp>();
const getCachedLocalFileRegex = (filename: string) => {
Expand Down Expand Up @@ -116,11 +112,8 @@ export const rule = createRule({

// We can also go ahead and check if this matches one
// of the static default files
const regex = getCachedLocalFileRegex(
element.value,
);
for (const defaultFile of defaultFiles) {
if (regex.test(defaultFile)) {
if (defaultFile.test(element.value)) {
report(
elements,
index,
Expand Down
45 changes: 45 additions & 0 deletions src/tests/rules/no-redundant-files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,51 @@ ruleTester.run("no-redundant-files", rule, {
{
code: `{
\t"files": [
\t\t"README.a-b-c.md",
\t\t"./package.json"
]
}
`,
errors: [
{
data: { file: "README.a-b-c.md" },
line: 3,
messageId: "unnecessaryDefault",
suggestions: [
{
messageId: "remove",
output: `{
\t"files": [
\t\t
\t\t"./package.json"
]
}
`,
},
],
},
{
data: { file: "./package.json" },
line: 4,
messageId: "unnecessaryDefault",
suggestions: [
{
messageId: "remove",
output: `{
\t"files": [
\t\t"README.a-b-c.md"
\t\t
]
}
`,
},
],
},
],
},
{
code: `{
\t"files": [
\t\t"CHANGELOG.md",
\t\t"dist",
\t\t"CHANGELOG.md"
Expand Down

0 comments on commit 7453095

Please sign in to comment.