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

Add a feature which warns if a path can be resolved to something less complex #784

Closed
alasdairhurst opened this issue Mar 31, 2017 · 9 comments

Comments

@alasdairhurst
Copy link

alasdairhurst commented Mar 31, 2017

For example, if have an import x from
'../../../x/y/z' and it can be written more consisely: import x from 'z' (if you are in '/x/y') there should be a rule letting me know. This should be auto-fixable as well.
A better example for this could be when aliases are used and you have things like this all around your tests:

import * as authActions from '../../../../../src/components/pages/authentication/actions';

since the components dir is in my webpack config as an alias, it should warn me because i can write

import * as authActions from 'components/pages/authentication/actions';

instead.

@alasdairhurst
Copy link
Author

If this gets a good response, i wouldn't mind implementing it myself.

@ljharb
Copy link
Member

ljharb commented Mar 31, 2017

Duplicate of #776.

@ljharb ljharb closed this as completed Mar 31, 2017
@alasdairhurst
Copy link
Author

@ljharb almost, but not the same use case. #776 does not mention anything to do with aliases. In this case the path is perfectly valid, but i can still write it shorter. #776 seems to be about figuring out valid paths from invalid ones.

@ljharb
Copy link
Member

ljharb commented Mar 31, 2017

#776 is about warning when excessive directory traversals are used in valid paths only.

So you want a linting rule that forces use of webpack aliases when they are configured?

I'm -1 on that rule mainly because I think webpack aliases are a horrible idea, but I won't block if other collabs think it would be useful.

@alasdairhurst
Copy link
Author

alasdairhurst commented Mar 31, 2017

@ljharb I wouldn't say that it would be forcing webpack aliases. It's perfectly possible to have a webpack alias which is a longer path than the relative path. In this case the relative path should be preferred.

use-shortest-path or something? I'm sure configuration could be added to choose if (webpack) aliases are tested against or not.

@ljharb
Copy link
Member

ljharb commented Mar 31, 2017

Is there any utility for this rule when not using webpack aliases?

@alasdairhurst
Copy link
Author

@ljharb not that i know of. Although i just started looking for one today.

@benmosher
Copy link
Member

I'm a minus one on this as well. Sounds like a job for a Webpack-specific ESLint plugin. (which could totally use eslint-module-utils + the Webpack resolver, no need to duplicate those)

Thanks!

@danny-andrews
Copy link
Contributor

@alasdairhurst You can force alias use by restricting all uses of ../* using the core rule no-restricted-imports! See my comment here!

Note: this won't handle enforcing using one alias over another to make the path shorter. E.g. my-app/components/MyComponent vs components/MyComponent, but I think using multiple aliases is a bad idea anyway and causes more trouble than its worth. In my experience, using one alias at your src root gives you the most bang for your buck.

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

No branches or pull requests

4 participants