-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Eslint: Sort import
and require
statements, fix no-unused-vars
usage
#13256
Conversation
import
and requires
statements, fix no-unused-vars
usageimport
and require
statements, fix no-unused-vars
usage
This is a large list of files, but the changes are fairly straightforward. I've gone through a self review to make sure everything looks good. |
import
and require
statements, fix no-unused-vars
usageimport
and require
statements, fix no-unused-vars
usage
/cc @dvoytenko @aghassemi @choumx @lannka for comments on whether there might be any drawbacks to sorting imports. |
i feel very uncomfortable doing auto sort fixing (i'm ok with suggesting/warning but not erroring and moving around everything), importing modules have side effects and order matters (not alpha sorting etc but module loading order) |
@erwinmombay To clarify, we don't automatically move around imports when Moreover, there is support for turning off import-sorting errors in a file where there's good reason to maintain a different order. See https://github.com/ampproject/amphtml/blob/master/3p/integration.js#L63 for example. Does this alleviate your concern? |
if we didn't change the sort order of a few hundred files for the core runtime then yes. I guess we'll see in a week when this hits canary |
Honestly Im sure im just being paranoid, but we don't have enough integration tests coverage for this sort of change for me to feel confident (im sure the changes of an error are really low, like really low. only log.js really does global black magic side effect that would be obvious right away if it blew up) |
@erwinmombay You make a fair point. I merged this PR after asking around the team this morning. The responses I received were all in favor of sorting. It turns out that the original intent all this time has been to keep the imports sorted, but since it was a manual process, they didn't stay that way. Files in which the imports were explicitly grouped non-alphabetically were left out of this PR via an In addition, the research I did on module loading suggested that ES6 modules are loaded asynchronously, so declaration order doesn't strictly determine loading order. https://stackoverflow.com/questions/35551366/what-is-the-defined-execution-order-of-es6-imports I appreciate your concern though. One option is to wait and watch for the next canary. In any case, I'm ccing @cramforce in case he thinks this is a large enough concern to cause us to revert this PR. |
That's not true. Imports are async (kinda, we'll see what Node does) in browsers, but the execution order is well-defined. We're currently debating this in TC39, but my general feeling is this will not change. |
@jridgewell What I meant to say was that module load order among imports in a file is undefined, even though all modules are loaded before running the code in the file that's doing the importing. (Unless this too is incorrect?) Also, is this enough reason to revert this change, given the original intent of keeping our imports in order? |
I think as long as we don't muck with imports that look like What black magic does log.js do? I thought only polyfills like |
Yes, that's incorrect. Given: // main.js
import {a} from './a.js';
import {b} from './b.js' The files
No, probably not. I verified
It stores in a global variable. As long as the constructors are called in the main binary, everything will work correctly. |
@jridgewell Thanks for that explanation. It's worth mentioning that this PR doesn't change the order that's laid out in I'm going to leave this PR as it is, and revisit this discussion if we encounter any issues. Thanks for the comments, everyone! |
@rsimha-amp i don't think that is correct, the import order for |
also im surprised at that stackoverflow answer... |
It's an unspecified convention. TC39 didn't want to spec the module loader, so we couldn't spec the evaluation order. |
@erwinmombay Aha, I now see what you meant (https://github.com/ampproject/amphtml/blob/master/src/amp.js#L21). (My last comment was referring to the build order in Yes,
If it's imperative that Edit: Added safeguards for |
This PR makes the following
eslint
changes:import
sorting and auto-fixing viasort-imports-es6-autofix
(https://www.npmjs.com/package/eslint-plugin-sort-imports-es6-autofix)require
sorting and auto-fixing viasort-requires
(https://www.npmjs.com/package/eslint-plugin-sort-requires)no-unused-vars
(in favor of line-level usage)With this PR:
gulp lint
will flag errors for unsortedimport
andrequires
statements, andgulp lint --fix
will fix them locallygulp presubmit
will guard against unintended file-level usage ofno-unused-vars
, and suggest that the rule be used at the line-levelFixes #13253