How to get a warning whenever a given function is imported, even indirectly?

200 views Asked by At

In my current TypeScript/Node.js project, we have implemented an internal cache module that we are progressively getting rid of.

To achieve that, I would like to progressively reduce the parts of the codebase that can import that module, directly or indirectly. And to be warned during development whenever we violate that rule.

Example:

  • users.ts is allowed to import cache.ts
  • domain.ts imports users.ts, but it’s not allowed to import cache.ts (even thru users.ts), so we want to be warned about that.

Do you know a way to enforce that rule with TSC, Eslint, or another static code analysis tool?

2

There are 2 answers

0
matt helliwell On

The closest thing I know is the eslint-plugin-boundaries project. It might be worth refactoring your modules for the cache so you can use some of these rules.

Failing that, writing a custom ESLint rule is fairly straight forward so long as you've got a set of rules you can apply to the AST.

1
Romain Linsolas On

Since you want to remove your cache module, I would recommend to use @deprecated jsdoc statement. This way, you will be warned every time you want to use it in new development. In addition to that, you can use an ESLint plugin to warn