I wonder if there is a way to prevent all files in a certain scope from importing any file from a different second scope. Example:
Given this project structure:
project/
├── node_modules/
├── test/
├── src/
│ ├── domain/
│ │ ├── SomeModelClass.ts
│ ├── application/
│ │ ├── SomeApplicationConcern.ts
│ ├── database/
│ │ ├── SomeRepository.ts
├── tsconfig.json
└── tslint.json
I would like to enforce at least some of these rules:
SomeApplicationConcern
can import code from anywhere.SomeRepository
can not import code fromapplication
SomeModelClass
can not import code from neitherapplication
nordomain
.
Can it be achieved somehow using nested tsconfig.json
files?
Can it be achieved using some fancy tslint
rules?
I have no clue if anything like this is possible. I would like to get a compilation error (or tslint error, which is set to error severity in my project) if a forbidden dependency is detected.
A few ideas based on some quick online research:
good-fences
is a dedicated tool to restrict imports in a TypeScript project. You'd have to add it to your build process as a separate step.module
set toes6
(to a separate output directory if you need a differentmodule
setting to generate the code you actually run) and then run ESLint with theno-restricted-imports
rule on the output.tsconfig.json
so that you can use only non-relative imports, and then use theno-relative-imports
rule fromtslint-microsoft-contrib
. However, there was talk of deprecatingno-relative-imports
.no-restricted-imports
and contribute it to tslint-eslint-rules.