Skip to content

Commit 6137d08

Browse files
committed
fix: prevent false ES module detection when code contains "export" in strings
Fixed a critical bug where components containing nested code snippets with "export" keywords (e.g., displaying code examples) were incorrectly treated as ES modules by Deno, causing "Unexpected token '{'" syntax errors.
1 parent 96c5ef0 commit 6137d08

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

crates/rari/src/runtime/runtime_factory.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,29 @@ async fn run_event_loop_with_promise_timeout(
782782
Ok(())
783783
}
784784

785+
fn has_export_statement(code: &str) -> bool {
786+
for line in code.lines() {
787+
let trimmed = line.trim();
788+
789+
if trimmed.is_empty() {
790+
continue;
791+
}
792+
793+
if trimmed.starts_with("//") || trimmed.starts_with("/*") || trimmed.starts_with("*") {
794+
continue;
795+
}
796+
797+
if trimmed.starts_with("export ")
798+
|| trimmed.starts_with("export{")
799+
|| trimmed.starts_with("export {")
800+
{
801+
return true;
802+
}
803+
}
804+
805+
false
806+
}
807+
785808
async fn execute_script(
786809
runtime: &mut JsRuntime,
787810
module_loader: &Rc<RariModuleLoader>,
@@ -801,11 +824,11 @@ async fn execute_script(
801824
return Ok(create_already_evaluated_response(script_name));
802825
}
803826

804-
if is_registrable_module
805-
|| script_code.trim().starts_with("import ")
806-
|| script_code.trim().contains("export ")
827+
let has_actual_module_syntax = script_code.trim().starts_with("import ")
807828
|| script_code.contains("\"use module\"")
808-
{
829+
|| has_export_statement(script_code);
830+
831+
if is_registrable_module || has_actual_module_syntax {
809832
let specifier_str = module_loader.create_specifier(script_name, "rari_internal");
810833

811834
module_loader.add_module(&specifier_str, script_name, script_code_string.clone());

pnpm-lock.yaml

Lines changed: 60 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)