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

Update: Improve array-callback-return report message #13395

Merged
merged 6 commits into from Jul 3, 2020

Conversation

mrflip
Copy link
Contributor

@mrflip mrflip commented Jun 5, 2020

What changes did you make? (Give an overview)

The array-callback-return rule should explain that a return value is required because the surrounding method requires it. This makes it clear that eg. .filter() expects the passed function to return something, rather than a general expectation of that function.

The error messages for expectedAtEnd/Inside now read Method .{{arrayMethodName}}() expected a value to be returned at the end of {{name}}. and Method .{{arrayMethodName}}() expected a return value from {{name}}.

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[X] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

  • What rule do you want to change?: array-callback-return
  • Does this change cause the rule to produce more or fewer warnings?: same
  • How will the change be implemented? (New option, new default behavior, etc.)?: new error messages
  • Please provide some example code that this change will affect:
    Object.entries(extraParams.values).map(([op, argStrs]) => {
      ops[op].extraParams = Object.fromEntries(argStrs.map((pair) => pair.split(/: */)))
    })
  • What does the rule currently do for this code?: It says "Expected to return a value in arrow function". What? Who said arrow functions have to return? I do that all the time and there's no error.

  • What will the rule do after it's changed?: It will say "Method .map() expected a return value from arrow function". Aha! I need to change .map() to .forEach().

  • I Read the pull request guide (https://eslint.org/docs/developer-guide/contributing/pull-requests)

  • I modified the tests for this change

  • I think it automatically updates documentation for this change?

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

If I were writing the test I would say 'expects' and not 'expected'; is the past tense a style guide point or an accident of history? nbd.

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Jun 5, 2020
@kaicataldo kaicataldo added 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 Jun 5, 2020
Copy link
Member

@kaicataldo kaicataldo left a comment

Choose a reason for hiding this comment

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

I support this change - one small request to change to the error message for clarity.

lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
lib/rules/array-callback-return.js Outdated Show resolved Hide resolved
Copy link
Contributor Author

@mrflip mrflip left a comment

Choose a reason for hiding this comment

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

Great! Fixed the tests to pass with the new message.

@mrflip mrflip requested a review from kaicataldo June 16, 2020 03:59
@kaicataldo
Copy link
Member

@eslint/eslint-team Can we get a few more eyes on this? Looks like we need a champion and one more 👍 to accept this.

@mdjermanovic mdjermanovic self-assigned this Jun 25, 2020
@mdjermanovic
Copy link
Member

Makes sense to me, too. I'll champion this.

If this gets accepted, could we also fix the message for .forEach?

Also, Array.from isn't Array.prototype.from.

I think it automatically updates documentation for this change?

No, but I don't think there is something in the docs that should be updated for this particular change.

If I were writing the test I would say 'expects' and not 'expected'; is the past tense a style guide point or an accident of history?

Agreed to change to "expects".

mrflip and others added 4 commits June 27, 2020 19:35
The array-callback-return rule should explain that a return value is required because the surrounding method requires it. This makes it clear that eg. .filter() expects the passed function to return something, rather than a general expectation of that function.

The error messages for expectedAtEnd/Inside now read 'Method .{{arrayMethodName}}() expected a value to be returned at the end of {{name}}.' and 'Method .{{arrayMethodName}}() expected a return value from {{name}}.'
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
Co-authored-by: Kai Cataldo <kai@kaicataldo.com>
* forEach has a message, and describes why no return value is expected.
* present tense ('Array.from expects no' vs 'Array.from expected no')
* Added explicit check of the forEach message
@mrflip
Copy link
Contributor Author

mrflip commented Jun 28, 2020

I think this addresses all review points:

  • Rebased to master
  • forEach has a message, and describes why no return value is expected.
  • Made the messages present tense ('Array.from expects no…' vs 'Array.from expected no…')
  • Attributed methods correctly: Array.from, Array.prototype.(everything else)
  • Added explicit check of the forEach message

@kaicataldo Could you re-review and let me know if all changes have been taken care of?

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.

Looks good in general, there's one thing that should be fixed if this change gets accepted (it still needs one more 👍 from a team member).

@@ -273,7 +273,8 @@ module.exports = {
node,
messageId,
data: {
name: lodash.upperFirst(astUtils.getFunctionNameWithKind(funcInfo.node))
name: astUtils.getFunctionNameWithKind(funcInfo.node),
arrayMethodName: "Array.prototype.".concat(funcInfo.arrayMethodName)
Copy link
Member

Choose a reason for hiding this comment

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

This path can also report Array.from, e.g.:

/* eslint array-callback-return: error */

// it shouldn't be "Array.prototype.from() expects a return value from arrow function"
Array.from(foo, () => { return; });

Maybe we could make a function that gets funcInfo.arrayMethodName and returns its "full name", and then use that function here and from the other context.report?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, those lines felt fishy and a function is the right answer. I went ahead and included all class methods (of, isArray, from) in case the function was ever lifted somewhere else. Pushed a commit with this change.

Copy link
Contributor Author

@mrflip mrflip left a comment

Choose a reason for hiding this comment

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

Pushed a commit to clean up the fully-qualified method naming

@@ -273,7 +273,8 @@ module.exports = {
node,
messageId,
data: {
name: lodash.upperFirst(astUtils.getFunctionNameWithKind(funcInfo.node))
name: astUtils.getFunctionNameWithKind(funcInfo.node),
arrayMethodName: "Array.prototype.".concat(funcInfo.arrayMethodName)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, those lines felt fishy and a function is the right answer. I went ahead and included all class methods (of, isArray, from) in case the function was ever lifted somewhere else. Pushed a commit with this change.

@kaicataldo
Copy link
Member

@eslint/eslint-team We just need one more 👍 to accept this.

@mdjermanovic mdjermanovic changed the title Update: Improve array-callback-return report (explains why) Update: Improve array-callback-return report message Jun 30, 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.

LGTM, thanks!

Copy link
Member

@btmills btmills left a comment

Choose a reason for hiding this comment

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

Added my 👍 and marking as accepted. LGTM, thanks for this nice improvement @mrflip!

@btmills btmills 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 labels Jun 30, 2020
@kaicataldo kaicataldo merged commit ff5317e into eslint:master Jul 3, 2020
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Dec 31, 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 Dec 31, 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 enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants