enforce-dependency-direction
Ensures dependencies flow in the correct architectural direction
💼 This rule is enabled in the following configs: recommended, typescript.
💡 This rule is manually fixable by editor suggestions.
Rule Details
Dependencies point inward: a layer may depend on the layers listed before it in layers (default domain, application, infrastructure, presentation), never on a later one. Every way a module is loaded or re-exported is checked: import, side-effect import, export … from, export * from, require(), import() and TypeScript's import x = require().
A file's layer is the layerPatterns directory on its path (default: the four layers plus ui and api; when only layers is set, the layers themselves). A relative specifier is resolved against the importing file and takes the layer of the path it resolves to. An import from a later layer is reported; with allowSameLayer: false, an import from the file's own layer is reported too.
Not reported: a file or target outside every layer directory, or in one not listed in layers; packages (their layer is unknown without resolver configuration).
❌ Incorrect
// src/domain/user.ts
import { db } from '../infrastructure/db';
export * from '../application/service';✅ Correct
// src/application/service.ts
import { User } from '../domain/user';
import { helper } from './helper';Options
[
{
"type": "object",
"properties": {
"layers": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"domain",
"application",
"infrastructure",
"presentation"
],
"description": "Layer definitions with dependency order"
},
"layerPatterns": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"domain",
"application",
"infrastructure",
"presentation",
"ui",
"api"
],
"description": "Layer directory patterns"
},
"allowSameLayer": {
"type": "boolean",
"default": true,
"description": "Allow same-layer imports"
}
},
"additionalProperties": false
}
]Implementation
OWASP Foundation
- Category: A00:2021 - General Security
Use this rule
npm install -D oxlint-plugin-import-next{ "jsPlugins": ["oxlint-plugin-import-next"], "rules": { "import-next/enforce-dependency-direction": "error" }}import { defineConfig } from 'vite-plus';export default defineConfig({ lint: { jsPlugins: ['oxlint-plugin-import-next'], rules: { 'import-next/enforce-dependency-direction': 'error', }, },});oxlint-plugin-import-next re-exports eslint-plugin-import-next/oxlint: the same rules under the same IDs, and no ESLint install. Why oxlint.
Did this rule catch something? Star the repo to get new CWE coverage as we ship it — or follow the AI-code-security benchmarks behind these rules.