Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent default handling in destructured arrays and arguments #4408

Closed
za-creature opened this issue Dec 21, 2016 · 7 comments
Closed

Comments

@za-creature
Copy link

Possibly related to #4406

# bar, bar
meth = (foo = bar, bar) -> console.log(foo, bar)
meth(null, "bar")


# null, bar; should arguably be bar, bar for consistency
[foo = bar, bar] = [null, "bar"]
console.log(foo, bar)


# foo, foo
meth = (foo, bar = foo) -> console.log(foo, bar)
meth("foo", null)


# foo, foo
[foo, bar = foo] = ["foo", null]
console.log(foo, bar)
@lydell
Copy link
Collaborator

lydell commented Dec 21, 2016

ES2015 for comparison:

// throws: ReferenceError: bar is not defined
let meth = (foo = bar, bar) => console.log(foo, bar)
meth(undefined, "bar")
// throws: ReferenceError: bar is not defined
// or, if using var: logs: undefined 'bar'
let [foo = bar, bar] = [undefined, "bar"]
console.log(foo, bar)
// logs: foo foo
let meth = (foo, bar = foo) => console.log(foo, bar)
meth("foo", undefined)
// logs: foo foo
let [foo, bar = foo] = ["foo", undefined]
console.log(foo, bar)

@GeoffreyBooth
Copy link
Collaborator

Is this issue for 1.12.2 or for 2?

@za-creature
Copy link
Author

1.12, though it might be an issue in 2 as well (I haven't tested)

@connec
Copy link
Collaborator

connec commented Dec 22, 2016

I think you could use the case from #4406 (destructured arguments) to show the same issue.

The first example is definitely something else, though, since the params aren't destructured. That may actually be fixed in 2 since we're compiling to ES6 defaults.

Edit: Except that you'd have to pass undefined to trigger the default shakes fist

@GeoffreyBooth
Copy link
Collaborator

In #4478 your example compiles to (with null replaced by undefined, because that’s how default values get applied in CS2):

var bar, foo, meth;

meth = function(foo = bar, bar) {
  return console.log(foo, bar);
};

meth(void 0, "bar");

[foo = bar, bar] = [void 0, "bar"];

console.log(foo, bar);

meth = function(foo, bar = foo) {
  return console.log(foo, bar);
};

meth("foo", void 0);

[foo, bar = foo] = ["foo", void 0];

console.log(foo, bar);

Running it generates the same output as @lydell’s ES2015 example (first two are bar is not defined errors, latter two are foo foo).

I would consider this to be fixed in CS2 via #4478. I don’t plan on many (if any) more bug fixes for CS1, but pull requests are welcome 😄

@za-creature
Copy link
Author

So, is reference error to be expected? Asking because I expect I'll need to update my linter soon enough

@GeoffreyBooth
Copy link
Collaborator

Yes, in the first two when you have foo = bar bar isn't defined yet, so it's a reference error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants