Skip to content
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

[perhaps a Feature Request] Custom rule #108

Open
askarby opened this issue Mar 14, 2019 · 10 comments
Open

[perhaps a Feature Request] Custom rule #108

askarby opened this issue Mar 14, 2019 · 10 comments
Labels
enhancement 👑 New feature or request
Milestone

Comments

@askarby
Copy link

askarby commented Mar 14, 2019

It may just be me, but...

I can't seem to find a way to include my own, custom rule (just like ESLint and TSLint provides). Granted, I could create a rule, and submit a pull request for it, but I may have a very domain specific rule, that I want to apply.

Is there already an option to do that.... if not, this can be considered a feature request 😄

@tclindner
Copy link
Owner

Hey @askarby! We don't have that capability yet. I like the suggestion.

What rule are you thinking about adding?

@askarby
Copy link
Author

askarby commented Mar 20, 2019

I'm thinking about rules like:

  • disallowing relative paths in dependencies
  • disallowing git-repo dependencies
  • disallowing specific dependency versions (based on certain git repos)

etc.

Granted, I could contribute separate rules for those, but sometimes, the rules you require can be very specific (hence not very general purpose)

@tclindner tclindner added the enhancement 👑 New feature or request label Jun 16, 2019
@tclindner tclindner added this to To do in v4 via automation Jun 16, 2019
@tclindner tclindner added this to the v4.0.0 milestone Jun 16, 2019
@dietergeerts
Copy link

I'm in for this too. For example, we want to restrict the 'files' value for library packages in our mono-repo, so adding something to validate the value of it against something we want would be awesome.

@tclindner
Copy link
Owner

Thanks, @dietergeerts. I'm moving (slowly) through v4. I'm hoping this will be a part of it. Do you have any thoughts on your preferred interface for writing custom rules? I'm starting to think through how this might work.

@aaroncadrian
Copy link

I too would love this feature. In terms of an interface, I'd prefer to have one similar to that of tslint's custom rules API.

On that same note, I'd prefer to have a TypeScript interface available to write custom rules.

@tclindner tclindner removed this from To do in v4 Jan 30, 2020
@tclindner tclindner modified the milestones: v4.0.0, v5.0.0 Jan 30, 2020
@StephenEsser
Copy link

Consolidating issue #208 here. Adding the notes mentioned on the other issue:

There are use cases where certain projects may want to implement custom rule integration. A plugin system similar to adding rules to eslint or stylelint.

Examples of custom rules:

  • Disallow specific packages from being listed in dependencies, devDependencies, or peerDependencies.
  • Require certain dependencies be listed in certain sections. IE: Package A can never be listed in dependencies. It should always be a devDependency.
  • Mutually exclusive dependencies. IE: If dependency A is listed dependency B cannot be.
  • Require certain custom keys be populated in the package.json
  • Versioning restraints. Dependency A should never be hard locked. "1.0.0" vs "^1.0.0"

@dietergeerts
Copy link

dietergeerts commented Nov 11, 2021

Would it not be a possibility to have a JSON Schema provided that can be checked? Because then we can provide all our custom rules through that JSON Schema. Off course, error messages may be a bit more difficult to understand then, but at least, then we can define any custom rule we want. It is JSON after all.

I like a lot of the rules that are available, and they make it easy to configure and know what it will check without needing to know some internals on JSON Schemas, but the way I see it is that such extra optional JSON Schema is similar to what ESLint provides with the https://eslint.org/docs/rules/no-restricted-syntax rule, where you can use a "selector" for the syntax you don't want.

@abejfehr
Copy link

abejfehr commented Mar 15, 2022

A few custom rules I'd make if I could:

  • enforce that there's no resolutions field in a package.json file
  • enforce that a specific value is set in the browserlist field so that all front end repos support the same set of browsers
  • enforce that workspaces don't have any root-level packages (or a specified set of them)
  • enforce certain commands existing

@kevinbarabash
Copy link

I would like to create a rule for enforcing naming conventions for scripts. We have multiple repos and the naming is all over the place.

@pawelgur
Copy link

pawelgur commented Apr 22, 2024

Even though it is not officially supported and requires using internal api, but custom rules can be added using nodejs api:

// lint-package.mjs, run this with `node lint-package.mjs`
import path from 'path';
import { fileURLToPath } from 'url';
import { NpmPackageJsonLint } from 'npm-package-json-lint';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

const npmPackageJsonLint = new NpmPackageJsonLint({
  patterns: ['./package.json'],
});

// add custom rule
npmPackageJsonLint.rules.rules['peer-deps-star'] = path.resolve(__dirname, './rule-peer-deps-star.js');

const results = npmPackageJsonLint.lint();
console.log(results); // need to handle results and throw errors manually here

Custom rule should be provided in commonjs format. Inspired by #596 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 👑 New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants