Skip to content

Commit bc54025

Browse files
authored
feat(valid-main): add new rule for validating main (#1382)
<!-- πŸ‘‹ 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 #831 - [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-main` rule that uses `validateMain` from package-json-validator to identify errors. It's included in the `recommended` config.
1 parent ecb492d commit bc54025

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
226226
| [valid-keywords](docs/rules/valid-keywords.md) | Enforce that the `keywords` property is valid. | βœ”οΈ βœ… | | | |
227227
| [valid-license](docs/rules/valid-license.md) | Enforce that the `license` property is valid. | βœ”οΈ βœ… | | | |
228228
| [valid-local-dependency](docs/rules/valid-local-dependency.md) | Checks existence of local dependencies in the package.json | | | | ❌ |
229+
| [valid-main](docs/rules/valid-main.md) | Enforce that the `main` property is valid. | βœ”οΈ βœ… | | | |
229230
| [valid-name](docs/rules/valid-name.md) | Enforce that package names are valid npm package names | βœ”οΈ βœ… | | | |
230231
| [valid-optionalDependencies](docs/rules/valid-optionalDependencies.md) | Enforce that the `optionalDependencies` property is valid. | βœ”οΈ βœ… | | | |
231232
| [valid-package-definition](docs/rules/valid-package-definition.md) | Enforce that package.json has all properties required by the npm spec | βœ”οΈ βœ… | | | |

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# valid-main
2+
3+
πŸ’Ό This rule is enabled in the following configs: βœ”οΈ `legacy-recommended`, βœ… `recommended`.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
This rule checks that the `main` property is a non-empty string.
8+
9+
Example of **incorrect** code for this rule:
10+
11+
```json
12+
{
13+
"main": ["index.js", "secondary.js"]
14+
}
15+
```
16+
17+
Example of **correct** code for this rule:
18+
19+
```json
20+
{
21+
"main": "index.js"
22+
}
23+
```

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
validateHomepage,
1313
validateKeywords,
1414
validateLicense,
15+
validateMain,
1516
validateScripts,
1617
validateType,
1718
} from "package-json-validator";
@@ -49,6 +50,7 @@ const properties = [
4950
["homepage", validateHomepage],
5051
["keywords", validateKeywords],
5152
["license", validateLicense],
53+
["main", validateMain],
5254
["optionalDependencies", validateDependencies],
5355
["peerDependencies", validateDependencies],
5456
["scripts", validateScripts],
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { rules } from "../../rules/valid-properties.ts";
2+
import { ruleTester } from "./ruleTester.ts";
3+
4+
ruleTester.run("valid-main", rules["valid-main"], {
5+
invalid: [
6+
{
7+
code: `{
8+
"main": null
9+
}
10+
`,
11+
errors: [
12+
{
13+
data: {
14+
error: "the value is `null`, but should be a `string`",
15+
},
16+
line: 2,
17+
messageId: "validationError",
18+
},
19+
],
20+
},
21+
{
22+
code: `{
23+
"main": 123
24+
}
25+
`,
26+
errors: [
27+
{
28+
data: {
29+
error: "the type should be a `string`, not `number`",
30+
},
31+
line: 2,
32+
messageId: "validationError",
33+
},
34+
],
35+
},
36+
{
37+
code: `{
38+
"main": []
39+
}
40+
`,
41+
errors: [
42+
{
43+
data: {
44+
error: "the type should be a `string`, not `Array`",
45+
},
46+
line: 2,
47+
messageId: "validationError",
48+
},
49+
],
50+
},
51+
{
52+
code: `{
53+
"main": ""
54+
}
55+
`,
56+
errors: [
57+
{
58+
data: {
59+
error: "the value is empty, but should be the path to the package's main module",
60+
},
61+
line: 2,
62+
messageId: "validationError",
63+
},
64+
],
65+
},
66+
],
67+
valid: ["{}", `{ "main": "./index.js" }`, `{ "main": "index.js" }`],
68+
});

0 commit comments

Comments
Β (0)