Navigation Menu

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

Breaking: check unnamed default export in func-names (fixes #12194) #12195

Merged
merged 4 commits into from Feb 17, 2020
Merged

Breaking: check unnamed default export in func-names (fixes #12194) #12195

merged 4 commits into from Feb 17, 2020

Conversation

golopot
Copy link
Contributor

@golopot golopot commented Aug 31, 2019

fixes #12194

What is the purpose of this pull request? (put an "X" next to item)
[X] Bug fix (template)

What changes did you make? (Give an overview)
Start report code like export default function() {} when always or as-needed.

Is there anything you'd like reviewers to focus on?
Yes.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Aug 31, 2019
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

Looks pretty good. Just left one question. 😄

parserOptions: { sourceType: "module", ecmaVersion: 6 }
},
{
code: "export default function foo() {}",
Copy link
Member

Choose a reason for hiding this comment

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

How is this valid? The option is "never" but the function has a name.

Copy link
Member

Choose a reason for hiding this comment

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

That would require more checks, foo is here a declaration and can be used from within the module. And even if that's not the case, I believe this would break some builds

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The reasoning is that, under option never, function f() is allowed, export function f() is allowed, so likewise export default function f() should be allowed.

I understand in some perspective this should be reported, so this case is controvesial. Not reporting in this case would make both sides happy.

@mdjermanovic
Copy link
Member

Just to note that this PR at the moment also changes how the rule works with function expressions when the option is as-needed.

Also, all 3 changes would be 'false negatives' if this is seen as a bug, and the message should certainly start with Update:

@golopot golopot changed the title Fix: fix false positive for func-names with export default (fixes #12194) Update: fix func-names for unnamed export default (fixes #12194) Sep 7, 2019
@platinumazure platinumazure added bug ESLint is working incorrectly enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Sep 29, 2019
@mdjermanovic mdjermanovic added the breaking This change is backwards-incompatible label Oct 17, 2019
@kaicataldo
Copy link
Member

How do we want to proceed here? It looks like this was marked as a breaking change rather than a semver-minor bug fix, but it's not entirely clear why.

@kaicataldo kaicataldo added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint labels Dec 23, 2019
@kaicataldo
Copy link
Member

@mdjermanovic Can we merge this?

@mdjermanovic
Copy link
Member

An overview of the new behavior after this PR:

/*eslint func-names: ["error", "always"]*/

export default function () {} // error (*)

export default function foo() {} // ok

export default (function (){}); // error

export default (function foo(){}); // ok
/*eslint func-names: ["error", "as-needed"]*/

export default function () {} // error (*)

export default function foo() {} // ok

export default (function (){}); // error (*)

export default (function foo(){}); // ok
/*eslint func-names: ["error", "never"]*/

export default function () {} // ok

export default function foo () {} // ok!

export default (function (){}); // ok

export default (function foo(){}); // error

(*) marks changes in behavior made in this PR.

There are three (*), caused by these two different changes in logic:

  • export default function () {} will be treated as function expressions. It was ignored before since it's a function declaration.
  • Anonymous functions in export default will be treated as functions that don't get automatically assigned name, thus they'll be reported when the option is as-needed. They were valid before since they get assigned the "default" name (intentionally valid, there is an explicit condition for that and an example in the original issue #7235).

Also, export default function foo () {} with "never" is an intentional exception to the new logic. It might be consistent to require removing the name, but users probably wouldn't want that.

Questions:

  • Do we have consensus on both changes.
  • Should we update the documentation (aside from the new examples). At the moment, it's explicitly about function expressions only. Also, export default does assign a name.

@btmills btmills added this to Needs discussion in v7.0.0 Feb 12, 2020
@nzakas nzakas moved this from Needs discussion to Accepted, ready to implement in v7.0.0 Feb 13, 2020
@mysticatea mysticatea moved this from Accepted, ready to implement to Implemented, pending review in v7.0.0 Feb 13, 2020
@btmills
Copy link
Member

btmills commented Feb 13, 2020

In today's TSC meeting, we added this to the v7.0.0 release!

@golopot golopot changed the title Update: fix func-names for unnamed export default (fixes #12194) Breaking: check unnamed default export in func-names (fixes #12194) Feb 16, 2020
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

The code LGTM!

Should we add a short note that as-needed requires names for functions in export default, somewhere in the documentation (maybe in the ### as-needed section)?

The description for the option is now inaccurate:

"as-needed" requires function expressions to have a name, if the name cannot be assigned automatically in an ES6 environment

so it might be good to clarify the behavior, as this in an exception?

Maybe also a short note that always and as-needed options also apply to function declarations in export default?

docs/rules/func-names.md Outdated Show resolved Hide resolved
Copy link
Member

@mdjermanovic mdjermanovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@kaicataldo kaicataldo merged commit 6423e11 into eslint:master Feb 17, 2020
v7.0.0 automation moved this from Implemented, pending review to Done Feb 17, 2020
@kaicataldo
Copy link
Member

Thanks for contributing to ESLint!

@golopot golopot deleted the fix-func-names-export-default branch March 1, 2020 15:53
montmanu pushed a commit to montmanu/eslint that referenced this pull request Mar 4, 2020
) (eslint#12195)

* Breaking: check unnamed default export in func-names (fixes eslint#12194)

* docs: add that default export functions are checked

* Revert "docs: add that default export functions are checked"

This reverts commit 036ce53.

* docs: add a note that export default functions needs name
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 17, 2020
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion breaking This change is backwards-incompatible bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
No open projects
v7.0.0
  
Done
Development

Successfully merging this pull request may close these issues.

func-names does not report default exported function
5 participants