-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 no-useless-path-segments rule Fixes #471 #912
Conversation
avoid problems with ./ham/ -> ./ham/index.js by simply consuming '..', path segment pairs.
seems to not normalize trailing path seps. ./foo -> foo ./ -> ./ . -> . .. -> .. ../ -> ../
2 similar comments
1 similar comment
@benmosher not sure why it's still failing on appveyor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Could you add a lot more test cases, with things like ../foo/../../bar/foo/baz
etc?
It'd also be good to warn on /absolute/path/to/foo
when it in fact should be './foo
and similar.
import moduleVisitor from 'eslint-module-utils/moduleVisitor' | ||
|
||
function toRel(rel, sep) { | ||
const rSep = escapeRegExp(sep) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a way this could be done without having to escape regexes, or use them at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah it can be removed, this is only used on the posix side now.
Maybe on the no absolute rule? |
If I want to permit absolute paths, but not ones that could instead be relative, this one seems the appropriate place. |
Would need to know how far up is too far for absolute paths. '../../../../users/ljharb/Downloads/bower/jquery-ui' As that escapes out of the project |
Right, you'd locate the package.json, and consider that the root - anything within that should be relative instead of absolute. When phrased like that, however, it might make sense as an option to |
7768af7
to
f7ef6ea
Compare
2 similar comments
**/ | ||
function toRel(rel) { | ||
const stripped = rel.replace(/\/$/g, '') | ||
return stripped.match(/^((\.\.)|(\.))($|\/)/) ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we're not using the match object here, let's use regex.test(str)
instead.
errors: [ 'Useless path segments for "../files/malformed", should be "./malformed"'], | ||
}), | ||
test({ | ||
code: 'import "./test-module/"', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should test all of these cases:
foo/index.something
exists → path should be "foo"foo.something
exists → path should be "foo"- both
foo/index.something
andfoo.something
exist →foo/
andfoo
are different, and the difference is important, and this rule should not warn on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foo.something exists → path should be "foo"
I think that should be handled by https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that should handle whether there's an extension or not - i'm saying that "whether this rule can warn on a path or not" actually depends on what's on disk, because the trailing slash is meaningful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test case for catching multiple slashes, e.g. foo//something
? See #915 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea.
f7ef6ea
to
26fb213
Compare
@danny-andrews @ljharb is this ready to go? |
Ready on my end! |
I'm going to rebase this soon and merge it. |
Hype |
61bce61
to
3090e84
Compare
3 similar comments
@benmosher do you have the release process documented anywhere? If so, I'll cut a release; if not, would you mind? :-D |
add no-useless-path-segments rule Fixes #471