Sub topic 12
Enforcing boundaries with ESLint
A public API only works if people use it. Code review catches some violations, but a lint rule catches all of them, every time. This chapter adds that check. It assumes you know the feature-module layout, the index.ts public API, and the @/ path alias from earlier chapters.
Why conventions need enforcement
Suppose orders needs a cart total. Importing @/features/cart/components/CartList is one line, and it works. It also ties orders to a file that the cart team never promised to keep stable. Move or rename that file and orders breaks.
A few of these imports are harmless. Hundreds of them make every internal refactor risky, and the feature boundary exists only on paper.
The rule: restrict deep imports
ESLint's built-in no-restricted-imports rule can ban import paths that match a pattern. Because the alias gives every feature import the same shape, one pattern covers all features.
// eslint.config.js
export default [
{
files: ["src/**/*.{ts,tsx}"],
rules: {
"no-restricted-imports": [
"error",
{
patterns: [
{
group: ["@/features/*/*"],
message: "Import from the feature's index file instead of its internals.",
},
],
},
],
},
},
];The pattern @/features/*/* matches a feature name followed by anything deeper. These imports behave differently:
| Import | Result |
|---|---|
| `@/features/cart` | Allowed. It resolves to `index.ts`. |
| `@/features/cart/hooks/useCart` | Reported as an error. |
| `@/shared/ui/Button` | Allowed. The pattern only covers `features`. |
Allowing a feature to import itself
The rule above has a gap. Inside cart, files often import each other. If they use the alias, the rule flags them. Inside a feature, use relative imports, as the useOrders hook does with ../api. Relative paths do not match the alias pattern, so they pass.
// src/features/cart/components/CartList.tsx
import { useCart } from "../hooks/useCart"; // relative: allowed
import { Button } from "@/shared/ui/Button"; // shared code: allowedThis also gives you a readable convention. An alias import means "I am crossing a boundary". A relative import means "I am inside my own feature".
How the check runs
Lint only protects you if it runs where people cannot skip it. Run it in CI as a required check, and in the editor for fast feedback.
Common errors
- The rule never fires. The
filesglob does not match your sources, or the config is not the one ESLint loads. Runnpx eslint src --print-config src/app/App.tsxand look for the rule. - The rule fires on valid code. Tests or the
appfolder may need a different policy. Add a second config object with a narrowerfilesglob and override the rule there. - Deep imports through relative paths.
../../cart/hooks/useCartis not caught by the alias pattern. Add"**/features/*/*/**"carefully, or use a boundary plugin such aseslint-plugin-boundariesif you need path-independent checks. - Missing exports. After you ban deep imports, features may need names they never exported. Add them to
index.tsdeliberately. Do not disable the rule.
Exercise
A developer in orders writes import { useCart } from "@/features/cart/hooks/useCart". The lint rule reports an error. What is the correct fix, and what must be true of cart for the fix to work?
Show answer
Change the import to import { useCart } from "@/features/cart". This works only if src/features/cart/index.ts exports useCart. If it does not, add the export to the index file, which makes it part of the feature's public API.
Exercise
Why does the pattern @/features/*/* not flag import { Button } from "@/shared/ui/Button"?
Show answer
The pattern starts with @/features/, so only paths under features can match. The shared folder is outside the pattern.