mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -05:00
fix(node): handle cjs exports with escaped chars (#27438)
Closes https://github.com/denoland/deno/issues/27422
This commit is contained in:
parent
26425a137b
commit
e6869d7fa6
5 changed files with 34 additions and 11 deletions
|
@ -162,7 +162,7 @@ impl<TCjsCodeAnalyzer: CjsCodeAnalyzer, TNodeResolverEnv: NodeResolverEnv>
|
|||
add_export(
|
||||
&mut source,
|
||||
export,
|
||||
&format!("mod[\"{}\"]", escape_for_double_quote_string(export)),
|
||||
&format!("mod[{}]", to_double_quote_string(export)),
|
||||
&mut temp_var_count,
|
||||
);
|
||||
}
|
||||
|
@ -561,8 +561,8 @@ fn add_export(
|
|||
"const __deno_export_{temp_var_count}__ = {initializer};"
|
||||
));
|
||||
source.push(format!(
|
||||
"export {{ __deno_export_{temp_var_count}__ as \"{}\" }};",
|
||||
escape_for_double_quote_string(name)
|
||||
"export {{ __deno_export_{temp_var_count}__ as {} }};",
|
||||
to_double_quote_string(name)
|
||||
));
|
||||
} else {
|
||||
source.push(format!("export const {name} = {initializer};"));
|
||||
|
@ -620,14 +620,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) -> Cow<str> {
|
||||
// this should be rare, so doing a scan first before allocating is ok
|
||||
if text.chars().any(|c| matches!(c, '"' | '\\')) {
|
||||
// don't bother making this more complex for perf because it's rare
|
||||
Cow::Owned(text.replace('\\', "\\\\").replace('"', "\\\""))
|
||||
} else {
|
||||
Cow::Borrowed(text)
|
||||
}
|
||||
fn to_double_quote_string(text: &str) -> String {
|
||||
// serde can handle this for us
|
||||
serde_json::to_string(text).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -665,4 +660,13 @@ mod tests {
|
|||
Some(("@some-package/core".to_string(), "./actions".to_string()))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_to_double_quote_string() {
|
||||
assert_eq!(to_double_quote_string("test"), "\"test\"");
|
||||
assert_eq!(
|
||||
to_double_quote_string("\r\n\t\"test"),
|
||||
"\"\\r\\n\\t\\\"test\""
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"args": "run -A main.js",
|
||||
"output": "output.out"
|
||||
}
|
2
tests/specs/node/cjs_key_escaped_whitespace/main.js
Normal file
2
tests/specs/node/cjs_key_escaped_whitespace/main.js
Normal file
|
@ -0,0 +1,2 @@
|
|||
const bang = await import("./module.cjs");
|
||||
console.log("imported:", bang);
|
6
tests/specs/node/cjs_key_escaped_whitespace/module.cjs
Normal file
6
tests/specs/node/cjs_key_escaped_whitespace/module.cjs
Normal file
|
@ -0,0 +1,6 @@
|
|||
module.exports = {
|
||||
"\nx": "test",
|
||||
"\ty": "test",
|
||||
"\rz": "test",
|
||||
'"a': "test",
|
||||
};
|
7
tests/specs/node/cjs_key_escaped_whitespace/output.out
Normal file
7
tests/specs/node/cjs_key_escaped_whitespace/output.out
Normal file
|
@ -0,0 +1,7 @@
|
|||
imported: [Module: null prototype] {
|
||||
"\ty": "test",
|
||||
"\nx": "test",
|
||||
"\rz": "test",
|
||||
'"a': "test",
|
||||
default: { "\nx": "test", "\ty": "test", "\rz": "test", '"a': "test" }
|
||||
}
|
Loading…
Reference in a new issue