import/no-useless-path-segments

Disallow path segments in imports that can be removed

fixable

Also called import-x/no-useless-path-segments

./../format.js works, and so does ../pages/header.js written inside pages, but each has a shorter spelling that points to the same file. The long ones are usually left over from moving a file, and they make one module look like two when you search for it.

Only relative paths are checked. --fix writes the shorter path.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-useless-path-segments" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/no-useless-path-segments" = ["error", {
  commonjs = false,
  noUselessIndex = false,
}]

Examples#

Reported:

src/format.js
export function formatDate() {}
src/pages/header.js
export function Header() {}
src/pages/home.js2 problems
import { formatDate } from "./../format.js";
import { Header } from "../pages/header.js";

Not reported:

src/format.js
export function formatDate() {}
src/pages/header.js
export function Header() {}
src/pages/home.jsNo problems
import { formatDate } from "../format.js";
import { Header } from "./header.js";

Options#

OptionDefaultWhat it does
commonjsfalseAlso check require().
noUselessIndexfalseAlso report a path that ends in /index or /index.js, because the folder's name resolves to the same file.

With noUselessIndex, the first import is reported and the second isn't:

parts/index.js
export function Footer() {}
example.jswith { "noUselessIndex": true }1 problem
import { Footer } from "./parts/index.js";
import { Footer as SameFooter } from "./parts";

When not to use it#

Leave noUselessIndex off for code that runs as ES modules in Node.js or in a browser. There, an import needs to name the file, and ./parts doesn't find ./parts/index.js.