Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d6233d8

Browse files
fix(plugin): fix expand_typeof_replacements (#2196)
<!-- Thank you for contributing! --> ### Description <!-- Please insert your description here and provide especially info about the "what" this PR is solving --> Related issue:#2057 . fix the function of `expand_typeof_replacements`
1 parent 3874566 commit d6233d8

2 files changed

Lines changed: 55 additions & 21 deletions

File tree

crates/rolldown_plugin_replace/src/utils.rs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,36 @@ static OBJECT_RE: LazyLock<Regex> = LazyLock::new(|| {
1010
pub(crate) fn expand_typeof_replacements(
1111
values: &HashMap<String, String>,
1212
) -> HashMap<String, String> {
13-
values
14-
.keys()
15-
.filter_map(|key| {
16-
if let Ok(true) = OBJECT_RE.is_match(key) {
17-
Some(key.char_indices().filter_map(|(pos, c)| {
18-
if c == '.' {
19-
Some((format!("typeof {}", &key[0..pos]), "\"object\"".to_string()))
20-
} else {
21-
None
22-
}
23-
}))
24-
} else {
25-
None
26-
}
27-
})
28-
.flatten()
29-
.collect()
13+
let mut replacements: Vec<(String, String)> = Vec::new();
14+
15+
for key in values.keys() {
16+
if let Ok(matched) = OBJECT_RE.captures(key) {
17+
let capture_str = matched.unwrap().get(0).unwrap().as_str();
18+
19+
let capture_vec: Vec<&str> = capture_str.split('.').collect::<Vec<&str>>();
20+
21+
let capture_arr = capture_vec.as_slice();
22+
23+
let replaces: Vec<(String, String)> = capture_arr[0..capture_arr.len() - 1]
24+
.iter()
25+
.flat_map(|x| {
26+
vec![
27+
(format!("typeof {} ===", *x), "\"object\" ===".to_string()),
28+
(format!("typeof {}===", *x), "\"object\"===".to_string()),
29+
(format!("typeof {} !==", *x), "\"object\" !==".to_string()),
30+
(format!("typeof {}!==", *x), "\"object\"!==".to_string()),
31+
(format!("typeof {} ==", *x), "\"object\" ===".to_string()),
32+
(format!("typeof {}==", *x), "\"object\"===".to_string()),
33+
(format!("typeof {}!=", *x), "\"object\"!==".to_string()),
34+
(format!("typeof {} !=", *x), "\"object\" !==".to_string()),
35+
]
36+
})
37+
.collect();
38+
replacements.extend(replaces);
39+
};
40+
}
41+
42+
HashMap::from_iter(replacements)
3043
}
3144

3245
#[cfg(test)]
@@ -42,9 +55,30 @@ mod tests {
4255
let result = expand_typeof_replacements(&map);
4356

4457
let expected = HashMap::from([
45-
("typeof a".to_string(), "\"object\"".to_string()),
46-
("typeof a.b".to_string(), "\"object\"".to_string()),
47-
("typeof a.b.c".to_string(), "\"object\"".to_string()),
58+
("typeof a===".to_string(), "\"object\"===".to_string()),
59+
("typeof a ===".to_string(), "\"object\" ===".to_string()),
60+
("typeof a==".to_string(), "\"object\"===".to_string()),
61+
("typeof a ==".to_string(), "\"object\" ===".to_string()),
62+
("typeof a!==".to_string(), "\"object\"!==".to_string()),
63+
("typeof a !==".to_string(), "\"object\" !==".to_string()),
64+
("typeof a!=".to_string(), "\"object\"!==".to_string()),
65+
("typeof a !=".to_string(), "\"object\" !==".to_string()),
66+
("typeof b===".to_string(), "\"object\"===".to_string()),
67+
("typeof b ===".to_string(), "\"object\" ===".to_string()),
68+
("typeof b==".to_string(), "\"object\"===".to_string()),
69+
("typeof b ==".to_string(), "\"object\" ===".to_string()),
70+
("typeof b!==".to_string(), "\"object\"!==".to_string()),
71+
("typeof b !==".to_string(), "\"object\" !==".to_string()),
72+
("typeof b!=".to_string(), "\"object\"!==".to_string()),
73+
("typeof b !=".to_string(), "\"object\" !==".to_string()),
74+
("typeof c===".to_string(), "\"object\"===".to_string()),
75+
("typeof c ===".to_string(), "\"object\" ===".to_string()),
76+
("typeof c==".to_string(), "\"object\"===".to_string()),
77+
("typeof c ==".to_string(), "\"object\" ===".to_string()),
78+
("typeof c!==".to_string(), "\"object\"!==".to_string()),
79+
("typeof c !==".to_string(), "\"object\" !==".to_string()),
80+
("typeof c!=".to_string(), "\"object\"!==".to_string()),
81+
("typeof c !=".to_string(), "\"object\" !==".to_string()),
4882
]);
4983

5084
assert_eq!(result, expected);

crates/rolldown_plugin_replace/tests/form/process_check/artifacts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ source: crates/rolldown_testing/src/integration_test.rs
88
```js
99
1010
//#region input.js
11-
{
11+
if (typeof process !== "undefined" && typeof process.env === "object" && true) {
1212
console.log("production");
1313
}
1414

0 commit comments

Comments
 (0)