Skip to content

πŸš€ Feature: add a rule to control which extra top-level package.json properties are allowedΒ #1435

@andreww2012

Description

@andreww2012

Feature Request Checklist

Overview

package.json allows arbitrary properties which are often used by other tools, such as prettier, lint-staged or pnpm. Some people prefer to consolidate different tools' settings in one place as much as possible to avoid creating a multitude of configuration files in the root of the repo - while others may prefer the opposite.

I have two use cases for which I would like a rule that can restrict certain top-level properties in package.json:

  • The first case (described above): I prefer to keep my package.json as minimal and clean as possible, since tools' options sometimes can be long and/or unrepresentable in JSON;
  • The second case: older versions of pnpm used the pnpm package.json key for pnpm specific options. Nowadays, the recommended way is to use a separate pnpm-workspace.yaml file, while the former method continues to work. It'd be nice to disallow pnpm property in that scenario.

Proposed rule name

restrict-top-level-properties

Proposed options

type Options = [{
  ban?: (string | {property: string; message?: string})[]; // Alternative option: Record<string, true | string>
}];

ban

Provides a list of banned top-level properties with an optional custom message shown in the error message when the banned property is used. Can include any property, even standard ones like name or version. If the same property is banned multiple times, the last declaration takes precedence. (Alternatively, ban could be represented as Record<string, true | string> where a string value represents a custom message.)

Dropped from this issue

allow

  • If omitted: all properties are allowed except those listed in ban.
  • If an empty array: no extra top-level properties are allowed (only standard package.json fields).
  • If a non-empty array: only the listed extra properties are allowed in addition to the standard fields.

ban can still be used alongside with allow to provide a custom message for specific properties.

If the same property appears in both allow and ban, the rule should throw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: accepting prsPlease, send a pull request to resolve this! πŸ™type: featureNew enhancement or request πŸš€

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions