Home > Enterprise >  Warn developers when using specific 3rd party components
Warn developers when using specific 3rd party components

Time:04-04

We use a 3rd party library where most components are used directly. But a few are wrapped in other internally developed components. Is there a way to warn developers when they attempt to use one of the 3rd party library's components that we wrapped?

I'm imagining something like a transpilation-time check for specific imports that would throw a build error. But ideally it would be a dev-time warning.

CodePudding user response:

The eslint rule "no-restricted-imports" allows you to configure banned imports: https://eslint.org/docs/rules/no-restricted-imports.

An error would appear both in the developer's IDE as well as during compilation.

CodePudding user response:

There are two ways I know of that you can leverage around this.

1.) Is by using. the no-restricted-imports eslint rule: https://eslint.org/docs/rules/no-restricted-imports

Basically you can add a lint to your project (either during build time or pre-commit) so that they can "warn" the developer if a certain import is used. (This can be seen both on your IDE and on build warnings):

Add this to your eslint configuration (https://eslint.org/docs/user-guide/getting-started):

"no-restricted-imports": ["warn", "my-3rd-party-librarty", "my-other-3rd-party-library"]

2.) The solution #1 only accounts if you are statically importing the library. However, it does not error/warn out if it was used by a different library as a dependency. If you want finer control, you can use Dependency Cruiser instead: https://www.npmjs.com/package/dependency-cruiser - and you can add to its config something like this:

{
  "forbidden": [
    {
      "name": "no-3rd-party-library",
      "comment": "Don't rely on this 3rd party library.",
      "severity": "warn",
      "from": {},
      "to": { "path": "my-3rd-party-library" }
    }
  ]
}
  • Related