fix(lint): infer void return type for block-bodied arrow functions#825
Open
crowlbot wants to merge 2 commits into
Open
fix(lint): infer void return type for block-bodied arrow functions#825crowlbot wants to merge 2 commits into
crowlbot wants to merge 2 commits into
Conversation
An exported arrow function with a block body and no value-returning
return statement implicitly returns void (or Promise<void> when async),
exactly like a function expression. Previously only function
expressions had this return type inferred, so a const initialized with
an arrow like `() => {}` raised a spurious missing-return-type lint
diagnostic while the equivalent `function () {}` did not.
Share the inference via a helper and apply it to arrow bodies.
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
deno doc --lintraised a spuriousmissing-return-typediagnostic for an exportedconstinitialized with a block-bodied arrow function, while the equivalent function expression was accepted:Both forms implicitly return
void(orPromise<void>whenasync), so the lint should treat them identically. The arrow/function-expression discrepancy was reported in #823.Fix
function_to_function_defalready inferredvoid/Promise<void>for function expressions whose body has no value-returningreturn. This extracts that logic into a sharedinferred_void_return_typehelper and applies it to block-bodied arrow functions as well.Behavior after the change is consistent across both forms:
() => {}/function () {}voidasync () => {}/async function () {}Promise<void>() => { return 1; }/function () { return 1; }missing-return-type(unchanged)() => 1(expression body returning a value)missing-return-type(unchanged)Expression-bodied arrows (
() => 1) and bodies containing a value-returningreturnare deliberately left alone — inferring those would require type-checking the body.Test
Adds
tests/specs/function_const_return_type_inference.txt, covering arrow vs. function-expression parity for void inference, async inference, variable-annotation satisfaction, and the value-returning cases that still require an explicit annotation.Fixes #823