Skip to content

Truly independent web browser

LibJS: Declare destructured catch parameters only in their catch scope

Tim Ledbetterd81256bccf7e
diff --git a/Libraries/LibJS/Rust/src/parser/statements.rs b/Libraries/LibJS/Rust/src/parser/statements.rs
index 8aab8490d68..6e63f3d5a18 100644
--- a/Libraries/LibJS/Rust/src/parser/statements.rs
+++ b/Libraries/LibJS/Rust/src/parser/statements.rs
@@ -751,14 +751,14 @@ impl Parser<'_> {
self.scope_collector.open_catch_scope();
+ let mut catch_pattern_names: Vec<StringId> = Vec::new();
let parameter = if self.match_token(TokenType::ParenOpen) {
self.consume();
let parameter = if self.match_token(TokenType::CurlyOpen) || self.match_token(TokenType::BracketOpen) {
- self.pattern_bound_names.clear();
let pattern = self.parse_binding_pattern();
+ let pattern_bound_names = std::mem::take(&mut self.pattern_bound_names);
// Materialize Utf16Strings for slice-based scope-collector calls.
- let names_to_check: Vec<Utf16String> = self
- .pattern_bound_names
+ let names_to_check: Vec<Utf16String> = pattern_bound_names
.iter()
.map(|(n, _)| self.arena.strings[*n].clone())
.collect();
@@ -781,7 +781,8 @@ impl Parser<'_> {
self.scope_collector.add_catch_parameter_pattern(&bound_names);
// Register each binding pattern identifier for scope analysis
// so they get is_local() annotations (matching variable declarations).
- let pattern_bound_ids: Vec<IdentifierId> = self.pattern_bound_names.iter().map(|(_, id)| *id).collect();
+ let pattern_bound_ids: Vec<IdentifierId> = pattern_bound_names.iter().map(|(_, id)| *id).collect();
+ catch_pattern_names = pattern_bound_names.iter().map(|(n, _)| *n).collect();
let Self {
scope_collector, arena, ..
} = self;
@@ -830,7 +831,7 @@ impl Parser<'_> {
// Collect catch parameter names (as StringIds) for post-body validation.
let catch_names: Vec<StringId> = match &parameter {
Some(CatchBinding::Identifier(id)) => vec![self.arena.identifiers[*id].name],
- Some(CatchBinding::BindingPattern(_)) => self.pattern_bound_names.iter().map(|(n, _)| *n).collect(),
+ Some(CatchBinding::BindingPattern(_)) => catch_pattern_names,
None => Vec::new(),
};
diff --git a/Tests/LibJS/Runtime/destructured-parameter-locals.js b/Tests/LibJS/Runtime/destructured-parameter-locals.js
index a7dedc933a5..3bc9d232ce0 100644
--- a/Tests/LibJS/Runtime/destructured-parameter-locals.js
+++ b/Tests/LibJS/Runtime/destructured-parameter-locals.js
@@ -54,3 +54,17 @@ test("multiple destructured parameters", () => {
}
expect(f({ a: 1 }, [2, 3])).toBe(6);
});
+
+test("destructured catch parameter followed by a destructuring declaration", () => {
+ function f() {
+ let caught;
+ try {
+ throw { message: "error" };
+ } catch ({ message }) {
+ caught = message;
+ }
+ const { value } = { value: 1 };
+ return [caught, value, typeof message];
+ }
+ expect(f()).toEqual(["error", 1, "undefined"]);
+});