Skip to content

Commit

Permalink
no-useless-path-segments: Add detectUselessIndex option
Browse files Browse the repository at this point in the history
  • Loading branch information
timkraut committed Feb 19, 2019
1 parent a00d312 commit 6c97e3b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
23 changes: 23 additions & 0 deletions docs/rules/no-useless-path-segments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ my-project
├── app.js
├── footer.js
├── header.js
└── helpers.js
└── helpers
└── index.js
└── pages
├── about.js
├── contact.js
Expand All @@ -30,6 +33,7 @@ import "../pages/about.js"; // should be "./pages/about.js"
import "../pages/about"; // should be "./pages/about"
import "./pages//about"; // should be "./pages/about"
import "./pages/"; // should be "./pages"
import "./pages/index"; // should be "./pages" (except if there is a ./pages.js file)
```

The following patterns are NOT considered problems:
Expand All @@ -46,3 +50,22 @@ import ".";
import "..";
import fs from "fs";
```

## Options

### detectUselessIndex

If you want to detect unnecessary `/index` imports in your paths, you can enable the option `detectUselessIndex`. By default it is set to `false`:
```js
"import/no-useless-path-segments": ["error", {
detectUselessIndex: true,
}]
```

Additionally to the patterns described above, the following imports are considered problems if `detectUselessIndex` is enabled:

```js
// in my-project/app.js
import "./helpers/index"; // should be "./helpers" (not auto-fixable because of helpers.js)
import "./pages/index"; // should be "./pages" (auto-fixable)
```
20 changes: 17 additions & 3 deletions src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = {
type: 'object',
properties: {
commonjs: { type: 'boolean' },
detectUselessIndex: { type: 'boolean' },
},
additionalProperties: false,
},
Expand All @@ -60,7 +61,7 @@ module.exports = {

create(context) {
const currentDir = path.dirname(context.getFilename())
const config = context.options[0]
const options = context.options[0]

function checkSourceValue(source) {
const { value: importPath } = source
Expand All @@ -73,7 +74,7 @@ module.exports = {
path: importPath,
proposedPath,
},
fix: fixer => fixer.replaceText(source, JSON.stringify(proposedPath)),
fix: fixer => proposedPath && fixer.replaceText(source, JSON.stringify(proposedPath)),
})
}

Expand All @@ -90,6 +91,19 @@ module.exports = {
return report(normedPath)
}

// Path contains unnecessary /index
if (options && options.detectUselessIndex && importPath.endsWith('index')) {
const parentDirectory = path.dirname(importPath)

// TODO Find a way to get list of possible file extensions from ESLint config
if (resolve(`${parentDirectory}.js`, context)) {
// No proposed path as the import is ambiguous (e.g. /bar.js and /bar/index.js exist
report()
}

report(parentDirectory)
}

// Path is shortest possible + starts from the current directory --> Return directly
if (importPath.startsWith('./')) {
return
Expand Down Expand Up @@ -123,6 +137,6 @@ module.exports = {
)
}

return moduleVisitor(checkSourceValue, config)
return moduleVisitor(checkSourceValue, options)
},
}
32 changes: 32 additions & 0 deletions tests/src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function runResolverTests(resolver) {
test({ code: 'import "."' }),
test({ code: 'import ".."' }),
test({ code: 'import fs from "fs"' }),
test({ code: 'import fs from "fs"' }),
test({ code: 'import "../index"' }), // detectUselessIndex is false by default
],

invalid: [
Expand Down Expand Up @@ -61,6 +63,21 @@ function runResolverTests(resolver) {
options: [{ commonjs: true }],
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
test({
code: 'require("./importType/index")',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "./importType/index", should be "./importType"'],
}),
test({
code: 'require("./index")',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "./index", should be "."'],
}),
test({
code: 'require("../index")',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "../index", should be ".."'],
}),

// esmodule
test({
Expand Down Expand Up @@ -95,6 +112,21 @@ function runResolverTests(resolver) {
code: 'import "./deep//a"',
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
test({
code: 'import "./importType/index"',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "./importType/index", should be "./importType"'],
}),
test({
code: 'import "./index"',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "./index", should be "."'],
}),
test({
code: 'import "../index"',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "../index", should be ".."'],
}),
],
})
}
Expand Down

0 comments on commit 6c97e3b

Please sign in to comment.