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 1b1abc0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 6 deletions.
25 changes: 25 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,24 @@ 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)
```

Note: `detectUselessIndex` detects only ambiguous imports for `.js` files if you haven't specified all resolved file extensions. See [Settings: import/extensions](https://github.com/benmosher/eslint-plugin-import#importextensions) for details.
44 changes: 38 additions & 6 deletions src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,24 @@ function normalize(fn) {
return toRelativePath(path.posix.normalize(fn))
}

const countRelativeParents = (pathSegments) => pathSegments.reduce(
(sum, pathSegment) => pathSegment === '..' ? sum + 1 : sum, 0
)
function countRelativeParents(pathSegments) {
return pathSegments.reduce((sum, pathSegment) => pathSegment === '..' ? sum + 1 : sum, 0)
}

function getFileExtensions(settings) {
if (
settings &&
settings['import/resolver'] &&
settings['import/resolver'].node &&
settings['import/resolver'].node.extensions
) {
return settings['import/resolver'].node.extensions
} else {
return [
'js',
]
}
}

module.exports = {
meta: {
Expand All @@ -47,6 +62,7 @@ module.exports = {
type: 'object',
properties: {
commonjs: { type: 'boolean' },
detectUselessIndex: { type: 'boolean' },
},
additionalProperties: false,
},
Expand All @@ -60,7 +76,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 +89,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 +106,22 @@ module.exports = {
return report(normedPath)
}

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

// Try to find ambiguous imports (to avoid breaking import paths with auto-fixing)
const fileExtensions = getFileExtensions(context.settings)
fileExtensions.forEach(fileExtension => {
if (resolve(`${parentDirectory}${fileExtension}`, context)) {
// No fix possible as import is ambiguous (e.g. /bar.js and /bar/index.js exist both)
return report()
}
})

return report(parentDirectory)
}

// Path is shortest possible + starts from the current directory --> Return directly
if (importPath.startsWith('./')) {
return
Expand Down Expand Up @@ -123,6 +155,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 1b1abc0

Please sign in to comment.