Skip to content

feat(require-types): add new rule #958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord
| [require-files](docs/rules/require-files.md) | Requires the `files` property to be present. | | | | |
| [require-keywords](docs/rules/require-keywords.md) | Requires the `keywords` property to be present. | | | | |
| [require-name](docs/rules/require-name.md) | Requires the `name` property to be present. | ✅ | | | |
| [require-types](docs/rules/require-types.md) | Requires the `types` property to be present. | | | | |
| [require-version](docs/rules/require-version.md) | Requires the `version` property to be present. | ✅ | | | |
| [sort-collections](docs/rules/sort-collections.md) | Dependencies, scripts, and configuration values must be declared in alphabetical order. | ✅ | 🔧 | | |
| [unique-dependencies](docs/rules/unique-dependencies.md) | Checks a dependency isn't specified more than once (i.e. in `dependencies` and `devDependencies`) | ✅ | | 💡 | |
Expand Down
27 changes: 27 additions & 0 deletions docs/rules/require-types.md
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-Actionable] From https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html:

Note that the "typings" field is synonymous with types, and could be used as well.

Confirmed that "types" is the primary one. I don't adding opt-in support for "typings" is required for the base rule. If someone really wants that they can file an issue explaining why. 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# require-types

<!-- end auto-generated rule header -->

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

Example of **incorrect** code for this rule:

```json
{
"name": "Thee Silver Mt. Zion",
"version": "13.0.0"
}
```

Example of **correct** code for this rule:

```json
{
"name": "Thee Silver Mt. Zion",
"version": "13.0.0",
"types": "./index.d.ts"
}
```

See [TypeScript Handbook > Publishing](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html) for more information on publishing package types.
1 change: 1 addition & 0 deletions src/rules/require-properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const properties = [
["files", false],
["keywords", false],
["name", true],
["types", false],
["version", true],
// TODO: More to come!
] satisfies [string, boolean][];
Expand Down
47 changes: 47 additions & 0 deletions src/tests/rules/require-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { rules } from "../../rules/require-properties.js";
import { ruleTester } from "./ruleTester.js";

ruleTester.run("require-types", rules["require-types"], {
invalid: [
{
code: "{}",
errors: [
{
data: { property: "types" },
line: 1,
messageId: "missing",
},
],
},
{
code: `{
"version": "1.0.0"
}
`,
errors: [
{
data: { property: "types" },
line: 1,
messageId: "missing",
},
],
},
{
code: `{
"author": "Jessica Moss",
"bin": {
"types": "./index.js"
}
}
`,
errors: [
{
data: { property: "types" },
line: 1,
messageId: "missing",
},
],
},
],
valid: [`{ "types": "./index.d.ts" }`],
});