-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Complain when a non-void function lacks a return expresson. #147
Conversation
|
||
declare class A { | ||
get length() : number; | ||
~ | ||
!!! '{' expected. | ||
~~~~~~ | ||
!!! A function whose declared type is neither 'void' nor 'any' must have a 'return' expression or consist of a single 'throw' statement. |
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 seems like an unfortunate consequence of #139.
Baselines 🆗 |
if (!isInAmbientContext(node) && node.body && !(checkGetterContainsSingleThrowStatement(node) || checkGetterReturnsValue(node))) { | ||
error(node.name, Diagnostics.Getters_must_return_a_value); | ||
if (!isInAmbientContext(node) && node.body && !(bodyContainsReturnExpressions(<Block>node.body) || bodyContainsSingleThrowStatement(<Block>node.body))) { | ||
error(node.name, Diagnostics.A_get_accessor_must_return_a_value_or_consist_of_a_single_throw_statement); |
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.
Did we used to support a getter with a single throw statement?
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.
Yes; however, we erroneously permitted the following in both the old compiler and the new one prior to this commit.
class C {
get foo() {
return;
}
}
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.
Can you add an explanation why this is error? Per TypeScript spec (and ECMA 262) return statement that lacks expression returns the value undefined
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.
Yes, This is crazy world of JavaScript. Also restricting this is a potential breaking change. I'd say that these issues should be traced by some sort of linter tool
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.
Well yes, but doesn't the following also implicitly return undefined
?
class C {
get foo() {
}
}
Additionally, the following should be permissible by the same reasoning, meaning that the entire check should be removed.
function f(): string {
return;
}
I think that the rules for the check should be the same between get accessors and functions with type annotations. If not, we may need to open this up to discussion, because then maybe we don't need the check.
Edit: Sorry @vladima, didn't see your last response, and I decided to remove my answer and think the matter over a bit more before posting 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.
Adding 'Breaking Change' label to the original issue then
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 have opened up #162 to prevent this issue from blocking the fix.
I'll simply amend this pull request so that it does not cause a breaking change.
Looks great! |
@@ -1,4 +1,6 @@ | |||
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement4.ts (1 errors) ==== | |||
==== tests/cases/conformance/parser/ecmascript5/Statements/ReturnStatements/parserReturnStatement4.ts (2 errors) ==== | |||
var v = { get foo() { return } }; |
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 is legal JavaScript code and should not be an error. Return statement without expression returns undefined
function checkAndAggregateReturnExpressionTypes(body: Block, contextualType?: Type, contextualMapper?: TypeMapper): Type[] { | ||
var aggregatedTypes: Type[] = []; | ||
|
||
visitReturnStatements(body, (returnStatement) => { |
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.
no need for parens around the parameter.
In effect this fixes #62. Also - Changes the error message for get accessors lacking return expressions. - Actually checks for return expressions instead of return statements for get-accessors. - Removes fancy quotes. - Corrects errors in the compiler caught by the new check. - Simplified `checkAndAggregateReturnTypes` by extracting it out to `visitReturnStatements`.
👍 |
Complain when a non-void function lacks a return expresson.
function checkAndAggregateReturnExpressionTypes(body: Block, contextualType?: Type, contextualMapper?: TypeMapper): Type[] { | ||
var aggregatedTypes: Type[] = []; | ||
|
||
forEachReturnStatement(body, (returnStatement) => { |
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.
no parens around (returnStatement)
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.
Fixed in 0e10fc7.
As per feedback in pull request #147.
In effect this fixes #62
Also
- Changes the error message for get accessors lacking return expressions.
- Actually checks for return expressions instead of return statements for get-accessors.
- Removes fancy quotes.
- Corrects errors in the compiler caught by the new check.
- Simplified
checkAndAggregateReturnTypes
by extracting it out tovisitReturnStatements
.