-
Notifications
You must be signed in to change notification settings - Fork 43
Upstream PR for utility method for detecting ES module syntax #330
Comments
I’d expect usage of |
And HTML comments. |
My method detects whether source code contains syntax that's permitted only in the Module parse goal. You're describing the inverse, detecting syntax that's only permitted in the Script parse goal. In theory such a method could serve as a companion to this one, but it would have very limited usefulness as Script-only syntax is generally all obsolete and almost never appears anymore in real world code. |
they're saying you should force the result to false if html comments and/or with statements are present. |
This is a generic utility method, it should be generically applicable. |
It should also be fast, and checking for things that are unlikely to be present slows it down with no benefit. |
correctness > speed? we're discussing the evaluation of random code after all... |
There’s nothing incorrect about You can propose a companion Another detection we discussed was looking for references to the CommonJS globals |
I assume it would only not already be false if the text contains import/export syntax. And in those cases the choice is "script with syntax error or module with syntax error". Or am I missing something? If that's the case, returning |
I’m thinking of opening a PR against core for nodejs/ecmascript-modules#69. Does anyone have any feedback for me before I do so?
Here’s what I plan to say about the PR:
This PR adds a utility method to detect whether input JavaScript source code contains ES module syntax:
The use case for this is any utility that currently uses
vm.Script
’srunInThisContext
and might also want to support ESM input source code, and therefore would need to know when to usevm.Script
versus the ES modulevm.SourceTextModule
(and has no definitive signal from the user as to the intended source code type, and where the risk of evaluating in the wrong module system is acceptable for the use case). Two such utilities that I’m familiar with are Babel and CoffeeScript, which each take arbitrary source code as user input (like--eval
) and usevm.runInThisContext
for evaluation. I would also imagine that this method would be useful to some loaders or testing frameworks.The text was updated successfully, but these errors were encountered: