Skip to content

Commit

Permalink
fix: cjs export rewritten to invalid identifier (#21853)
Browse files Browse the repository at this point in the history
<!--
Before submitting a PR, please read https://deno.com/manual/contributing

1. Give the PR a descriptive title.

  Examples of good title:
    - fix(std/http): Fix race condition in server
    - docs(console): Update docstrings
    - feat(doc): Handle nested reexports

  Examples of bad title:
    - fix #7123
    - update docs
    - fix bugs

2. Ensure there is a related issue and it is referenced in the PR text.
3. Ensure there are tests that cover the changes.
4. Ensure `cargo test` passes.
5. Ensure `./tools/format.js` passes without changing files.
6. Ensure `./tools/lint.js` passes.
7. Open as a draft PR if your work is still in progress. The CI won't
run
   all steps, but you can add '[ci]' to a commit message to force it to.
8. If you would like to run the benchmarks on the CI, add the 'ci-bench'
label.
-->

Fixes #21836
  • Loading branch information
marvinhagemeister authored Jan 8, 2024
1 parent e212e1f commit 040fdee
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ext/node/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,16 @@ fn add_export(
) {
fn is_valid_var_decl(name: &str) -> bool {
// it's ok to be super strict here
if name.is_empty() {
return false;
}

if let Some(first) = name.chars().next() {
if !first.is_ascii_alphabetic() && first != '_' && first != '$' {
return false;
}
}

name
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
Expand Down Expand Up @@ -479,7 +489,7 @@ mod tests {
let mut temp_var_count = 0;
let mut source = vec![];

let exports = vec!["static", "server", "app", "dashed-export"];
let exports = vec!["static", "server", "app", "dashed-export", "3d"];
for export in exports {
add_export(&mut source, export, "init", &mut temp_var_count);
}
Expand All @@ -492,6 +502,8 @@ mod tests {
"export const app = init;".to_string(),
"const __deno_export_2__ = init;".to_string(),
"export { __deno_export_2__ as \"dashed-export\" };".to_string(),
"const __deno_export_3__ = init;".to_string(),
"export { __deno_export_3__ as \"3d\" };".to_string(),
]
)
}
Expand Down

0 comments on commit 040fdee

Please sign in to comment.