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

Enable import/no-anonymous-default-export and import/no-named-default #472

Merged
merged 2 commits into from Jun 3, 2020
Merged

Enable import/no-anonymous-default-export and import/no-named-default #472

merged 2 commits into from Jun 3, 2020

Conversation

fregante
Copy link
Member

@fregante fregante commented May 8, 2020

https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-anonymous-default-export.md
Reports:

export default () => {}
export default class {}
export default function () {}

Function names help automatic imports by IDEs


https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-named-default.md

Reports:

// message: Using exported name 'bar' as identifier for default export.
import { default as foo } from './foo.js';
import { default as foo, bar } from './foo.js';

@sindresorhus
Copy link
Member

Function names help automatic imports by IDEs

You mean autocomplete? Why can't the IDEs just take the name from the filename?

@fregante
Copy link
Member Author

fregante commented May 8, 2020

You mean autocomplete?

Yeah, type stripW| and VS Code adds:

import stripWhitespace from './strip-whitespace';

stripWhitespace|

Why can't the IDEs just take the name from the filename?

Don’t ask me, I don’t make IDEs 😅

@sindresorhus
Copy link
Member

I know. But I'm saying you're trying to fix it in the wrong place. This should instead be an issue on the IDE you use. I don't like having to have needlessly more verbose code just to please an IDE.

@fregante
Copy link
Member Author

fregante commented May 8, 2020

It's not just an IDE issue, your functions will also appear as anonymous when debugging them (named default or module.exports)

@sindresorhus
Copy link
Member

sindresorhus commented May 8, 2020

So the debugger needs to be improved too then. The filename is available to the debugger. Which debugger are we talking about? Chrome DevTools?

(I’m not saying I won’t accept this. Just something to consider.)

@fregante
Copy link
Member Author

fregante commented May 8, 2020

So the debugger needs to be improved too then

The debugger shows the real function name which as per spec is default or module.exports, debuggers can't change that.

Which debugger are we talking about?

I think every debugger tries to show the function name in the stack, but yes users will see the file name as at default (./node_modules/throw-error/index.js) or at module.require (./throw-error.js)

With sourcemap enabled even bundlers will show the correct filename in this case ✅ Rollup is the smartest one and the function appears as const x = () => {throw Error()} so it will show the user’s chosen name.


I think however naming an important function is worth it (so important in fact that it deserves its own file/module).

For example this can also serve as a capitalization suggestion, no IDE will ever suggest importing the url-search-params module as URLSearchParams. If the module has export default class URLSearchParams this name is exactly what will be used, without extra guesses.

@sindresorhus sindresorhus merged commit 1240dd2 into xojs:master Jun 3, 2020
@sindresorhus
Copy link
Member

sindresorhus commented Jun 3, 2020

I'm still lukewarm about it. It does help debugging, but it also makes exports less elegant. But I'm willing to try it out.

@fregante fregante deleted the patch-1 branch June 3, 2020 09:52
@fregante
Copy link
Member Author

fregante commented Jun 3, 2020

Some will complain that this lengthens exports that aren’t functions or classes I’m sure, since export default 42 becomes const life = 42; export default life

If that’s too annoying , it looks like the rule can be configured, but I guess the same principle applies in that case too: the export now has a name

@jimmywarting
Copy link
Contributor

Didn't like this rule... too opinionated. IDE should become smarter instead.

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

Successfully merging this pull request may close these issues.

None yet

3 participants