Skip to content
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

Provide a specialized error on top-level await and --module node12 #46149

Open
DanielRosenwasser opened this issue Sep 30, 2021 · 6 comments · May be fixed by #46488
Open

Provide a specialized error on top-level await and --module node12 #46149

DanielRosenwasser opened this issue Sep 30, 2021 · 6 comments · May be fixed by #46488
Labels
Good First Issue Help Wanted Suggestion
Milestone

Comments

@DanielRosenwasser
Copy link
Member

@DanielRosenwasser DanielRosenwasser commented Sep 30, 2021

Under node12, a top-level await results in

Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.

This error message has too much text. We should consider specializing these cases for

  • ES modules < es2022
  • Node modules < nodenext
The module setting '{0}' does not support top-level 'await' expressions. Consider switching to '{1}'.
@fatcerberus
Copy link

@fatcerberus fatcerberus commented Sep 30, 2021

I thought "module": "es2020" allowed top-level await. Did that change?

@DanielRosenwasser
Copy link
Member Author

@DanielRosenwasser DanielRosenwasser commented Sep 30, 2021

import() is in module: es2020, but top-level await hasn't technically been added to a latest-ratified standard. When it does, it will likely be ES2022.

In other words, it hit stage 4 this past year.

@sandersn sandersn added Good First Issue Suggestion labels Oct 7, 2021
@sandersn sandersn added this to the Backlog milestone Oct 7, 2021
@sandersn sandersn added the Help Wanted label Oct 7, 2021
@sidharthv96
Copy link
Contributor

@sidharthv96 sidharthv96 commented Oct 15, 2021

Proposed new error messages would be similar to:

  • The module setting 'es2015' does not support top-level 'await' expressions. Consider switching to 'es2022'.
  • The module setting 'node12' does not support top-level 'await' expressions. Consider switching to 'nodenext'.
  • The target setting 'es5' does not support top-level 'await' expressions. Consider switching to 'es2017'.

Is there a situation to suggest esnext or system?


Is the same applicable for this message too?

Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.

Changed to:

  • The module setting '{0}' does not support top-level 'for await' expressions. Consider switching to '{1}'.

@DanielRosenwasser
Copy link
Member Author

@DanielRosenwasser DanielRosenwasser commented Oct 15, 2021

The target setting 'es5' does not support top-level 'await' expressions. Consider switching to 'es2017'.

I believe that unless you're using system for your module target, the target has to be es2022 or later. Very confusing, but someone else may be able to correct me.

@sidharthv96
Copy link
Contributor

@sidharthv96 sidharthv96 commented Oct 15, 2021

This is the current check for the message.

(moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017

So if someone is using module: ES2015 and target:ES5, should they get two error messages?
(I'm not sure if this is a valid scenario or not 😅)

Based on the current error message, My understanding is

GivenModule GivenTarget SuggestedModule SuggestedTarget
>= ES2022 >= ES2017 - -
System >= ES2017 - -
NodeNext >= ES2017 - -
< ES2022 >= ES2017 ES2022 -
Node12 >= ES2017 NodeNext -
>= ES2022 < ES2017 - ES2017
< ES2022 < ES2017 ES2022 ES2017

I'm not sure if you meant the target to be es2022 as the code only has till es2021

export const enum ScriptTarget {
        ES3 = 0,
        ES5 = 1,
        ES2015 = 2,
        ES2016 = 3,
        ES2017 = 4,
        ES2018 = 5,
        ES2019 = 6,
        ES2020 = 7,
        ES2021 = 8,
        ESNext = 99,
        JSON = 100,
        Latest = ESNext,
    }

This is really confusing indeed. 😅

@weswigham
Copy link
Member

@weswigham weswigham commented Oct 15, 2021

TLA is actually allowed in only in esm mode nodenext files, since latest node allows TLA in esm - we have tests to that effect - a specialized error for cjs mode files would be good, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Help Wanted Suggestion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants