Sub topic 11
Enforcing Boundaries with Lint Rules
This chapter assumes you know TypeScript, Angular dependency injection, and the barrel files and path aliases from the previous chapters. Folder conventions and barrels only work while people follow them. A lint rule makes the build fail when someone doesn't.
Why conventions are not enough
A barrel marks which symbols are public, but TypeScript still lets any file import a deep path such as src/app/orders/data-access/orders.store. Reviewers miss these imports. Over months, features reach into each other's internals, and a change in one feature breaks another.
A lint rule turns the convention into a check that runs in the editor and in CI.
Tag your code with layers
Rules need a vocabulary. In an Nx workspace, each project (an app or library) gets tags in its project.json. Use one tag for the layer and, if you like, one for the feature:
{
"name": "orders-feature",
"tags": ["type:feature", "scope:orders"]
}The type: tags match the layers from the earlier chapter: type:feature, type:ui, type:data-access and type:util.
Declare which tags may depend on which
The @nx/enforce-module-boundaries rule reads those tags. You list the allowed dependencies for each source tag:
{
"@nx/enforce-module-boundaries": [
"error",
{
"depConstraints": [
{
"sourceTag": "type:feature",
"onlyDependOnLibsWithTags": ["type:ui", "type:data-access", "type:util"]
},
{
"sourceTag": "type:ui",
"onlyDependOnLibsWithTags": ["type:ui", "type:util"]
}
]
}
]
}With this setting, a ui library that imports a data-access library produces a lint error. The error names the offending import, so the fix is easy to find.
Keep features apart with scope tags
Layer rules stop upward imports. They do not stop orders from importing customers. Add a scope constraint for that:
{
"sourceTag": "scope:orders",
"onlyDependOnLibsWithTags": ["scope:orders", "scope:shared"]
}Now orders code can use its own libraries and shared ones only. If it needs customer data, the two features must communicate through a shared contract, which the next chapter covers.
Without Nx: plain ESLint
If you don't use Nx, ESLint's built-in no-restricted-imports rule blocks deep imports into a feature:
{
"rules": {
"no-restricted-imports": [
"error",
{
"patterns": [
{
"group": ["**/orders/*/**"],
"message": "Import from @app/orders instead of reaching into its internals."
}
]
}
]
}
}Apply this block only to files outside the orders folder using an overrides entry. Otherwise the feature cannot import its own files. Plugins such as eslint-plugin-boundaries give tag-style rules without Nx.
Common errors
- Circular dependency between libraries. The rule reports it. Move the shared code into a lower layer such as
util. - Constraints too loose. If every tag may depend on every other tag, the rule never fires. Start strict and loosen with a reason.
- Missing tags. An untagged project matches no constraint. Give every project at least a
type:tag. - Imports through relative paths. Importing a library with
../../bypasses its alias. The Nx rule flags this, so always import through the alias.
Exercise
A type:ui library imports a service from a type:data-access library. Your lint rule has a constraint for type:ui that lists only type:ui and type:util. What happens, and how do you fix it properly?
Show answer
Lint reports an error because type:data-access is not in the allowed list for type:ui. Don't add it to the list. Instead, make the ui component take the data through an input() and let a feature component, which may depend on data-access, fetch the data and pass it down.
Exercise
Why do layer tags alone not stop the orders feature from importing from customers, and what do you add?
Show answer
Both are type:feature projects, and a feature may depend on other libraries by layer. Add scope tags such as scope:orders and a constraint that limits each scope to its own scope plus scope:shared.