diff options
author | await-ovo <13152410380@163.com> | 2023-07-04 02:41:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-03 18:41:09 +0000 |
commit | 208e65d33a6eed09404ecb0bc6858ce95c754f81 (patch) | |
tree | b8150630e110013379fa292b2145b819d28f4b2c /ext/node/analyze.rs | |
parent | d632cce129cb7025a34cf0aa7262a38fb12f47c4 (diff) |
fix(npm): escape export identifier in double quoted string (#19694)
Diffstat (limited to 'ext/node/analyze.rs')
-rw-r--r-- | ext/node/analyze.rs | 8 |
1 files changed, 6 insertions, 2 deletions
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<TCjsEsmCodeAnalyzer: CjsEsmCodeAnalyzer> 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::*; |