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

Add prefer-export-from rule #1453

Merged
merged 24 commits into from
Nov 1, 2021
Merged

Conversation

fisker
Copy link
Collaborator

@fisker fisker commented Jul 27, 2021

Fixes #334

@fisker fisker marked this pull request as ready for review July 28, 2021 03:27
@sindresorhus
Copy link
Owner

With this case:

import foo from 'foo';

unicorn(foo);

export default foo;

I would personally prefer this instead (re-exporting, even if it was imported):

import foo from 'foo';

unicorn(foo);

export default from 'foo';

As it's IMHO more readable and also makes a guarantee that the export is just re-exported and not modified on the way.

Thoughts?

@fisker
Copy link
Collaborator Author

fisker commented Jul 28, 2021

Personally, I only use export...from if it's not used(at least not modified, but we can't static analysis it), imaging this

import foo from 'foo';

foo.bar = 1;

export default from 'foo';

Still prefer export...from ?

@fisker fisker closed this Jul 28, 2021
@fisker fisker reopened this Jul 28, 2021
@sindresorhus
Copy link
Owner

Still prefer export...from ?

Yes. It's still more readable. You can immediately see what is exported from where instead of having to read and understand all the code in the file.

@fisker fisker marked this pull request as draft July 30, 2021 03:03
docs/rules/prefer-export-from.md Outdated Show resolved Hide resolved
rules/prefer-export-from.js Outdated Show resolved Hide resolved
Comment on lines +270 to +287
// Strange case
// TODO: confirm how should this work
test.snapshot({
valid: [
outdent`
import * as namespace from 'foo';
export default namespace;
export {namespace};
`,
],
invalid: [
outdent`
import * as namespace from 'foo';
export {namespace};
export default namespace;
`,
],
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These seem strange, I don't have time to confirm how should them work, but I guess we are fine to ignore it for now.

Copy link
Owner

Choose a reason for hiding this comment

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

Seems like a bug in ESLint. Both of those should be valid.

@fisker
Copy link
Collaborator Author

fisker commented Nov 1, 2021

This PR has been a while, I forgot if I left something to do, but seems fine to merge.

@fisker fisker marked this pull request as ready for review November 1, 2021 13:19
@sindresorhus sindresorhus merged commit ff43745 into sindresorhus:main Nov 1, 2021
@sindresorhus
Copy link
Owner

Thanks for finishing this 👍

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.

Rule proposal: prefer-export-from
2 participants