Skip to content

[CS2] Destructured parameter default evaluation order is broken #4406

@connec

Description

@connec

Example:

current = 0
next    = -> ++current

foo = ({ a = next() }, b = next()) -> [ a, b ]

console.log foo {}

Expected output: [ 1, 2 ]

Output on master: [ 1, 2 ]

Output on 2: [ 2, 1 ]


This is caused by the 2 branch compiling defaults for simple parameters into the argument list, but handling complex parameters' defaults in the function body.

Output on master (simplified)

function (arg, b) {
  var a
  a = arg.a != null ? arg.a : next();
  if (b == null) {
    b = next();
  }
  return [a, b];
}

Output on 2 (simplified)

function (arg, b = next()) {
  var a;
  a = arg.a != null ? arg.a : next();
  return [a, b];
}

I encountered this whilst improving super handling in #4354 (coming soon).

I guess the way we want to fix this is to compile destructured parameters and their defaults to ES also, but I wanted to create an issue to make sure we don't forget about it!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions