Skip to main content

is_module_source

Function is_module_source 

Source
pub fn is_module_source(source: &str) -> bool
Expand description

Whether source should be evaluated as a module, decided the way qjs decides.

This is a question about source text rather than about the engine — qjs asks it to pick between two eval modes and qjsc asks it to pick between two output shapes — so it lives here, where both can reach it without linking an interpreter.

The rule is deliberately shallow: the first token must be import or export (an import followed by . or ( is import.meta or import(), both of which are expressions, so a script may use them). Nothing else makes a file a module.

It has to be this shallow to match the reference, and the difference is observable: a file that merely mentions import.meta further down is a script, so it fails with “import.meta only valid in module code” rather than silently becoming a module.