From 040fdeec0bada9883e0d87bb6bb95651dda1769f Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Mon, 8 Jan 2024 18:50:52 +0100 Subject: [PATCH] fix: cjs export rewritten to invalid identifier (#21853) Fixes https://github.com/denoland/deno/issues/21836 --- ext/node/analyze.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/ext/node/analyze.rs b/ext/node/analyze.rs index 4e8ee8af46..2a0324ccf7 100644 --- a/ext/node/analyze.rs +++ b/ext/node/analyze.rs @@ -392,6 +392,16 @@ fn add_export( ) { fn is_valid_var_decl(name: &str) -> bool { // it's ok to be super strict here + if name.is_empty() { + return false; + } + + if let Some(first) = name.chars().next() { + if !first.is_ascii_alphabetic() && first != '_' && first != '$' { + return false; + } + } + name .chars() .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$') @@ -479,7 +489,7 @@ mod tests { let mut temp_var_count = 0; let mut source = vec![]; - let exports = vec!["static", "server", "app", "dashed-export"]; + let exports = vec!["static", "server", "app", "dashed-export", "3d"]; for export in exports { add_export(&mut source, export, "init", &mut temp_var_count); } @@ -492,6 +502,8 @@ mod tests { "export const app = init;".to_string(), "const __deno_export_2__ = init;".to_string(), "export { __deno_export_2__ as \"dashed-export\" };".to_string(), + "const __deno_export_3__ = init;".to_string(), + "export { __deno_export_3__ as \"3d\" };".to_string(), ] ) }