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

Add import in the non-module description #2374

Merged
merged 4 commits into from
Jul 24, 2023

Conversation

myogh
Copy link
Contributor

@myogh myogh commented May 5, 2022

  • I think await doesn't play a role in deciding whether a JS file is a module or a script.

@orta
Copy link
Contributor

orta commented May 11, 2022

I think this is previously accurate because only modules are allowed top level await but I think it's reasonable to add import to this list (instead of replacing await)

@myogh
Copy link
Contributor Author

myogh commented May 11, 2022

Thank you @orta, I was not aware of the "top level await" case. Thanks for mentioning it. Today I learned something new.

@myogh myogh changed the title fix: replace await with import Add import in the non-module description May 11, 2022
@jakebailey
Copy link
Member

I think this one is tricky; having a top-level await doesn't cause TS to treat the file as a module. Instead, we use import/export or the moduleDetection setting to determine that, and then if we see a top-level await that shouldn't be there, we error.

But, I think this section is already awkward... it's not that the JavaScript spec says anything about these things, right?

@andrewbranch
Copy link
Member

I’m not an expert on the ECMAScript grammar but I suspect the wording is a little weird here. The spec does distinguish between the grammars for script bodies and module bodies, and these three things are indeed relevant, but I think our wording probably implies the wrong causality. My understanding is that spec-compliant engines know ahead of time whether they should be parsing a script or a module (via <script type="module" in the browser, or file extension / package.json "type" in Node, for example), and that decision determines how the rest of the file is parsed, which makes import/export/TLA a syntax error in scripts. This is different from how TypeScript parses, where it has the ability to parse either one until an import or export is seen. That said, I don’t think it’s worth diving that far into the weeds here. This page will likely be replaced via microsoft/TypeScript#52593.

@andrewbranch andrewbranch merged commit e9f97da into microsoft:v2 Jul 24, 2023
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants