-
Notifications
You must be signed in to change notification settings - Fork 12.9k
No error on toplevel return in JS #48874
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
Conversation
Turns out it's only an error in modules. It's possible to keep this error on the list of "OK for JS" errors and make the checker code stop issuing it for JS scripts only. However, I don't think the error is valuable enough to do that. Fixes #48224
Is detecting if a js file is a script or a module not reliable? I'm not sure I understand why you feel it is not valuable enough to do this |
Making decisions based on JS-vs-TS is not a common thing to do in the middle of the checker. Of course, there are some constructs like commonjs imports that should only show up in JS, but the checker treats them the same regardless of location. In this case, it would have to skip the error only in the case that top-level |
Alternatively, we could instead stop issuing the error for any script. It's really good default assumption that a Actually, I kind of like that idea. Let me try it. |
yeah, that's what I was thinking and it seems reasonable to me |
Only issue "no top-level return" error for modules, not scripts, regardless of whether it's TS or JS.
@@ -6,12 +6,12 @@ return this.edit(role) | |||
>this.edit : any | |||
>this : typeof globalThis | |||
>edit : any | |||
>role : any | |||
>role : error |
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 a weird change... this is the role
parameter in this test's confusing return statement. Not sure why it went from an implicit any to an error
.
@@ -1,5 +0,0 @@ | |||
{ | |||
"compilerOptions": { |
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.
Why did this get deleted?
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'm pretty sure this is an orphaned baseline. I have no idea how I decided to delete it though.
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 undid the delete but all tests pass either way.
return { | ||
~~~~~~ | ||
!!! error TS1108: A 'return' statement can only be used within a function body. | ||
|
||
"set": function (key, value) { | ||
|
||
// 'private' should not be considered a member variable here. | ||
private[key] = value; | ||
|
||
} | ||
|
||
}; |
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.
We're still supposed to disallow this in TS files, right?
Allowing |
Hmm, OK. I looked again and there are piles of I'll push a commit that makes the checker issue the error for all non-js files and for JS files that use ES modules. |
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.
Just undo the introduction of this check. It used to only show up in TS files and in JS files when checkJS was on. Reinstate that behavior.
src/compiler/checker.ts
Outdated
@@ -38913,7 +38913,9 @@ namespace ts { | |||
} | |||
|
|||
if (!container) { | |||
grammarErrorOnFirstToken(node, Diagnostics.A_return_statement_can_only_be_used_within_a_function_body); | |||
if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) { |
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.
if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) { | |
if (!isInJSFile(node)) { |
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.
In fact, I would say that the right fix is still to issue the error in checkJs, but not when providing the default grammar errors for JS files.
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.
We should maybe revisit this after the beta since Gabriela's semantics are more accurate, but for the purposes of shipping a fix, it's definitely simpler to not show the error in any plain JS file.
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 LGTM now, a conservative choice, but I don't know if @DanielRosenwasser or @gabritto have any other last comments. I know Danial's PR status is still requesting changes.
I'm pretty sure I addressed Daniel's comments; Daniel, Gabriela and I will need to decide what future semantics should be separately, so I'm going to merge this PR so that it's not hanging around until the last minute of the beta. |
Turns out it's only an error in modules. It's possible to keep this error on the list of "OK for JS" errors and make the checker code stop issuing it for JS scripts only. However, I don't think the error is valuable enough to do that.
Fixes #48224