-
-
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
Rule to prohibit default exports #889
Comments
Why?? Default exports are the way things should operate most of the time. The link says "named imports/exports promote clarity", which I find blatantly false - a single default export + the filename is the most clear thing in my experience. Additionally, "current tooling" doesn't "differ" in how to handle default exports; handling them is pretty clear. |
It's mostly a style preference - some people feel it's clearer/easier to maintain when you can't have effectively anonymous default exports. You can basically pick whatever name you want for default exports, but it's harder to do that with named ones, and some people prefer the increased structure. For similar reasons, TSLint also has a similar rule that requires the imported and exported names to match. |
The filename is the name for a default export - they're never anonymous. So far all the reasons I've heard are covered by "have the most basic unit tests you can think of", and "provide a non-useless filename" - both of which anyone using a linter is likely already doing. I'm a -1 on having this rule; I think it's a very harmful style preference. |
I personally just like the consistency of only having one type of import used throughout. I do agree many of their claims (on the TSLint side) are invalid, though, and my reasoning differs from theirs.
(I probably should've used this from the onset...) |
JS isn't Java or Python. A JS Module isn't a box; it's either a thing or it's not (default), and then separately, it has 0-N additional named exports. Most things should be things - not "not a thing, but has an extra thing". |
Strong statement, @ljharb. I also have a tendency toward default exports but I agree with @isiahmeadows that it's a style thing. and I can imagine a team gelling around it and using a lint rule to enforce/communicate it with new hires/contributors. I'd accept a PR implementing a |
@ljharb JS module is not a thing. It is an abstraction for thinking about a project structure. It is good to export default from library stored in npm, so consumers can choose any name they like (especially if you name you lib offensive or sexist!))
But in a project with naming conventions and rules it is a dangerous freedom. What names the module exports - that names should be used, and when you refactored that names - you should refactor every name in consuming modules in your project. Good rule for application projects. TS-team right. |
@mordaha in fact, it is a thing, ES2015 adds a parsing goal to the language for "Modules", which is where No, in fact they are wrong - you should never have to refactor the names in consuming modules. The choice of consuming name is up to the consumer and it should not impact the use of that module whatsoever. If you want to restrict that freedom in your own consuming project, you can use linter rules to do so - it is simply user-hostile and incorrect for the providing module to attempt to force that preference upon you. |
@ljharb what is that wonderful rule you mentioned? Neither |
@vinogradov #936 is a PR to add a rule that can force this; but the "problem" you linked to imo is not a problem. A default export is never anonymous; the filename is its name. A default export is what a module (a file) is, a named export is what a module has. |
@ljharb If I was a library author, I would definitely use
|
@vinogradov in that case, There is not yet a rule for #325, but that would address that use case. |
@ljharb selaux/eslint-plugin-filenames addresses that case already. Please address "import-default" case instead. It would be great to have a rule saying: "rename OldUglyNameForgetToRefactor to myBeautifulModuleName" when you do this:
If it is not possible please consider accepting the PR #936 |
@vinogradov I'm already a -1 on #936 (and this issue's rule), but i'm not blocking. Totally separately, and in parallel, we absolutely should add a rule that enforces the identifier you use when importing a default, and ensuring it matches the filename. Also, I think |
@ljharb Note that I'm a heavy -1 on making it a recommended plugin. My only request is its existence.
Definitely also a good idea, since I know of several that do it already. TypeScript itself independently enforces this via a compiler option. |
Note that my main point is that |
@isiahmeadows since this appears not to be happening, would you mind publishing your good work as a separate plugin? |
@isiahmeadows good stuff, I would love to have this rule available! ❤️ |
TSLint has a similar rule, where it prohibits default exports. Basically this (second case generating a warning):
The text was updated successfully, but these errors were encountered: