an eslint plugin to enforce some best practices
MIT License
an eslint plugin to enforce some best practices
npm install eslint-plugin-best-practices
// eslintrc.js
module.exports = {
plugins: ['eslint-plugin-best-practices'],
rules: {
'best-practices/explicit-internal-boundaries': ['error'],
'best-practices/isolated-route-files': ['error', {
'routeFiles': ['app/*/routes/**'],
'ignoredRouteFiles': ['.*', '**/*.css'],
}]
},
}
module.exports = {
rules: {
'best-practices/explicit-internal-boundaries': ['error'],
},
};
This rule enforces explicit boundaries for importing files from internal
directories. It ensures that files within an internal
folder can only be imported by files that reside within the same module or directory tree. This rule prevents access to internal-specific code from outside its designated module, reinforcing module encapsulation and promoting cleaner, more maintainable code architecture.
Enforcing explicit internal boundaries:
Here is an example of how you might configure this rule in your ESLint setup:
module.exports = {
rules: {
'best-practices/explicit-internal-boundaries': ['error'],
},
};
Here is an example of how this rule applies to a typical project structure:
./app
├── module-a
│ ├── internal
│ │ ├── helper-a.ts
│ │ └── config-a.ts
│ └── service-a.ts
├── module-b
│ ├── internal
│ │ └── helper-b.ts
│ └── service-b.ts
└── common
└── utils.ts
With the explicit-internal-boundaries
rule applied:
service-a.ts
can import helper-a.ts
or config-a.ts
because they are in the same module.service-b.ts
cannot import helper-a.ts
or config-a.ts
because it crosses module boundaries.utils.ts
in the common
directory cannot import any files from internal
directories in either module-a
or module-b
.This rule ensures that each module's internals remain isolated, reinforcing clear and robust architectural boundaries within the application.
best-practices/isolated-route-files
module.exports = {
rules: {
'best-practices/isolated-route-files': ['error', {
'routeFiles': ['app/*/routes/**'],
'ignoredRouteFiles': ['.*', '**/*.css'],
}],
},
};
This rule enforces isolation for route or page files, based on file-based routing conventions. It ensures that files designated as route handlers remain decoupled from the rest of the application, prohibiting imports from these files into any other part of the application. This rule supports a clean separation between routing mechanisms and business logic, adhering to modern frontend architecture practices.
Maintaining isolation of route files:
Here is an example of how you might configure this rule in your ESLint setup:
module.exports = {
rules: {
'best-practices/isolated-route-files': ['error', {
'routeFiles': ['app/*/routes/**'],
'ignoredRouteFiles': ['.*', '**/*.css', '**/*.test.{ts,tsx}'],
}],
},
};
Here is an example of the application structure with route files and the expected enforcement by this rule:
./app
├── analytics
│ ├── config.ts
│ └── routes
│ ├── api.ts
│ └── index.tsx
├── auth
│ ├── config.ts
│ └── routes
│ ├── auth.auth0.callback.tsx
│ ├── login.tsx
│ └── logout.tsx
├── broadcasts-notifications
│ ├── config.ts
│ └── routes
│ ├── $notificationId.tsx
│ └── _layout.tsx
With the isolated-route-files
rule applied:
api.ts
, index.tsx
, login.tsx
, etc., within any routes
directory are prohibited from being imported into other parts of the application.This rule promotes a disciplined use of routing files, keeping them isolated as per the file-based routing conventions in modern web development frameworks.