Use tslint to check for invalid imports between packages and folders in your TypeScript project.
Automatic validation and documentation of package architecture (via
tslint-folders is stable and in use every day in CI builds and on dev boxes (Linux, Mac, Windows) for at least one major product.
Save time spent manually code reviewing for 'silly' mistakes such as:
file-name-casingonly allows for 1 style)
We use SemVer for versioning. For the versions available, see the tags on this repository.
yarn add tslint-folders
tslint.json to have an entry
"rulesDirectory" that points to tslint-folders.
Normally this would be like:
See tslint.tslint-folders.json for an example.
The tslint rules are enabled and configured in
Optionally, you can split out the tslint-folders configuration into a separate file, like
tslint.tslint-folders.json. To reference the file, add this code to
Assuming tslint is already in place, then you should now see any unexpected imports (or disabled tests) be flagged by tslint. This should work as usual for tslint: on the command line, or in an editor such as Visual Code (may require refreshing the editor).
A diagram can be automatically generated from the same config used to validated the code:
see tslint-folders-diagrams for details.
here is a summary of the current custom rules.
|tsf-folders-disabled-test||Detect a disabled unit test, test suite or integration test.|
|tsf-folders-file-names||Validates casing of filenames. Similar to standard rule
|tsf-folders-import-from-disallowed-folders||Detect an import from a non-standard folder like node_modules|
|tsf-folders-imports-between-packages||Detect an import from a 'higher level' package: for example, import from an application 'shell' package when inside an 'area' package. This is the main custom rule. Also can detect when a package has imports using this packages name (instead of a relative path).|
|tsf-folders-test-with-breakpoint||Detect when an integration test has a break point like
|source code (github)||https://github.com/mrseanryan/tslint-folders|
All of the rules use the same prefix
To make it clear to developers that a custom rule is involved, all messages from the rules also include the rule id.
Some of these rules could be replaced by standard tslint configuration. However, having custom rules gives clearer messages to help the developer to know what to fix (or what rule to disable for that piece of code).
Some of the rules are not strictly about 'folders', but are included here as they also seem useful.
For more details and examples please see the unit tests
To work on the source code for tslint-folders, there are a few scripts:
|yarn build||Builds the rules to the 'dist' folder, from where they can be executed.|
|yarn lint||Lints the source code of the rules.|
|yarn start||Builds, tests and lints the code.|
|yarn test||Tests the rules against spec files (*.lint)|
|yarn test-one||Test a single rule against spec files (*.lint)|
The unit tests for the rule
tsf-folders-imports-between-packages are here.
The unit tests use test data to check the package boundaries of a fairly typical website.
The matching configuration can be seen in tslint.tslint-folders.json
The test data is based around a website that uses multiple packages:
|shell||The application shell - this is the top level package, and it can import from any other package.|
|todo-area||A 'todo app' area of the website, that is hosted within the shell. It should not import from the shell or from other 'area' packages.|
|todo-contact||A 'contact info' area of the website, that is hosted within the shell. It should not import from the shell or from other 'area' packages.|
|grid-package||A UI grid that is used by area packages. It should not import any other recognised packages.|
|utils||A 'utils' package used by the shell and area packages. It should not import any other recognised packages.|
Example validation: 'shell' should be able to import from 'todo-area', but not the other way around (shell is at a higher level of abstraction, and also want to avoid 2-way dependencies).
tslint-folders can also validate imports between sub-folders of a package.
The test data 'todo-area' package is configured with fairly typical sub-folders such as 'components' and 'models'. tslint.tslint-folders.json has been configured to check the imports between these folders.
|components||Top-level folder of UI components. Can import from any of the other recognised folders.|
|viewmodels||Folder of view models, used by the UI components. Can only import from models or utils.|
|models||Folder of models, used by the view models. Can only import from utils.|
|utils||A 'utils' folder. It should not import any other recognised folders.|
Example validation: 'components' should be able to import from 'models', but not the other way around (components is at a higher level of abstraction, and also want to avoid 2-way dependencies).
see the contributing readme.
This project is based on the excellent seeder project typescript-library-starter.
The project was started to avoid having to repeatedly fix similar coding issues in large TypeScript code bases.
That's pretty much it. Let me know if this is useful or how it can be improved!
Original work by Sean Ryan - mr.sean.ryan(at gmail.com)
This project is licensed under the MIT License - see the LICENSE file for details
Models the config in tslint.tslint-folders.JSON for rule 'tsf-folders-imports-between-packages'
Generated using TypeDoc