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:
parent
d632cce129
commit
208e65d33a
6 changed files with 39 additions and 2 deletions
|
@ -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",
|
||||
|
|
13
cli/tests/testdata/npm/cjs-invalid-name-exports/main.out
vendored
Normal file
13
cli/tests/testdata/npm/cjs-invalid-name-exports/main.out
vendored
Normal 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"
|
||||
}
|
3
cli/tests/testdata/npm/cjs-invalid-name-exports/main.ts
vendored
Normal file
3
cli/tests/testdata/npm/cjs-invalid-name-exports/main.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
import * as foo from "npm:@denotest/cjs-invalid-name-exports";
|
||||
|
||||
console.log(foo);
|
6
cli/tests/testdata/npm/registry/@denotest/cjs-invalid-name-exports/1.0.0/index.js
vendored
Normal file
6
cli/tests/testdata/npm/registry/@denotest/cjs-invalid-name-exports/1.0.0/index.js
vendored
Normal 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'
|
4
cli/tests/testdata/npm/registry/@denotest/cjs-invalid-name-exports/1.0.0/package.json
vendored
Normal file
4
cli/tests/testdata/npm/registry/@denotest/cjs-invalid-name-exports/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"name": "@denotest/cjs-invalid-name-exports",
|
||||
"version": "1.0.0"
|
||||
}
|
|
@ -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::*;
|
||||
|
|
Loading…
Add table
Reference in a new issue