diff --git a/README.md b/README.md index d5fa5a68..502ec324 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ export default [ ...packageJson, rules: { ...packageJson.rules, - "package-json/valid-package-def": "off", + "package-json/valid-package-definition": "off", }, }, ]; @@ -96,7 +96,7 @@ module.exports = { parser: "jsonc-eslint-parser", plugins: ["package-json"], rules: { - "package-json/valid-package-def": "error", + "package-json/valid-package-definition": "error", }, }, ], @@ -117,19 +117,21 @@ The default settings don't conflict, and Prettier plugins can quickly fix up ord 💼 Configurations enabled in.\ ✅ 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). - -| Name                       | Description | 💼 | 🔧 | 💡 | -| :--------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------ | :- | :- | :- | -| [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. | ✅ | 🔧 | | -| [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`) | ✅ | | 💡 | -| [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-package-def](docs/rules/valid-package-def.md) | Enforce that package.json has all properties required by the npm spec | ✅ | | | -| [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-version](docs/rules/valid-version.md) | Enforce that package versions are valid semver specifiers | ✅ | | | +💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\ +❌ Deprecated. + +| Name                       | Description | 💼 | 🔧 | 💡 | ❌ | +| :--------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------ | :- | :- | :- | :- | +| [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. | ✅ | 🔧 | | | +| [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`) | ✅ | | 💡 | | +| [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-package-def](docs/rules/valid-package-def.md) | Enforce that package.json has all properties required by the npm spec | | | | ❌ | +| [valid-package-definition](docs/rules/valid-package-definition.md) | Enforce that package.json has all properties required by the npm spec | ✅ | | | | +| [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-version](docs/rules/valid-version.md) | Enforce that package versions are valid semver specifiers | ✅ | | | | diff --git a/docs/rules/valid-package-def.md b/docs/rules/valid-package-def.md index bb1e556b..9f314272 100644 --- a/docs/rules/valid-package-def.md +++ b/docs/rules/valid-package-def.md @@ -1,6 +1,6 @@ # valid-package-def -💼 This rule is enabled in the ✅ `recommended` config. +❌ This rule is deprecated. It was replaced by [`package-json/valid-package-definition`](valid-package-definition.md). diff --git a/docs/rules/valid-package-definition.md b/docs/rules/valid-package-definition.md new file mode 100644 index 00000000..0b269a58 --- /dev/null +++ b/docs/rules/valid-package-definition.md @@ -0,0 +1,54 @@ +# valid-package-definition + +💼 This rule is enabled in the ✅ `recommended` config. + + + +npm issues warnings after install if the `package.json` has a missing or invalid required property. +This rule uses [`package-json-validator`][pjv] to validate all `package.json` files against the [npm specification][npm-spec], and add any violations to lint warnings. + +## Rule Details + +This rule aims to ensure that `package.json` complies with specifications. + +Examples of **incorrect** code for this rule: + +```json +{ + "author": "", + "description": "", + "keywords": [], + "license": "ISC", + "main": "index.js", + "name": "noncompliant-version", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "version": "1.X.0" +} +``` + +Examples of **correct** code for this rule: + +```json +{ + "author": "", + "description": "", + "keywords": [], + "license": "ISC", + "main": "index.js", + "name": "compliant-version", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "version": "1.0.0" +} +``` + +## When Not To Use It + +npm may complain, but it works perfectly with many package files that do not violate spec. +If you don't mind those complaints then you can disable this rule. + +[pjv]: https://github.com/gorillamania/package.json-validator +[npm-spec]: https://docs.npmjs.com/files/package.json diff --git a/src/plugin.ts b/src/plugin.ts index 9b1e3c33..8d54f3d8 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,12 +1,14 @@ import { createRequire } from "node:module"; +import type { PackageJsonRuleModule } from "./createRule.js"; + import { rule as orderProperties } from "./rules/order-properties.js"; import { rule as preferRepositoryShorthand } from "./rules/repository-shorthand.js"; import { rule as sortCollections } from "./rules/sort-collections.js"; import { rule as uniqueDependencies } from "./rules/unique-dependencies.js"; import { rule as validLocalDependency } from "./rules/valid-local-dependency.js"; import { rule as validName } from "./rules/valid-name.js"; -import { rule as validPackageDef } from "./rules/valid-package-def.js"; +import { rule as validPackageDefinition } from "./rules/valid-package-definition.js"; import { rule as validRepositoryDirectory } from "./rules/valid-repository-directory.js"; import { rule as validVersion } from "./rules/valid-version.js"; @@ -17,14 +19,27 @@ const { name, version } = require("../package.json") as { version: string; }; -const rules = { +const rules: Record = { "order-properties": orderProperties, "repository-shorthand": preferRepositoryShorthand, "sort-collections": sortCollections, "unique-dependencies": uniqueDependencies, "valid-local-dependency": validLocalDependency, "valid-name": validName, - "valid-package-def": validPackageDef, + /** @deprecated use 'valid-package-definition' instead */ + "valid-package-def": { + ...validPackageDefinition, + meta: { + ...validPackageDefinition.meta, + deprecated: true, + docs: { + ...validPackageDefinition.meta.docs, + recommended: false, + }, + replacedBy: ["valid-package-definition"], + }, + }, + "valid-package-definition": validPackageDefinition, "valid-repository-directory": validRepositoryDirectory, "valid-version": validVersion, }; diff --git a/src/rules/valid-package-def.ts b/src/rules/valid-package-definition.ts similarity index 100% rename from src/rules/valid-package-def.ts rename to src/rules/valid-package-definition.ts diff --git a/src/tests/rules/valid-package-def.test.ts b/src/tests/rules/valid-package-definition.test.ts similarity index 92% rename from src/tests/rules/valid-package-def.test.ts rename to src/tests/rules/valid-package-definition.test.ts index d830bf2e..da8b9dd2 100644 --- a/src/tests/rules/valid-package-def.test.ts +++ b/src/tests/rules/valid-package-definition.test.ts @@ -1,7 +1,7 @@ -import { rule } from "../../rules/valid-package-def.js"; +import { rule } from "../../rules/valid-package-definition.js"; import { ruleTester } from "./ruleTester.js"; -ruleTester.run("valid-package-def", rule, { +ruleTester.run("valid-package-definition", rule, { invalid: [ { code: `{ "mane": "invalid-package" }`,