Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion crates/perry-hir/src/lower_decl/class_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,25 @@ pub fn lower_setter_method_with_name(
continue;
}
let param_type = extract_param_type_with_ctx(&param.pat, Some(ctx));
// #5703: a setter with a defaulted parameter (`set a(_ = expr)`) must
// apply the default when invoked with `undefined` (e.g. `C.a =
// undefined`). Previously the setter param dropped its default
// (`default: None`) and emitted no prologue, so the default expression
// never ran. Mirror the constructor/method path: lower the default
// BEFORE defining the param local so its references resolve to the
// enclosing scope rather than the param or the body's hoisted `var`s —
// i.e. the separate parameter/body VariableEnvironment the spec
// mandates (test262 scope-*-setter-paramsbody-var-open) — then emit the
// `if (param === undefined) param = <default>` prologue below.
let param_default = get_param_default(ctx, &param.pat)?;
let param_id = ctx.define_local(param_name.clone(), param_type.clone());
ctx.shadow_native_instance_if_present(&param_name);
ctx.shadow_native_module_if_present(&param_name);
params.push(Param {
id: param_id,
name: param_name,
ty: param_type,
default: None,
default: param_default,
decorators: Vec::new(),
is_rest: false,
arguments_object: None,
Expand Down Expand Up @@ -846,6 +857,17 @@ pub fn lower_setter_method_with_name(
body = destructuring_stmts;
}

// #5703: prepend `if (param === undefined) param = <default>` for a
// defaulted setter parameter, so an under-supplied / `undefined` assignment
// (`C.a = undefined`) applies the default. Runs before any destructuring
// prologue (which destructures the already-defaulted param).
let default_stmts = build_default_param_stmts(&params);
if !default_stmts.is_empty() {
let mut new_body = default_stmts;
new_body.append(&mut body);
body = new_body;
}

ctx.exit_strict_mode();
ctx.exit_scope(scope_mark);

Expand Down
32 changes: 32 additions & 0 deletions crates/perry/tests/issue_5703_class_expr_static_dflt_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,35 @@ var C = class {
"default applies inside a static async generator"
);
}

/// A class-expression static SETTER with a defaulted parameter, invoked via
/// assignment of `undefined` (`C.a = undefined`). Pre-fix the setter dropped
/// its default (`default: None`, no prologue), so the default expression never
/// ran and `probeParams` stayed `undefined`. The default must also evaluate in
/// the parameter VariableEnvironment — its `x` closure sees the OUTER `x`
/// ("outside"), distinct from the setter body's `var x = "inside"` — matching
/// test262 `language/expressions/class/scope-static-setter-paramsbody-var-open`.
#[test]
fn class_expr_static_setter_default_param_scope() {
let dir = tempfile::tempdir().expect("tempdir");
let stdout = compile_and_run(
dir.path(),
r#"
var x = "outside";
var probeParams: any, probeBody: any;
var C: any = class {
static set a(_ = (probeParams = function () { return x; })) {
var x = "inside";
probeBody = function () { return x; };
}
};
C.a = undefined;
console.log(probeParams());
console.log(probeBody());
"#,
);
assert_eq!(
stdout, "outside\ninside\n",
"defaulted setter param runs its default in the parameter scope"
);
}
Loading