From 208e65d33a6eed09404ecb0bc6858ce95c754f81 Mon Sep 17 00:00:00 2001 From: await-ovo <13152410380@163.com> Date: Tue, 4 Jul 2023 02:41:09 +0800 Subject: fix(npm): escape export identifier in double quoted string (#19694) --- ext/node/analyze.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'ext/node') diff --git a/ext/node/analyze.rs b/ext/node/analyze.rs index 7d98f53db..eed0ceb4f 100644 --- a/ext/node/analyze.rs +++ b/ext/node/analyze.rs @@ -202,7 +202,7 @@ impl add_export( &mut source, export, - &format!("mod[\"{export}\"]"), + &format!("mod[\"{}\"]", escape_for_double_quote_string(export)), &mut temp_var_count, ); } @@ -460,7 +460,8 @@ fn add_export( "const __deno_export_{temp_var_count}__ = {initializer};" )); source.push(format!( - "export {{ __deno_export_{temp_var_count}__ as \"{name}\" }};" + "export {{ __deno_export_{temp_var_count}__ as \"{}\" }};", + escape_for_double_quote_string(name) )); } else { source.push(format!("export const {name} = {initializer};")); @@ -518,6 +519,9 @@ fn not_found(path: &str, referrer: &Path) -> AnyError { std::io::Error::new(std::io::ErrorKind::NotFound, msg).into() } +fn escape_for_double_quote_string(text: &str) -> String { + text.replace('\\', "\\\\").replace('"', "\\\"") +} #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3