Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning when import uses ../ too many times #1672

Closed
pustovalov opened this issue Mar 3, 2020 · 3 comments
Closed

Warning when import uses ../ too many times #1672

pustovalov opened this issue Mar 3, 2020 · 3 comments

Comments

@pustovalov
Copy link

Hey,

I use aliases provided by webpack:
https://webpack.js.org/configuration/resolve/#resolvealias

there is such a structure:

folder1/folder2/../folder6/a.js
folder1/utils/ajax.js

In a.js there is such a code:

import { notifyDevelopers } from '../../../../../utils/ajax'

using alias this code can be simplified to:

import { notifyDevelopers } from 'utils/ajax'

Can I create a rule that forbids making a path that is larger than ../../ ?

I found similar issues:
#776
#471

But #471 doesn't solve my problem

@ljharb
Copy link
Member

ljharb commented Mar 3, 2020

This already exists; see https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-useless-path-segments.md.

This won't (and shouldn't) take aliases into account, however, you'll have to build a custom rule for that.

@pustovalov
Copy link
Author

Thx, found a way to create a rule:

{
  "rules": {
    "no-restricted-imports": [
      "error",
      {
        "patterns": ["../../../*"]
      }
    ]
  }
}

@ljharb
Copy link
Member

ljharb commented Mar 3, 2020

That will only catch 3+ levels, but that does work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants