Skip to content

About

Import-structure lint rules for ESLint and Oxlint

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

eslint-plugin-import-structure

Can the #billing module import #auth without creating a cycle? With layers you can see the answer, and ESLint enforces it. This plugin turns the order of your package.json imports into those layers, and reports upward imports and deep imports into another module's internals. Works in both ESLint and Oxlint.

package.json imports listing #app, #billing, #auth, #utils in layer order, next to each module drawn as a tree of files behind its index.ts, importing only modules to its right

Why

A cycle compiles fine, then bites at runtime: ReferenceError: Cannot access 'x' before initialization in native ESM, or a silent undefined from CommonJS output and some bundlers. Worse, it hides until something shuffles the import order.

Here, each # alias is a module with one front door, and the alias order is a one-way layer order. A module imports only the modules listed below it, so cycles between modules can't form in the first place. Unlike cycle detectors that walk the whole import graph, each rule checks one file against the declared layers, so it stays fast and the error names the alias to fix.

package.json becomes your architecture map.

Install

npm install --save-dev eslint-plugin-import-structure
# for ESLint, also:
npm install --save-dev eslint typescript-eslint jiti

Requires Node.js 22.18+.

Usage

// ESLint 9+: eslint.config.ts (needs eslint, typescript-eslint, and jiti)
import plugin from "eslint-plugin-import-structure";
import { parser } from "typescript-eslint";

const ts = { files: ["**/*.ts"], languageOptions: { parser } };
export default [ts, plugin.configs.modules];
// Oxlint, through Vite+: vite.config.ts
import plugin from "eslint-plugin-import-structure";
import { defineConfig } from "vite-plus";

const rules = { ...plugin.configs.modules.rules };
export default defineConfig({
  lint: { jsPlugins: ["eslint-plugin-import-structure"], rules },
});

Then declare your modules in package.json, top layer first:

{
  "imports": {
    "#billing": "./src/billing/index.ts",
    "#auth": "./src/auth/index.ts",
    "#db": "./src/db/index.ts"
  }
}
// in src/auth/session.ts
import { charge } from "#billing"; // ❌ upward import (enforce-layer-order)
import { query } from "#db"; // ✅ a lower layer, through its alias
import { pool } from "#db/pool.ts"; // ❌ deep import (no-barrel-bypass)

import { x } from "../x.ts"; // ❌ parent path (no-parent-import)
import { hash } from "./hash.ts"; // ✅ a sibling in the same module

Configs and rules

  • modules: every rule below, all at error.
  • all: every rule, including rules added later. Today it equals modules.

No rule takes options.

Modules

The order of the # aliases in package.json is the layer order, top first; a module may only import aliases declared below its own. An alias owns its target, and code outside the owned area reaches it only through the alias. The rules never depend on folder or file names; an alias may point anywhere.

  • An alias to a folder's index file (index.ts, or any other extension) owns that folder and everything below it. An alias to any other file owns only that file.
  • An alias may sit inside another alias's area. The deeper alias owns its part, carved out of the outer area, and the outer alias owns the rest; the two are ordered like any other pair of aliases.
  • No 2 aliases may have the same area, such as the same target or 2 index files in one folder. enforce-layer-order reports an overlap once at the top of every file both own, since lint cannot report on package.json.
  • Outside an area, any other route to an owned file is a bypass: a deeper alias specifier such as #a/one.ts (no-barrel-bypass), or a relative path (no-relative-bypass, and prefer-alias-import, which rewrites a path to the alias's target, in any spelling TypeScript resolves to it).
  • Inside an area, siblings are free, and a subfolder is reached only through its direct index.ts, like a Rust mod.rs (no-relative-bypass); a path deeper in the subfolder, a grandchild's index.ts included, fires, and a subfolder without an index.ts cannot be imported at all.
  • ../ is banned in every file, owned or not (no-parent-import). Anything above a file needs an alias.
  • Files no alias owns need no index.ts and no alias, and import ./ siblings and subfolders freely.
  • Don't declare /* subpath patterns: any use of one that reaches an owned area is a bypass (no-barrel-bypass). Entry points are named index.ts only (enforce-mod-file).
"imports": {
  "#a": "./a/index.ts", // owns a/ and everything below it but a/sub/
  "#a-sub": "./a/sub/index.ts", // owns a/sub/, carved out of #a
  "#f": "./b/f.ts", // owns only b/f.ts; b/g.ts stays unowned
}
// in main.ts, which no alias owns
import { a } from "#a"; // ✅
import { two } from "./a/two.ts"; // ❌ relative path into #a
import { f } from "./b/f.ts"; // ❌ relative path to a file alias; use "#f"
import { g } from "./b/g.ts"; // ✅ unowned
// in a/one.ts, owned by #a
import { two } from "./two.ts"; // ✅ sibling
import { other } from "./other/index.ts"; // ✅ direct subfolder's index.ts
import { deep } from "./other/deep.ts"; // ❌ past ./other/index.ts
import { sub } from "./sub/x.ts"; // ❌ relative path into #a-sub
import { sub } from "#a/sub/x.ts"; // ❌ deeper alias specifier; use "#a-sub"

The other rules (import order, duplicate sources, wildcards, absolute paths, self-imports) are not about aliases and apply to every file.

Rules

💼 Configurations enabled in.
🧭 Set in the modules configuration.
🔧 Automatically fixable by the --fix CLI option.
💡 Manually fixable by editor suggestions.

Name                       Description 💼 🔧 💡
enforce-import-order Enforce grouped and sorted import declarations and import members 🧭 🔧
enforce-layer-order Enforce the order of package.json imports as the layer order 🧭
enforce-mod-file Enforce index.ts as the name of module entry points 🧭
no-absolute-import Disallow absolute paths and file: URLs as module specifiers 🧭
no-barrel-bypass Disallow # specifiers deeper than an alias 🧭 💡
no-duplicate-import-source Disallow separate import declarations from the same source 🧭
no-parent-import Disallow parent-relative module specifiers 🧭 🔧
no-relative-bypass Disallow relative specifiers that reach into an alias's area from outside it or past a subfolder's index file 🧭
no-self-import Disallow a module from importing itself 🧭
no-wildcard-import Disallow namespace imports 🧭
prefer-alias-import Require the # alias for a module that package.json declares 🧭 🔧

License: MIT.

About

Import-structure lint rules for ESLint and Oxlint

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages