-
-
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
[New] extensions
: accept both a string, and an object to override it.
#555
Conversation
@benmosher Now that 1.15 is out, is this ready to be merged? I'd really like to resolve airbnb/javascript#978 :-) |
will review ASAP, likely tomorrow morning 😅 |
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.
though I'd like @jfmengels to take a look too.
LGTM though, especially the docs. 😄
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.
I would really like to see an example configuration in the examples section, just to make things very clear.
Ah, fair point @jfmengels. Could probably use the added test case as a starting point. |
@@ -6,10 +6,12 @@ In order to provide a consistent use of file extensions across your code base, t | |||
|
|||
## Rule Details | |||
|
|||
This rule has one option which could be either a string or an object. If it is `"never"` (the default value) the rule forbids the use for any extension. If `"always"` then the rule enforces the use of extensions for all import statements. | |||
This rule has one option which could be either a string, an object, or both. If it is `"never"` (the default value) the rule forbids the use for any extension. If `"always"` then the rule enforces the use of extensions for all import statements. |
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.
It's not one option
anymore, is it? It's either one, which is either a string or an object, or two, with first a string then an object. Or am I misunderstanding something?
options: [ 'always', { js: 'never', jsx: 'never' } ], | ||
settings: { 'import/resolve': { 'extensions': [ '.js', '.jsx', '.json', '.hbs' ] } }, | ||
}), | ||
|
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.
Maybe add a test case with never, for completeness' sake.
I'd also like to see both valid and invalid test cases with this change, if it's not too much to ask.
That said, this looks pretty good and I like this way of configuring the rule 👍 |
028ad4c
to
69e2534
Compare
@jfmengels thanks for the feedback; it helped me find some bugs. Updated! |
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.
I have a few more comments :s
@@ -6,10 +6,12 @@ In order to provide a consistent use of file extensions across your code base, t | |||
|
|||
## Rule Details | |||
|
|||
This rule has one option which could be either a string or an object. If it is `"never"` (the default value) the rule forbids the use for any extension. If `"always"` then the rule enforces the use of extensions for all import statements. | |||
This rule either takes one string option, one object option, or a string and an object option. If it is the string `"never"` (the default value) the rule forbids the use for any extension. If the string `"always"` then the rule enforces the use of extensions for all import statements. |
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.
If it is the string "never"
(the default value)
-->
If it is the string
"never"` (the default value), then
(for consistency)
If the string "always"
then the rule
-->
If it is the string "always"
then the rule
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.
alrighty
@@ -37,15 +39,15 @@ module.exports = function (context) { | |||
const extension = path.extname(resolvedPath || importPath).substring(1) | |||
|
|||
if (!extension || !endsWith(importPath, extension)) { | |||
if (isUseOfExtensionEnforced(extension)) { | |||
if (isUseOfExtensionRequired(extension) && !isUseOfExtensionForbidden(extension)) { |
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.
You could compute the value for the extension modifiers[extension] || defaultConfig
once, instead of recomputing it several times (nitpick)
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.
Do you just mean, cache it per-extension?
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.
Sure, can-do.
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.
Hmm, not cache per-se, though I don't see a reason why not. I was thinking of computing the value of it before line 41, as you will use the result later
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.
oh, that's true. let me do that.
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.
This change may not even be worth it overall tho - the tests are failing in Travis because the object is not extensible. Which do you prefer - remove the last commit; rework it to pass with has
; or rework it to pass without has
?
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.
What are the reasons that externals.hasOwnProperty(key)
could break? Is that just when externals is nil?
If you feel like it's better to have it, keep it, I'm not that against it, just didn't see the value if it was just to check if externals[key]
exists.
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.
so, the reason bracket access and in
won't work is because someone could define an extension named "toString" or "hasOwnProperty" or similar. The reason to avoid Object.prototype.hasOwnProperty.call
is because that relies on both Function#call
and Object#hasOwnProperty
being undisturbed in the environment. Using has
is robust against both.
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.
k, tests should be passing now
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.
(deleted and moved comment to line in question as a review)
a0bb733
to
bea0576
Compare
LGTM! (if the tests pass obviously) |
hm, now I'm getting failures that are unrelated to my branch/this rule, that I can't reproduce locally. |
bea0576
to
80cd6ac
Compare
@benmosher any idea why the tests are getting "duplicate export" errors for a rule that I'm not changing? |
@benmosher actually i just reprod it locally on master when i It might be an espree version that updated to use Acorn v4, so that it's now erroring out on syntax errors for duplicates in places where it wasn't before? If so, the solution is probably to delete or modify the test cases. |
I agree it sounds like an Espree error. I made a fix in #565 |
@@ -265,7 +266,7 @@ function findExternal(source, externals, context) { | |||
|
|||
// else, vanilla object | |||
for (var key in externals) { | |||
if (!externals.hasOwnProperty(key)) continue | |||
if (!has(externals, key)) continue |
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.
(restarted as an explicit line review, mostly out of curiosity + to gain familiarity with the new feature)
So, I'm not anti-dependencies in general, but I don't understand why
- someone would have file extensions of
toString
orhasOwnProperty
- someone would be surprised if things are broken when they monkeypatch JS core functions
Am I correct that these are the cases where has
matters over just a plain ol' hasOwnProperty
call or in
operator? Because if that's true, I'd still like to revert to hasOwnProperty
or really, just extension in modifiers
because these are well-understood JS core idioms, and in this specific case, not practically distinguishable.
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.
Looks ready to , other than this, though. 😄 Thanks to both of you for working through @jfmengels' feedback, too!
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.
The first one, I also don't know why someone would do it, but given that it's a possibility, that would be a really silly reason for this package to break, don't you think?
As to the second, for a tool like an eslint plugin, that's a stronger case, since it won't be running in browser environments, or environments with unknown third-party code. However, the idea that it's acceptable for one's code to be broken because someone else did something bad, foolish, or simply unexpected mystifies me - as long as my code runs in a pristine environment, nothing that happens afterwards can break it. This seems like a pretty important property to me.
I'll of course remove the dependency if you insist, but I think this is still strictly better because you don't have to think about all these edge cases. I do not think in
or hasOwnProperty
are well-understood idioms, which is why this problem keeps coming up in libs all over the place, and why has
exists.
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.
(@benmosher let me know your final call, and i'll rebase out the last commit if needed)
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.
Fair points. It's not a huge deal either way, or it wouldn't be so easy to have this back and forth. 😄
I hear what you're saying, and maybe one day all our key checks turn into has
calls, but I don't think today is that day.
I didn't realize the has
part was a separate commit -- I'll just go ahead and merge the first one.
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.
Agh, nevermind, it's bundled with some other stuff. I'll just keep it for now. 😁
80cd6ac
to
f507ff3
Compare
Yay! Thanks @ljharb! 🎉 |
Fixes #390.