1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-21 21:50:00 -05:00

fix(npm): escape export identifier in double quoted string (#19694)

This commit is contained in:
await-ovo 2023-07-04 02:41:09 +08:00 committed by GitHub
parent d632cce129
commit 208e65d33a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 39 additions and 2 deletions

View file

@ -81,6 +81,13 @@ itest!(cjs_this_in_exports {
exit_code: 1,
});
itest!(cjs_invalid_name_exports {
args: "run --allow-read --quiet npm/cjs-invalid-name-exports/main.ts",
output: "npm/cjs-invalid-name-exports/main.out",
envs: env_vars_for_npm_tests(),
http_server: true,
});
itest!(translate_cjs_to_esm {
args: "run -A --quiet npm/translate_cjs_to_esm/main.js",
output: "npm/translate_cjs_to_esm/main.out",

View file

@ -0,0 +1,13 @@
[Module: null prototype] {
"a \\ b": "a \\ b",
"another 'case'": "example",
default: {
'wow "double quotes"': "double quotes",
"another 'case'": "example",
"a \\ b": "a \\ b",
"name variable": "a",
"foo - bar": "foo - bar"
},
"foo - bar": "foo - bar",
'wow "double quotes"': "double quotes"
}

View file

@ -0,0 +1,3 @@
import * as foo from "npm:@denotest/cjs-invalid-name-exports";
console.log(foo);

View file

@ -0,0 +1,6 @@
exports['wow "double quotes"'] = "double quotes"
exports["another 'case'"] = 'example'
exports["a \\ b"] = 'a \\ b'
const a = 'name variable'
exports[a] = "a";
exports['foo - bar'] = 'foo - bar'

View file

@ -0,0 +1,4 @@
{
"name": "@denotest/cjs-invalid-name-exports",
"version": "1.0.0"
}

View file

@ -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::*;