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

[New] extensions: accept both a string, and an object to override it. #555

Merged
merged 2 commits into from Sep 20, 2016

Conversation

ljharb
Copy link
Member

@ljharb ljharb commented Sep 12, 2016

Fixes #390.

@coveralls
Copy link

coveralls commented Sep 12, 2016

Coverage Status

Coverage increased (+0.003%) to 97.797% when pulling 028ad4c on ljharb:extensions_options into bd99bdf on benmosher:master.

@benmosher benmosher mentioned this pull request Sep 12, 2016
4 tasks
@benmosher benmosher added this to the 1.16.0 milestone Sep 13, 2016
@ljharb
Copy link
Member Author

ljharb commented Sep 14, 2016

@benmosher Now that 1.15 is out, is this ready to be merged? I'd really like to resolve airbnb/javascript#978 :-)

@benmosher
Copy link
Member

will review ASAP, likely tomorrow morning 😅

Copy link
Member

@benmosher benmosher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: though I'd like @jfmengels to take a look too.

LGTM though, especially the docs. 😄

Copy link
Collaborator

@jfmengels jfmengels left a 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.

@benmosher
Copy link
Member

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.
Copy link
Collaborator

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' ] } },
}),

Copy link
Collaborator

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.

@jfmengels
Copy link
Collaborator

That said, this looks pretty good and I like this way of configuring the rule 👍

@ljharb
Copy link
Member Author

ljharb commented Sep 16, 2016

@jfmengels thanks for the feedback; it helped me find some bugs. Updated!

@coveralls
Copy link

coveralls commented Sep 16, 2016

Coverage Status

Coverage increased (+0.005%) to 97.841% when pulling 69e2534 on ljharb:extensions_options into e087ec4 on benmosher:master.

Copy link
Collaborator

@jfmengels jfmengels left a 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.
Copy link
Collaborator

@jfmengels jfmengels Sep 16, 2016

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

Copy link
Member Author

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)) {
Copy link
Collaborator

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)

Copy link
Member Author

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, can-do.

Copy link
Collaborator

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

Copy link
Member Author

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.

Copy link
Member Author

@ljharb ljharb Sep 16, 2016

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?

Copy link
Collaborator

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.

Copy link
Member Author

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.

Copy link
Member Author

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

Copy link
Member

@benmosher benmosher Sep 17, 2016

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)

@ljharb ljharb force-pushed the extensions_options branch 2 times, most recently from a0bb733 to bea0576 Compare September 16, 2016 21:15
@jfmengels
Copy link
Collaborator

LGTM! (if the tests pass obviously)
I'm letting @benmosher take a last look

@ljharb
Copy link
Member Author

ljharb commented Sep 16, 2016

hm, now I'm getting failures that are unrelated to my branch/this rule, that I can't reproduce locally.

@ljharb
Copy link
Member Author

ljharb commented Sep 16, 2016

@benmosher any idea why the tests are getting "duplicate export" errors for a rule that I'm not changing?

@ljharb
Copy link
Member Author

ljharb commented Sep 16, 2016

@benmosher actually i just reprod it locally on master when i rm -rf node_modules && npm install.

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.

@jfmengels
Copy link
Collaborator

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
Copy link
Member

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 or hasOwnProperty
  • 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ready to :shipit:, other than this, though. 😄 Thanks to both of you for working through @jfmengels' feedback, too!

Copy link
Member Author

@ljharb ljharb Sep 17, 2016

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.

Copy link
Member Author

@ljharb ljharb Sep 18, 2016

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)

Copy link
Member

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.

Copy link
Member

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. 😁

@coveralls
Copy link

coveralls commented Sep 18, 2016

Coverage Status

Coverage increased (+0.008%) to 97.844% when pulling f507ff3 on ljharb:extensions_options into 12bf6da on benmosher:master.

@benmosher benmosher merged commit f507ff3 into import-js:master Sep 20, 2016
@jfmengels
Copy link
Collaborator

Yay! Thanks @ljharb! 🎉

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

Successfully merging this pull request may close these issues.

None yet

4 participants