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

TsImportEquals causes program to be considered a module. Should it? #9694

Open
dsherret opened this issue Oct 30, 2024 · 4 comments
Open

TsImportEquals causes program to be considered a module. Should it? #9694

dsherret opened this issue Oct 30, 2024 · 4 comments

Comments

@dsherret
Copy link
Contributor

dsherret commented Oct 30, 2024

The prescence of a TsImportEquals causes a script to be considered a module.

import test = require("asdf");

Playground link (notice source type is "unknown" and the AST shows as "module" instead of "script"):

https://play.swc.rs/?version=1.7.40&code=H4sIAAAAAAAAA8vMLcgvKlEoSS0uUbBVKEotLM0sStVQSixOSVPStOblAgBnhfLXIAAAAA%3D%3D&config=H4sIAAAAAAAAA1WQMW7DMAxF95zC4BzUhcesLbJ16gkEhW6USqRAUmiNwHev5MRxupHv8%2FODvO66Di7q4dBda1mb7ERRHn0lOpG530rApozqJWSD%2FaqaNsmk4ELmmwDm5AutmVCH12GAlYsjHVnSc0JRfMcxEB5Z3qJTPQaMJ73vfUQJOm9PvopCyiz2yUU8trCzWdZD36OmFz33%2BWbZbwYpZCEts64YJ2fBw12e%2F10QmbXNjS5qu23hkPhUIm7vai9pyzynxHRR2CYDhXFaFywo6Mdqh0LfxD8Eu%2FkPEPbTUoQBAAA%3D

Is this expected behaviour? I understand that TsImportEquals are only included in the ModuleDecl enum.

@dsherret
Copy link
Contributor Author

I think probably this one is not worth fixing given this stuff is legacy? I can compute if a program is actually a script another way

@kdy1
Copy link
Member

kdy1 commented Oct 31, 2024

@swc-project/core-es Do you have any opinion?
I expect the fix to be trivial so not sure.

We would only need to check for ModuleDecl::xxx while matching ModuleItem::ModuleDecl

@magic-akari
Copy link
Member

We have already handled a similar issue

However, this time it needs to be processed in the parser.

Technically speaking, TsImportEquals is a module declaration that is part of the syntax and must appear at the top of the file. This declaration allows TypeScript to perform static analysis on CommonJS, similar to the import declaration in ESM.

In SWC, a module only represents an ECMAScript module and does not refer any other module system.
Therefore, I support this change.

@magic-akari
Copy link
Member

magic-akari commented Oct 31, 2024

There is a special case that complicates the issue.
In TypeScript, the module: nodenext turns TsImportEquals into a related call to _createRequire.

https://www.typescriptlang.org/play/?target=99&moduleResolution=99&module=199#code/PTAEAEFsHsBMFcA2BTAXKAdnZHkA8AXAKBAgIEMAnAc2QPWQGddCiBLSAB2koNElABeUJWQBHeG1EAKAEQwEKWQEoA3ESIBjaBkbQUAOkTRq0yAYCycJMmVA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants