import/no-restricted-paths

Disallow files in one folder from importing files in another

Also called import-x/no-restricted-paths

Some folders shouldn't depend on others. Code that ships to the browser shouldn't pull in server code, and a shared library shouldn't import from the app that uses it. You describe those boundaries as zones, and the rule reports every import that crosses one.

The rule does nothing until you give it zones. It checks import, export .. from, import() and require(), and only paths that resolve to a file.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-restricted-paths" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"import/no-restricted-paths" = ["error", {
  # zones = <list of objects>,
  # basePath = <string>,
}]

Examples#

bunfig.toml
[lint.rules]
"import/no-restricted-paths" = ["error", {
  zones = [{ target = "./src/client", from = "./src/server" }],
}]

With this zone, files in src/client can't import files in src/server.

Reported:

src/server/db.js
export function query() {}
src/client/app.jswith { "zones": [{ "target": "./src/client", "from": "./src/server" }] }1 problem
import { query } from "../server/db.js";

Not reported:

src/shared/format.js
export function formatDate() {}
src/client/app.jswith { "zones": [{ "target": "./src/client", "from": "./src/server" }] }No problems
import { formatDate } from "../shared/format.js";

Options#

OptionDefaultWhat it does
zones[]The list of zones.
basePaththe directory you run bun lint inThe directory that target and from are relative to.

Each zone has these keys:

KeyWhat it does
targetThe files the restriction applies to. A path, a glob, or a list of them. A path to a folder covers everything in it.
fromThe files that target can't import. A path, a glob, or a list. A list is either all paths or all globs.
exceptFiles inside from that you can still import.
messageText to add to the report.

If from is a path, each except is a path relative to from and must be inside it. If from is a glob, each except must be a glob too, and it needs to match the file's absolute path, like **/types.js.

With this zone, src/client/app.js can import ../server/types.js and nothing else from src/server:

bunfig.toml
[lint.rules]
"import/no-restricted-paths" = ["error", {
  zones = [{
    target = "./src/client",
    from = "./src/server",
    except = ["./types.js"],
    message = "Call the API instead.",
  }],
}]