| 1 | # import/no-useless-path-segments
|
|---|
| 2 |
|
|---|
| 3 | 🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
|
|---|
| 4 |
|
|---|
| 5 | <!-- end auto-generated rule header -->
|
|---|
| 6 |
|
|---|
| 7 | Use this rule to prevent unnecessary path segments in import and require statements.
|
|---|
| 8 |
|
|---|
| 9 | ## Rule Details
|
|---|
| 10 |
|
|---|
| 11 | Given the following folder structure:
|
|---|
| 12 |
|
|---|
| 13 | ```pt
|
|---|
| 14 | my-project
|
|---|
| 15 | ├── app.js
|
|---|
| 16 | ├── footer.js
|
|---|
| 17 | ├── header.js
|
|---|
| 18 | └── helpers.js
|
|---|
| 19 | └── helpers
|
|---|
| 20 | └── index.js
|
|---|
| 21 | ├── index.js
|
|---|
| 22 | └── pages
|
|---|
| 23 | ├── about.js
|
|---|
| 24 | ├── contact.js
|
|---|
| 25 | └── index.js
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | The following patterns are considered problems:
|
|---|
| 29 |
|
|---|
| 30 | ```js
|
|---|
| 31 | /**
|
|---|
| 32 | * in my-project/app.js
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | import "./../my-project/pages/about.js"; // should be "./pages/about.js"
|
|---|
| 36 | import "./../my-project/pages/about"; // should be "./pages/about"
|
|---|
| 37 | import "../my-project/pages/about.js"; // should be "./pages/about.js"
|
|---|
| 38 | import "../my-project/pages/about"; // should be "./pages/about"
|
|---|
| 39 | import "./pages//about"; // should be "./pages/about"
|
|---|
| 40 | import "./pages/"; // should be "./pages"
|
|---|
| 41 | import "./pages/index"; // should be "./pages" (except if there is a ./pages.js file)
|
|---|
| 42 | import "./pages/index.js"; // should be "./pages" (except if there is a ./pages.js file)
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | The following patterns are NOT considered problems:
|
|---|
| 46 |
|
|---|
| 47 | ```js
|
|---|
| 48 | /**
|
|---|
| 49 | * in my-project/app.js
|
|---|
| 50 | */
|
|---|
| 51 |
|
|---|
| 52 | import "./header.js";
|
|---|
| 53 | import "./pages";
|
|---|
| 54 | import "./pages/about";
|
|---|
| 55 | import ".";
|
|---|
| 56 | import "..";
|
|---|
| 57 | import fs from "fs";
|
|---|
| 58 | ```
|
|---|
| 59 |
|
|---|
| 60 | ## Options
|
|---|
| 61 |
|
|---|
| 62 | ### noUselessIndex
|
|---|
| 63 |
|
|---|
| 64 | If you want to detect unnecessary `/index` or `/index.js` (depending on the specified file extensions, see below) imports in your paths, you can enable the option `noUselessIndex`. By default it is set to `false`:
|
|---|
| 65 |
|
|---|
| 66 | ```js
|
|---|
| 67 | "import/no-useless-path-segments": ["error", {
|
|---|
| 68 | noUselessIndex: true,
|
|---|
| 69 | }]
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | Additionally to the patterns described above, the following imports are considered problems if `noUselessIndex` is enabled:
|
|---|
| 73 |
|
|---|
| 74 | ```js
|
|---|
| 75 | // in my-project/app.js
|
|---|
| 76 | import "./helpers/index"; // should be "./helpers/" (not auto-fixable to `./helpers` because this would lead to an ambiguous import of `./helpers.js` and `./helpers/index.js`)
|
|---|
| 77 | import "./pages/index"; // should be "./pages" (auto-fixable)
|
|---|
| 78 | import "./pages/index.js"; // should be "./pages" (auto-fixable)
|
|---|
| 79 | ```
|
|---|
| 80 |
|
|---|
| 81 | Note: `noUselessIndex` only avoids ambiguous imports for `.js` files if you haven't specified other resolved file extensions. See [Settings: import/extensions](https://github.com/import-js/eslint-plugin-import#importextensions) for details.
|
|---|
| 82 |
|
|---|
| 83 | ### commonjs
|
|---|
| 84 |
|
|---|
| 85 | When set to `true`, this rule checks CommonJS imports. Default to `false`.
|
|---|