Skip to content

No error on toplevel return in JS - #48874

Merged
Nathan Shively-Sanders (sandersn) merged 15 commits into
mainfrom
no-error-on-toplevel-return-in-js
May 5, 2022
Merged

Nathan Shively-Sanders (sandersn) merged 15 commits into
mainfrom
no-error-on-toplevel-return-in-js

Conversation

@sandersn

Copy link
Copy Markdown
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
Comment thread tests/cases/conformance/salsa/plainJSGrammarErrors.ts
@gabritto

Copy link
Copy Markdown
Member

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
Copy Markdown
Member Author

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
Copy Markdown
Member Author

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
Copy Markdown
Member

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

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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;

}

}; No newline at end of file

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member Author

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
Copy Markdown
Member

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.

Comment thread 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)) {

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

@jakebailey Jake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

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 Daniel Rosenwasser (@DanielRosenwasser) or Gabriela Araujo Britto (@gabritto) have any other last comments. I know Danial's PR status is still requesting changes.

@sandersn

Copy link
Copy Markdown
Member Author

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
Nathan Shively-Sanders (sandersn) deleted the no-error-on-toplevel-return-in-js branch May 5, 2022 16:35
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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