Skip to content

Commit d2a81d3

Browse files
authored
feat(valid-files): add new rule for validating files (#1380)
<!-- πŸ‘‹ 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 #827 - [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 adds a new `valid-files` rule that uses `validateFiles` from package-json-validator to identify errors. It's included in the `recommended` config.
1 parent f49070b commit d2a81d3

File tree

6 files changed

+147
-6
lines changed

6 files changed

+147
-6
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
221221
| [valid-devDependencies](docs/rules/valid-devDependencies.md) | Enforce that the `devDependencies` property is valid. | βœ”οΈ βœ… | | | |
222222
| [valid-directories](docs/rules/valid-directories.md) | Enforce that the `directories` property is valid. | βœ”οΈ βœ… | | | |
223223
| [valid-exports](docs/rules/valid-exports.md) | Enforce that the `exports` property is valid. | βœ”οΈ βœ… | | | |
224+
| [valid-files](docs/rules/valid-files.md) | Enforce that the `files` property is valid. | βœ”οΈ βœ… | | | |
224225
| [valid-homepage](docs/rules/valid-homepage.md) | Enforce that the `homepage` property is valid. | βœ”οΈ βœ… | | | |
225226
| [valid-license](docs/rules/valid-license.md) | Enforce that the `license` property is valid. | βœ”οΈ βœ… | | | |
226227
| [valid-local-dependency](docs/rules/valid-local-dependency.md) | Checks existence of local dependencies in the package.json | | | | ❌ |

β€Ždocs/rules/valid-files.mdβ€Ž

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# valid-files
2+
3+
πŸ’Ό This rule is enabled in the following configs: βœ”οΈ `legacy-recommended`, βœ… `recommended`.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
This rule does the following checks on the value of the `files` property:
8+
9+
- It must be an array.
10+
- The array should only consist of non-empty strings
11+
12+
Example of **incorrect** code for this rule:
13+
14+
```json
15+
{
16+
"files": "dist/*"
17+
}
18+
```
19+
20+
Example of **correct** code for this rule:
21+
22+
```json
23+
{
24+
"files": ["CHANGELOG.md", "dist/"]
25+
}
26+
```

β€Žpackage.jsonβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"detect-indent": "^7.0.2",
6161
"detect-newline": "^4.0.1",
6262
"eslint-fix-utils": "~0.4.0",
63-
"package-json-validator": "~0.57.0",
63+
"package-json-validator": "~0.58.0",
6464
"semver": "^7.7.3",
6565
"sort-object-keys": "^2.0.0",
6666
"sort-package-json": "^3.4.0",

β€Žpnpm-lock.yamlβ€Ž

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žsrc/rules/valid-properties.tsβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
validateDescription,
99
validateDirectories,
1010
validateExports,
11+
validateFiles,
1112
validateHomepage,
1213
validateLicense,
1314
validateScripts,
@@ -43,6 +44,7 @@ const properties = [
4344
["devDependencies", validateDependencies],
4445
["directories", validateDirectories],
4546
["exports", validateExports],
47+
["files", validateFiles],
4648
["homepage", validateHomepage],
4749
["license", validateLicense],
4850
["optionalDependencies", validateDependencies],
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { rules } from "../../rules/valid-properties.ts";
2+
import { ruleTester } from "./ruleTester.ts";
3+
4+
ruleTester.run("valid-files", rules["valid-files"], {
5+
invalid: [
6+
{
7+
code: `{
8+
"files": null
9+
}
10+
`,
11+
errors: [
12+
{
13+
data: {
14+
error: "the value is `null`, but should be an `Array` of strings",
15+
},
16+
line: 2,
17+
messageId: "validationError",
18+
},
19+
],
20+
},
21+
{
22+
code: `{
23+
"files": 123
24+
}
25+
`,
26+
errors: [
27+
{
28+
data: {
29+
error: "the type should be `Array`, not `number`",
30+
},
31+
line: 2,
32+
messageId: "validationError",
33+
},
34+
],
35+
},
36+
{
37+
code: `{
38+
"files": "invalid"
39+
}
40+
`,
41+
errors: [
42+
{
43+
data: {
44+
error: "the type should be `Array`, not `string`",
45+
},
46+
line: 2,
47+
messageId: "validationError",
48+
},
49+
],
50+
},
51+
{
52+
code: `{
53+
"files": {
54+
"invalid-bin": 123
55+
}
56+
}
57+
`,
58+
errors: [
59+
{
60+
data: {
61+
error: "the type should be `Array`, not `object`",
62+
},
63+
line: 2,
64+
messageId: "validationError",
65+
},
66+
],
67+
},
68+
{
69+
code: `{
70+
"files": [
71+
"valid",
72+
"",
73+
123,
74+
null,
75+
{}
76+
]
77+
}
78+
`,
79+
errors: [
80+
{
81+
data: {
82+
error: "item at index 1 is empty, but should be a file pattern",
83+
},
84+
line: 4,
85+
messageId: "validationError",
86+
},
87+
{
88+
data: {
89+
error: "item at index 2 should be a string, not `number`",
90+
},
91+
line: 5,
92+
messageId: "validationError",
93+
},
94+
{
95+
data: {
96+
error: "item at index 3 should be a string, not `null`",
97+
},
98+
line: 6,
99+
messageId: "validationError",
100+
},
101+
{
102+
data: {
103+
error: "item at index 4 should be a string, not `object`",
104+
},
105+
line: 7,
106+
messageId: "validationError",
107+
},
108+
],
109+
},
110+
],
111+
valid: ["{}", `{ "files": [] }`, `{ "files": ["CHANGELOG.md", "dist/"] }`],
112+
});

0 commit comments

Comments
Β (0)