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.
| >this : typeof globalThis | ||
| >edit : any | ||
| >role : any | ||
| >role : error |
There was a problem hiding this comment.
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.
Why did this get deleted?
There was a problem hiding this comment.
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.
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; | ||
|
|
||
| } | ||
|
|
||
| }; No newline at end of file |
There was a problem hiding this comment.
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. |
DanielRosenwasser
left a comment
There was a problem hiding this comment.
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
|
|
||
| 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.
| if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) { | |
| if (!isInJSFile(node)) { |
There was a problem hiding this comment.
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.
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.
jakebailey
left a comment
There was a problem hiding this comment.
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