Skip to content

Commit c8e44e4

Browse files
authored
feat(require-type): add auto-fix (#1534)
<!-- πŸ‘‹ 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 #1530 - [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 sets up "simple" `require-` type rules with the ability to provide fixers, and configures `require-type` with a fixer that will add `type: "commonjs"` if missing. Per [Node.js docs](https://nodejs.org/api/packages.html#type), projects without a "type" property default to CommonJS. Since adding "type": "commonjs" simply makes this implicit default explicit without changing runtime behavior, it is a safe, non-breaking autofix.
1 parent 6636ce4 commit c8e44e4

File tree

5 files changed

+187
-90
lines changed

5 files changed

+187
-90
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
230230
| [require-repository](docs/rules/require-repository.md) | Requires the `repository` property to be present. | πŸ“¦ | | | |
231231
| [require-scripts](docs/rules/require-scripts.md) | Requires the `scripts` property to be present. | | | | |
232232
| [require-sideEffects](docs/rules/require-sideEffects.md) | Requires the `sideEffects` property to be present. | πŸ“¦ | | | |
233-
| [require-type](docs/rules/require-type.md) | Requires the `type` property to be present. | βœ”οΈ βœ… πŸ“¦ | | | |
233+
| [require-type](docs/rules/require-type.md) | Requires the `type` property to be present. | βœ”οΈ βœ… πŸ“¦ | πŸ”§ | | |
234234
| [require-types](docs/rules/require-types.md) | Requires the `types` property to be present. | | | | |
235235
| [require-version](docs/rules/require-version.md) | Requires the `version` property to be present. | βœ”οΈ βœ… πŸ“¦ | | | |
236236
| [restrict-dependency-ranges](docs/rules/restrict-dependency-ranges.md) | Restricts the range of dependencies to allow or disallow specific types of ranges. | | | πŸ’‘ | |

β€Ždocs/rules/require-type.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
πŸ’Ό This rule is enabled in the following configs: βœ”οΈ `legacy-recommended`, βœ… `recommended`, πŸ“¦ `recommended-publishable`.
44

5+
πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6+
57
<!-- end auto-generated rule header -->
68

79
This rule checks for the existence of the `"type"` property in a package.json, and reports a violation if it doesn't exist.

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,35 @@ import {
33
createSimpleRequirePropertyRule,
44
} from "../utils/createSimpleRequirePropertyRule.js";
55

6-
const properties: [name: string, options?: CreateRequirePropertyRuleOptions][] =
7-
[
8-
["author"],
9-
["bugs"],
10-
["bundleDependencies"],
11-
["dependencies"],
12-
["description", { isRecommended: true }],
13-
["devDependencies"],
14-
["engines"],
15-
["exports", { category: "Publishable" }],
16-
["files", { category: "Publishable" }],
17-
["homepage"],
18-
["keywords"],
19-
["license", { ignorePrivateDefault: true, isRecommended: true }],
20-
["name", { ignorePrivateDefault: true, isRecommended: true }],
21-
["optionalDependencies"],
22-
["peerDependencies"],
23-
["repository", { category: "Publishable", ignorePrivateDefault: true }],
24-
["scripts"],
25-
["sideEffects", { category: "Publishable" }],
26-
["type", { isRecommended: true }],
27-
["types"],
28-
["version", { ignorePrivateDefault: true, isRecommended: true }],
29-
];
6+
export const propertyConfig: [
7+
name: string,
8+
options?: CreateRequirePropertyRuleOptions,
9+
][] = [
10+
["author"],
11+
["bugs"],
12+
["bundleDependencies"],
13+
["dependencies"],
14+
["description", { isRecommended: true }],
15+
["devDependencies"],
16+
["engines"],
17+
["exports", { category: "Publishable" }],
18+
["files", { category: "Publishable" }],
19+
["homepage"],
20+
["keywords"],
21+
["license", { ignorePrivateDefault: true, isRecommended: true }],
22+
["name", { ignorePrivateDefault: true, isRecommended: true }],
23+
["optionalDependencies"],
24+
["peerDependencies"],
25+
["repository", { category: "Publishable", ignorePrivateDefault: true }],
26+
["scripts"],
27+
["sideEffects", { category: "Publishable" }],
28+
["type", { fixValue: "commonjs", isRecommended: true }],
29+
["types"],
30+
["version", { ignorePrivateDefault: true, isRecommended: true }],
31+
];
3032

3133
export const rules = Object.fromEntries(
32-
properties.map(([propertyName, options]) => {
34+
propertyConfig.map(([propertyName, options]) => {
3335
const { rule, ruleName } = createSimpleRequirePropertyRule(
3436
propertyName,
3537
options,

0 commit comments

Comments
Β (0)