An open API service indexing awesome lists of open source software.

https://github.com/JoshuaKGoldberg/eslint-plugin-package-json

Rules for consistent, readable, and valid package.json files. πŸ—‚οΈ
https://github.com/JoshuaKGoldberg/eslint-plugin-package-json

eslint eslint-plugin json

Last synced: 9 months ago
JSON representation

Rules for consistent, readable, and valid package.json files. πŸ—‚οΈ

Awesome Lists containing this project

README

          

eslint-plugin-package-json


Rules for consistent, readable, and valid package.json files.
πŸ—‚οΈ




πŸ‘ͺ All Contributors: 29


🀝 Code of Conduct: Kept
πŸ§ͺ Coverage
πŸ“ License: MIT
πŸ“¦ npm version
πŸ’ͺ TypeScript: Strict

## Installation

This package requires [ESLint](http://eslint.org) >=8:

```shell
npm install eslint eslint-plugin-package-json --save-dev
```

## Usage

### Flat Config

This plugin's recommended configuration enables its rules on `**/package.json` files, parsing them with [`jsonc-eslint-parser`](https://github.com/ota-meshi/jsonc-eslint-parser).

In your ESLint configuration file:

```ts
import packageJson from "eslint-plugin-package-json";

export default [
// your other ESLint configurations
packageJson.configs.recommended,
];
```

If you want to override the recommended rules:

```ts
import packageJson from "eslint-plugin-package-json";

export default [
// your other ESLint configurations
packageJson.configs.recommended,
{
rules: {
"package-json/valid-package-definition": "off",
},
},
];
```

See [ESLint's _Configuration Files_ guide](https://eslint.org/docs/latest/use/configure/configuration-files-new) for details on how to customize your rules and other config settings.

### Legacy Config

Usage with ESLint's legacy ("eslintrc") format requires also installing [`jsonc-eslint-parser`](https://github.com/ota-meshi/jsonc-eslint-parser):

```shell
npm install jsonc-eslint-parser --save-dev
```

Add an override to your ESLint configuration file that specifies `jsonc-eslint-parser`, this plugin, and its recommended rules for your `package.json` file:

```ts
module.exports = {
overrides: [
{
extends: ["plugin:package-json/legacy-recommended"],
files: ["package.json"],
parser: "jsonc-eslint-parser",
},
],
};
```

You may also want to individually configure rules.
See [ESLint's _Configure Rules_ guide](https://eslint.org/docs/latest/use/configure/rules) for details on how to customize your rules.

```ts
module.exports = {
overrides: [
{
extends: ["plugin:package-json/legacy-recommended"],
files: ["package.json"],
parser: "jsonc-eslint-parser",
rules: {
"package-json/valid-package-definition": "error",
},
},
],
};
```

### Settings

Some rules can be configured in ESLint shared settings.
You can set them in `settings.packageJson` in an ESLint flat config.

Example:

```ts
// eslint.config.ts
import packageJson from "eslint-plugin-package-json";

export default {
plugins: {
"package-json": packageJson,
},
rules: {
// `description` won't be required in package.json with `"private": true`
"package-json/require-description": "error",
},
settings: {
packageJson: {
enforceForPrivate: false,
},
},
};
```

#### `enforceForPrivate`

- **Type:** `boolean`
- **Default:** [see below]

When a package.json file has a `"private": true` field, it indicates that the package will not be published to npm (or another online registry).
Some fields that are nice to have in public packages become less relevant when a package is private.
This option determines whether `require-*` rules, if used, should enforce the presence of the corresponding property in package.json files that have `"private": true`.

By default, this is:

- `false` for [`require-name`](docs/rules/require-name.md) and [`require-version`](docs/rules/require-version.md).
- `true` for every other `require-*` rule.

By specifying this setting as `true` or `false`, it will override the defaults and apply the setting for ALL rules.
In that case, either all `require-*` rules will be applied to private packages or no `require-*` rules will be applied to private packages.
Even then, you can override the setting again at the rule level, by using the rule's `ignorePrivate` option, which will take precedence over this global setting.

### Usage Alongside Prettier

**[`prettier-plugin-packagejson`](https://github.com/matzkoh/prettier-plugin-packagejson)** is a [Prettier plugin](https://prettier.io/docs/en/plugins) that enforces the same `package.json` keys ordering as the [`order-properties`](docs/rules/order-properties.md) and [sort-collections](docs/rules/sort-collections.md) rules with default options.
We recommend using both the Prettier plugin and `eslint-plugin-package-json`'s recommended configuration.
The default settings don't conflict, and Prettier plugins can quickly fix up ordering in your editor on save and/or as a Git hook.

## Supported Rules

πŸ’Ό Configurations enabled in.\
βœ”οΈ Set in the `legacy-recommended` configuration.\
βœ… Set in the `recommended` configuration.\
πŸ”§ Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
πŸ’‘ Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\
❌ Deprecated.

| NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  | Description | πŸ’Ό | πŸ”§ | πŸ’‘ | ❌ |
| :------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------------------- | :--- | :- | :- | :- |
| [no-empty-fields](docs/rules/no-empty-fields.md) | Reports on unnecessary empty arrays and objects. | βœ”οΈ βœ… | | πŸ’‘ | |
| [no-redundant-files](docs/rules/no-redundant-files.md) | Prevents adding unnecessary / redundant files. | βœ”οΈ βœ… | | πŸ’‘ | |
| [order-properties](docs/rules/order-properties.md) | Package properties must be declared in standard order | βœ”οΈ βœ… | πŸ”§ | | |
| [repository-shorthand](docs/rules/repository-shorthand.md) | Enforce either object or shorthand declaration for repository. | βœ”οΈ βœ… | πŸ”§ | | |
| [require-author](docs/rules/require-author.md) | Requires the `author` property to be present. | | | | |
| [require-bugs](docs/rules/require-bugs.md) | Requires the `bugs` property to be present. | | | | |
| [require-bundleDependencies](docs/rules/require-bundleDependencies.md) | Requires the `bundleDependencies` property to be present. | | | | |
| [require-dependencies](docs/rules/require-dependencies.md) | Requires the `dependencies` property to be present. | | | | |
| [require-description](docs/rules/require-description.md) | Requires the `description` property to be present. | βœ”οΈ βœ… | | | |
| [require-devDependencies](docs/rules/require-devDependencies.md) | Requires the `devDependencies` property to be present. | | | | |
| [require-engines](docs/rules/require-engines.md) | Requires the `engines` property to be present. | | | | |
| [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-optionalDependencies](docs/rules/require-optionalDependencies.md) | Requires the `optionalDependencies` property to be present. | | | | |
| [require-peerDependencies](docs/rules/require-peerDependencies.md) | Requires the `peerDependencies` property to be present. | | | | |
| [require-type](docs/rules/require-type.md) | Requires the `type` 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. | βœ”οΈ βœ… | | | |
| [restrict-dependency-ranges](docs/rules/restrict-dependency-ranges.md) | Restricts the range of dependencies to allow or disallow specific types of ranges. | | | πŸ’‘ | |
| [sort-collections](docs/rules/sort-collections.md) | Selected collections must be in a consistent order (lexicographical for most; lifecycle-aware for scripts). | βœ”οΈ βœ… | πŸ”§ | | |
| [unique-dependencies](docs/rules/unique-dependencies.md) | Checks a dependency isn't specified more than once (i.e. in `dependencies` and `devDependencies`) | βœ”οΈ βœ… | | πŸ’‘ | |
| [valid-author](docs/rules/valid-author.md) | Enforce that the `author` property is valid. | βœ”οΈ βœ… | | | |
| [valid-bin](docs/rules/valid-bin.md) | Enforce that the `bin` property is valid. | βœ”οΈ βœ… | | πŸ’‘ | |
| [valid-bundleDependencies](docs/rules/valid-bundleDependencies.md) | Enforce that the `bundleDependencies` (also: `bundledDependencies`) property is valid. | βœ”οΈ βœ… | | | |
| [valid-config](docs/rules/valid-config.md) | Enforce that the `config` property is valid. | βœ”οΈ βœ… | | | |
| [valid-cpu](docs/rules/valid-cpu.md) | Enforce that the `cpu` property is valid. | βœ”οΈ βœ… | | | |
| [valid-dependencies](docs/rules/valid-dependencies.md) | Enforce that the `dependencies` property is valid. | βœ”οΈ βœ… | | | |
| [valid-description](docs/rules/valid-description.md) | Enforce that the `description` property is valid. | βœ”οΈ βœ… | | | |
| [valid-devDependencies](docs/rules/valid-devDependencies.md) | Enforce that the `devDependencies` property is valid. | βœ”οΈ βœ… | | | |
| [valid-directories](docs/rules/valid-directories.md) | Enforce that the `directories` property is valid. | βœ”οΈ βœ… | | | |
| [valid-exports](docs/rules/valid-exports.md) | Enforce that the `exports` property is valid. | βœ”οΈ βœ… | | | |
| [valid-license](docs/rules/valid-license.md) | Enforce that the `license` property is valid. | βœ”οΈ βœ… | | | |
| [valid-local-dependency](docs/rules/valid-local-dependency.md) | Checks existence of local dependencies in the package.json | | | | ❌ |
| [valid-name](docs/rules/valid-name.md) | Enforce that package names are valid npm package names | βœ”οΈ βœ… | | | |
| [valid-optionalDependencies](docs/rules/valid-optionalDependencies.md) | Enforce that the `optionalDependencies` property is valid. | βœ”οΈ βœ… | | | |
| [valid-package-definition](docs/rules/valid-package-definition.md) | Enforce that package.json has all properties required by the npm spec | βœ”οΈ βœ… | | | |
| [valid-peerDependencies](docs/rules/valid-peerDependencies.md) | Enforce that the `peerDependencies` property is valid. | βœ”οΈ βœ… | | | |
| [valid-repository-directory](docs/rules/valid-repository-directory.md) | Enforce that if repository directory is specified, it matches the path to the package.json file | βœ”οΈ βœ… | | πŸ’‘ | |
| [valid-scripts](docs/rules/valid-scripts.md) | Enforce that the `scripts` property is valid. | βœ”οΈ βœ… | | | |
| [valid-type](docs/rules/valid-type.md) | Enforce that the `type` property is valid. | βœ”οΈ βœ… | | | |
| [valid-version](docs/rules/valid-version.md) | Enforce that package versions are valid semver specifiers | βœ”οΈ βœ… | | | |

These rules only run on `package.json` files; they will ignore all other files being linted.
They can lint `package.json` files at project root and in any subfolder of the project, making this plugin great for monorepos.

## Deprecation Policy

We never _want_ to remove things, when we're building them!
But the reality is that libraries evolve and deprecations are a fact of life.
Following are the different timeframes that we've defined as it relates to deprecating APIs in this project.

### RFC Timeframe (6 weeks)

When some aspect of our API is going to be deprecated (and eventually removed), it must initially go through an RFC phase.
Whoever's motivating the removal of the api, should create an RFC issue explaining the proposal and inviting feedback from the community.
That RFC should remain active for at least 6 weeks.
The RFC text should make clear what the target date is for closing the RFC.
Once the RFC period is over, if the removal is still moving forward, the API(s) should be officially deprecated.

### Removal Timeframe (6 months)

Once an API has been marked as deprecated, it will remain intact for at least 6 months.
After 6 months from the date of deprecation, the API is subject to removal.

## Development

See [`.github/CONTRIBUTING.md`](./.github/CONTRIBUTING.md), then [`.github/DEVELOPMENT.md`](./.github/DEVELOPMENT.md).
Thanks! πŸ—‚

## Contributors



Alan
Alan

πŸ› πŸ’»
Andreas Lindberg
Andreas Lindberg

πŸ›
Andrew Kazakov
Andrew Kazakov

πŸ› πŸ’» πŸ€”
Anton Khitrenovich
Anton Khitrenovich

πŸ€”
Azat S.
Azat S.

πŸ€” πŸ’»
Brad Jorsch
Brad Jorsch

πŸ€” πŸ› πŸ’»
Curtis Jewell
Curtis Jewell

πŸ€”


David LJ
David LJ

πŸ“–
Eli
Eli

πŸ€” πŸ›
Heggria
Heggria

πŸ€”
James
James

πŸ’» πŸ€” πŸ› πŸ“–
James Zetlen
James Zetlen

πŸ’» πŸ› πŸ“– πŸš‡ 🚧 πŸ”§
JesΓΊs LeganΓ©s-Combarro
JesΓΊs LeganΓ©s-Combarro

πŸ’»
Josh Goldberg ✨
Josh Goldberg ✨

πŸ”§ πŸ› πŸ’» πŸš‡ πŸ“– 🚧 πŸ€” πŸ–‹ πŸ“†


Kendall Gassner
Kendall Gassner

πŸ’» 🚧
Kristjan ESPERANTO
Kristjan ESPERANTO

πŸ€” πŸ› πŸ’»
Mathias Schreck
Mathias Schreck

πŸ€”
Michael
Michael "Mike" Ferris

πŸ’»
Nick Schonning
Nick Schonning

πŸ’»
Pavel
Pavel

πŸ€” πŸ”§ πŸ“– πŸ’» πŸ›
Sasial
Sasial

πŸ’»


Stephen
Stephen

πŸ’»
Stephen Zhou
Stephen Zhou

πŸ› πŸ’» πŸ€” πŸ“–
Yosuke Ota
Yosuke Ota

πŸ› πŸ’»
b3rnhard
b3rnhard

πŸ›
chouchouji
chouchouji

πŸ’»
michael faith
michael faith

πŸš‡ πŸ’» 🚧 πŸ€” πŸ› πŸ”§ πŸ“–
roottool
roottool

πŸ’»


sunnytsang1998
sunnytsang1998

πŸ›

## Appreciation

Many thanks to [@zetlen](https://github.com/zetlen) for creating the initial version and core infrastructure of this package! πŸ’–

> πŸ’ This package was templated with [`create-typescript-app`](https://github.com/JoshuaKGoldberg/create-typescript-app) using the [Bingo engine](https://create.bingo).