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

prefer-default-export doesn't understand destructoring #342

Closed
scottnonnenberg opened this issue May 14, 2016 · 8 comments · Fixed by #343
Closed

prefer-default-export doesn't understand destructoring #342

scottnonnenberg opened this issue May 14, 2016 · 8 comments · Fixed by #343

Comments

@scottnonnenberg
Copy link
Contributor

The prefer-default-export rule fires when I do this:

// typography.js
export const { rhythm, TypographyStyle } = typography;

But I get import-named complaining that 'rhythm' cannot be found when I change it back to:

// typography.js
export default typography;

And continue to reference it like this:

// component.js
import { rhythm } from 'utils/typography';

I guess I prefer the first option, but that would require that prefer-default-export understand destructuring.

On the other hand, do we really want to be that restrictive on the import destructuring check? Isn't it reasonable to destructure a default export?

@benmosher
Copy link
Member

The prefer-default-export issue is a bug.

The named rule behavior is correct, though. The default export is just a named export that is named "default" and has special syntax. You may be able to do that with Babel 5 because of the way it encodes a single default export into CommonJS, but it's technically invalid. Named exports must be explicitly declared, as in your destructuring example.

Will make note of this and address in prefer-default-export. Thanks!

@scottnonnenberg
Copy link
Contributor Author

Good to know about the correct syntax. I am using Babel 6 however, so it continues to allow the incorrect usage even in the current release.

By the way, many thanks for this package! It's so useful to know that your imports are correct before running the code. And I thought I was the only one that wanted my imports sorted like that! :0)

@gavriguy
Copy link
Contributor

Just saw this issue - I'll create a PR to fix it.
BTW If you have any issue regarding prefer-default-export I'll be happy try and fix it - please mention me in the issue if you can so I can respond more quickly.

gavriguy added a commit to gavriguy/eslint-plugin-import that referenced this issue May 19, 2016
benmosher added a commit that referenced this issue May 19, 2016
@benmosher benmosher added this to the next milestone May 19, 2016
@benmosher benmosher modified the milestones: v1.9.x, next Jun 16, 2016
@sk1e
Copy link

sk1e commented Jan 1, 2017

@benmosher @gavriguy Hi, there is also destructuring syntax for arrays:

export const [one, two] = [1, 2];

and for nested things:

export const [one, { two, three: [three] }] = [1, { two: 2, three: [3] }];

Plugin still complaining about prefer-default-export for these cases.

@ljharb
Copy link
Member

ljharb commented Jan 1, 2017

Shouldn't it? Those are named exports, and prefer-default-export wants a default export.

@sk1e
Copy link

sk1e commented Jan 1, 2017

I guess it shouldn't since there are multiple exports and rule states about "When there is only a single export from a module, prefer using default export over named export."

@ljharb
Copy link
Member

ljharb commented Jan 1, 2017

gotcha. can you file a new issue for that?

@sk1e
Copy link

sk1e commented Jan 1, 2017

ok

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