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

Filter out default export in an ExportAllDeclaration #331

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ module.exports = function (context) {
return
}
let any = false
remoteExports.forEach((v, name) => (any = true) && addNamed(name, node))
remoteExports.forEach((v, name) => (
(any = true) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know it was already in the original code (so this is more like a question to @benmosher), but wouldn't it be simpler & less odd (an assignation in a condition? oô), to do const any = remoteExports.length > 0?

I would actually split it up somewhat like this

const namedExports = remoteExports.filter((v, name) => name !== 'default');
const anyNamed = namedExports.length > 0;
namedExports.forEach((v, name) => addNamed(name, node));

Copy link
Author

Choose a reason for hiding this comment

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

@jfmengels wouldn't quite work since any is used for the No named exports found in module check below.

This would work though:

const anyNamed = remoteExports.length > 0;
const namedExports = remoteExports.filter((v, name) => name !== 'default');
namedExports.forEach((v, name) => addNamed(name, node));

Happy to change if you think it makes it more readable (I admit I was confused about the assignment in the condition)

Copy link
Member

Choose a reason for hiding this comment

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

FWIW, remoteExports is more of a Map than an Array, so it has forEach but no filter. It has a size, but this size includes the default name.

I agree that the (any = true) assignment expression is a little laconic, but it gets the job done.

Copy link
Member

Choose a reason for hiding this comment

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

This is what I ended up with, here: https://github.com/benmosher/eslint-plugin-import/pull/332/files#diff-71778249fbd501cbc801fa0ee1cac9a2R53

default shouldn't count, so it does trigger the no exported names report if you export * from 'foo' where foo has only a default export.

Copy link
Author

Choose a reason for hiding this comment

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

That makes sense! I'll close this PR then.

// Ignore default export from an export all declaration
// Ref: https://github.com/benmosher/eslint-plugin-import/issues/328
name !== 'default' &&
addNamed(name, node)
))

if (!any) {
context.report(node.source,
Expand Down
5 changes: 1 addition & 4 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ruleTester.run('export', rule, {
test({ code: 'export { bar }; export * from "./export-all"' }),
test({ code: 'export * from "./export-all"' }),
test({ code: 'export * from "./does-not-exist"' }),
test({ code: 'export default foo; export * from "./default-export"' }),
],

invalid: [
Expand All @@ -29,10 +30,6 @@ ruleTester.run('export', rule, {
code: 'export default foo; export default bar',
errors: ['Multiple default exports.', 'Multiple default exports.'],
}),
test({
code: 'export default foo; export * from "./default-export"',
errors: ['Multiple default exports.', 'Multiple default exports.'],
}),
test({
code: 'export default function foo() {}; ' +
'export default function bar() {}',
Expand Down