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

Rule proposal: default-export-function-style #1089

Open
sindresorhus opened this issue Jan 30, 2021 · 15 comments
Open

Rule proposal: default-export-function-style #1089

sindresorhus opened this issue Jan 30, 2021 · 15 comments

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Jan 30, 2021

Whether to prefer a default export as a named function or a const variable with an arrow function.

It should default to function style.

It should be auto-fixable.

Fail

// "unicorn/default-export-function-style": ["error", "function"]

const foo = () => {};
export default foo;
// "unicorn/default-export-function-style": ["error", "variable"]

export default function foo() {}

Pass

// "unicorn/default-export-function-style": ["error", "function"]

export default function foo() {}
// "unicorn/default-export-function-style": ["error", "variable"]

const foo = () => {};
export default foo;

Open question. What should happen to:

export default () => {};

Should we have a separate style for that or just disallow it for both function and variable style?

@fregante
Copy link
Collaborator

fregante commented Jan 30, 2021

Open question. What should happen to:

I think that that code won’t pass an existing rule already: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-anonymous-default-export.md

Also #154

@sindresorhus
Copy link
Owner Author

I think that that code won’t pass an existing rule already: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-anonymous-default-export.md

I'm aware, but we cannot assume other rules. We need to decide how to handle that case.

@fregante
Copy link
Collaborator

Then I think it should be covered by the “function” style

@fregante
Copy link
Collaborator

fregante commented Jan 30, 2021

I don’t think it should be called "function/variable" however, they're all functions:

function foo() {}
const foo = () => {}
let foo = function () {}

Maybe these are inline:

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

And this should be pre-defined or something:

export default foo; // However the `foo` function is defined is irrelevant

Discussing this rule further:

Do you want to apply the same rule to classes? export default class {}? Objects? Where does it stop? export default 4 instead of const n = 4; export default n;?

Maybe the logic should be: if it can be inlined and named at the same time, then it should be part of this rule. As far as I know only functions and classes can be that (in the example above, 4 will lose its n name for example, so this rule can’t be applied there)

@sindresorhus
Copy link
Owner Author

Sounds good. How about statement and inline?

@sindresorhus
Copy link
Owner Author

Do you want to apply the same rule to classes? export default class {}? Objects? Where does it stop? export default 4 instead of const n = 4; export default n;?

I think it makes sense for classes, but not objects or variables.

Maybe we should just do an options-object?

{
	functions: 'statement',
	classes: 'statement',
	objects: 'inline',
	values: 'inline'
}

@fregante
Copy link
Collaborator

fregante commented Jan 31, 2021

If we agree with import/no-anonymous-default-export I don’t think it’s worth your time to implement something you’re not going to use (objects: inline, for example), but that’s up to you. I’d limit it to classes and functions and a single configuration string: ['error', 'inline'/'statement']

@sindresorhus
Copy link
Owner Author

I’d limit it to classes and functions and a single configuration string: ['error', 'inline'/'statement']

Alright. Let's try that first.

@fisker
Copy link
Collaborator

fisker commented Mar 30, 2021

Personally I don't use inline (except some really simple functions), what style do you use? @fregante @sindresorhus

@fregante
Copy link
Collaborator

I use inline exports all the time. For defaults, I still prefer inline exports if they can be named inline (functions, classes)

@sindresorhus
Copy link
Owner Author

I used to prefer non-inline so I could use arrow functions, but lately I'm preferring inline export default function foo () {}.

@fisker

This comment was marked as off-topic.

@fregante

This comment was marked as off-topic.

@fisker

This comment was marked as off-topic.

@fisker

This comment was marked as off-topic.

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

No branches or pull requests

3 participants