Skip to content

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

Merged
merged 15 commits into from
May 5, 2022
Merged

Conversation

sandersn
Copy link
Member

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

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
@gabritto
Copy link
Member

gabritto commented May 2, 2022

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

@sandersn
Copy link
Member Author

sandersn commented May 2, 2022

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 return is in a JS file.

@sandersn
Copy link
Member Author

sandersn commented May 2, 2022

Alternatively, we could instead stop issuing the error for any script. It's really good default assumption that a .ts script will emit to .js script that will run in node or the browser, where top-level return is legal.

Actually, I kind of like that idea. Let me try it.

@gabritto
Copy link
Member

gabritto commented May 2, 2022

Alternatively, we could instead stop issuing the error for any script. It's really good default assumption that a .ts script will emit to .js script that will run in node or the browser, where top-level return is legal.

Actually, I kind of like that idea. Let me try it.

yeah, that's what I was thinking and it seems reasonable to me

sandersn added 2 commits May 2, 2022 11:11
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
Copy link
Member

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": {
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

Comment on lines 5 to 16
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;

}

};
Copy link
Member

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?

@DanielRosenwasser
Copy link
Member

Allowing return at the top-level of a .ts file was never allowed, and probably shouldn't be allowed now. Node.js is one of the few environments that actually allows this, and new code should use process.exit(0) to work across environments.

@sandersn
Copy link
Member Author

sandersn commented May 2, 2022

Hmm, OK. I looked again and there are piles of isInJSFile calls throughout the checker, so I guess I made up the "don't check for js" rule there.

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.

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a 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.

@@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!!getSourceFileOfNode(node).externalModuleIndicator || !isInJSFile(node)) {
if (!isInJSFile(node)) {

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@jakebailey jakebailey left a 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.

@sandersn
Copy link
Member Author

sandersn commented May 5, 2022

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.

@sandersn sandersn merged commit 650c056 into main May 5, 2022
@sandersn sandersn deleted the no-error-on-toplevel-return-in-js branch May 5, 2022 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TS1108 "problem" showing on pure Javascript (node) file - Erroneous Error Flag
5 participants