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
Conversation
There was a problem hiding this 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.
There was a problem hiding this 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.
@eslint/eslint-team Can we get a few more eyes on this? Looks like we need a champion and one more 👍 to accept this. |
Makes sense to me, too. I'll champion this. If this gets accepted, could we also fix the message for Also,
No, but I don't think there is something in the docs that should be updated for this particular change.
Agreed to change to |
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>
* Made tests match proposed new message.
0da1aab
to
0cdf1a8
Compare
* 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
I think this addresses all review points:
@kaicataldo Could you re-review and let me know if all changes have been taken care of? |
There was a problem hiding this 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).
lib/rules/array-callback-return.js
Outdated
@@ -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) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this 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
lib/rules/array-callback-return.js
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
@eslint/eslint-team We just need one more 👍 to accept this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There was a problem hiding this 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!
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}}.
andMethod .{{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 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.