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 26, 2019
1 parent 7f30a16 commit b2c17d0
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,17 @@ A list of file extensions that will be parsed as modules and inspected for
This defaults to `['.js']`, unless you are using the `react` shared config,
in which case it is specified as `['.js', '.jsx']`.

```js
"settings": {
"import/extensions": [
".js",
".jsx"
]
}
```

If you require more granular extension definitions, you can use:

```js
"settings": {
"import/resolver": {
Expand Down
26 changes: 26 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,8 @@ 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)
import "./pages/index.js"; // should be "./pages" (except if there is a ./pages.js file)
```

The following patterns are NOT considered problems:
Expand All @@ -46,3 +51,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 other resolved file extensions. See [Settings: import/extensions](https://github.com/benmosher/eslint-plugin-import#importextensions) for details.
51 changes: 42 additions & 9 deletions src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @author Thomas Grainger
*/

import { getFileExtensions } from 'eslint-module-utils/ignore'
import moduleVisitor from 'eslint-module-utils/moduleVisitor'
import resolve from 'eslint-module-utils/resolve'
import path from 'path'
Expand Down Expand Up @@ -31,9 +32,9 @@ 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)
}

module.exports = {
meta: {
Expand All @@ -47,6 +48,7 @@ module.exports = {
type: 'object',
properties: {
commonjs: { type: 'boolean' },
detectUselessIndex: { type: 'boolean' },
},
additionalProperties: false,
},
Expand All @@ -55,25 +57,27 @@ module.exports = {
fixable: 'code',
messages: {
uselessPath: 'Useless path segments for "{{ path }}", should be "{{ proposedPath }}"',
uselessAmbiguousPath: 'Useless path segments for "{{ path }}", import path would be ' +
'ambiguous: "{{ ambiguousPath1 }}" and "{{ ambiguousPath2 }}"',
},
},

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

function report(proposedPath) {
function reportWithProposedPath(proposedPath) {
context.report({
node: source,
messageId: 'uselessPath',
data: {
path: importPath,
proposedPath,
},
fix: fixer => fixer.replaceText(source, JSON.stringify(proposedPath)),
fix: fixer => proposedPath && fixer.replaceText(source, JSON.stringify(proposedPath)),
})
}

Expand All @@ -87,7 +91,36 @@ module.exports = {
const normedPath = normalize(importPath)
const resolvedNormedPath = resolve(normedPath, context)
if (normedPath !== importPath && resolvedPath === resolvedNormedPath) {
return report(normedPath)
return reportWithProposedPath(normedPath)
}

const fileExtensions = getFileExtensions(context.settings)
const regexUnnecessaryIndex = new RegExp(
`.*\\/index(\\${Array.from(fileExtensions).join('|\\')})?$`
)

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

// Try to find ambiguous imports (to avoid breaking import paths with auto-fixing)
if (parentDirectory !== '.' && parentDirectory !== '..') {
for (let fileExtension of fileExtensions) {
if (resolve(`${parentDirectory}${fileExtension}`, context)) {
return context.report({
node: source,
messageId: 'uselessAmbiguousPath',
data: {
path: importPath,
ambiguousPath1: `${parentDirectory}${fileExtension}`,
ambiguousPath2: parentDirectory,
},
})
}
}
}

return reportWithProposedPath(parentDirectory)
}

// Path is shortest possible + starts from the current directory --> Return directly
Expand All @@ -113,7 +146,7 @@ module.exports = {
}

// Report and propose minimal number of required relative parents
return report(
return reportWithProposedPath(
toRelativePath(
importPathSplit
.slice(0, countExpectedRelativeParents)
Expand All @@ -123,6 +156,6 @@ module.exports = {
)
}

return moduleVisitor(checkSourceValue, config)
return moduleVisitor(checkSourceValue, options)
},
}
57 changes: 55 additions & 2 deletions tests/src/rules/no-useless-path-segments.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ 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
test({ code: 'import "../my-custom-index"', options: [{ detectUselessIndex: true }] }),
],

invalid: [
Expand Down Expand Up @@ -61,6 +64,31 @@ 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 ".."'],
}),
test({
code: 'require("./bar/index")',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "./bar/index", import path would be ambiguous: "./bar.js" and "./bar"'],
}),
test({
code: 'require("../index.js")',
options: [{ commonjs: true, detectUselessIndex: true }],
errors: ['Useless path segments for "../index.js", should be ".."'],
}),

// esmodule
test({
Expand Down Expand Up @@ -95,8 +123,33 @@ function runResolverTests(resolver) {
code: 'import "./deep//a"',
errors: [ 'Useless path segments for "./deep//a", should be "./deep/a"'],
}),
],
})
test({
code: 'import "./importType/index"',
options: [{ detectUselessIndex: true }],
errors: ['Useless path segments for "./importType/index", should be "./importType"'],
}),
test({
code: 'import "./index"',
options: [{ detectUselessIndex: true }],
errors: ['Useless path segments for "./index", should be "."'],
}),
test({
code: 'import "../index"',
options: [{ detectUselessIndex: true }],
errors: ['Useless path segments for "../index", should be ".."'],
}),
test({
code: 'import "./bar/index"',
options: [{ detectUselessIndex: true }],
errors: ['Useless path segments for "./bar/index", import path would be ambiguous: "./bar.js" and "./bar"'],
}),
test({
code: 'import "../index.js"',
options: [{ detectUselessIndex: true }],
errors: ['Useless path segments for "../index.js", should be ".."'],
}),
],
})
}

['node', 'webpack'].forEach(runResolverTests)
1 change: 1 addition & 0 deletions utils/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function makeValidExtensionSet(settings) {

return exts
}
exports.getFileExtensions = makeValidExtensionSet

exports.default = function ignore(path, context) {
// check extension whitelist first (cheap)
Expand Down

0 comments on commit b2c17d0

Please sign in to comment.