Skip to content

Nodes: array destructuring is broken in some cases and leads to TypeError #5004

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

Closed
adrian-gierakowski opened this issue Mar 4, 2018 · 0 comments · Fixed by #5005
Closed
Assignees
Labels

Comments

@adrian-gierakowski
Copy link
Contributor

bug report

when destructuring an array into properties on this, in some cases incorrect code is generated which leads to runtime error when executed

Input Code

array = [ 1, 2, 3, 4, 5 ]

[ @all_but_last_two... , @penultimate, @last ] = array

Expected Behavior

array is correctly destructured and running the code should not lead to runtime error

Current Behavior

running the code leads to following runtime error:

TypeError: Array.prototype.splice called on null or undefined

js code produced by CS 2.2.2:

// Generated by CoffeeScript 2.2.2
(function() {
  var array,
    splice = [].splice;

  array = [1, 2, 3, 4, 5];

  [...this.all_but_last_two] = array, [this.penultimate, this.last] = splice.call(undefined, -2);

}).call(this);

note the splice.call(undefined, -2); which should have been splice.call(this.all_but_last_two, -2);

Additional Notes

output from CS 2.1.1 ( which works as expected )

// Generated by CoffeeScript 2.1.1
(function() {
  var array, i,
    slice = [].slice;

  array = [1, 2, 3, 4, 5];

  this.all_but_last_two = 3 <= array.length ? slice.call(array, 0, i = array.length - 2) : (i = 0, []), this.penultimate = array[i++], this.last = array[i++];

}).call(this);

The bug has probably been introduced by #4825.

if we change @all_but_last_two to all_but_last_two the problem disappears

// Generated by CoffeeScript 2.2.2
(function() {
  var all_but_last_two, array,
    splice = [].splice;

  array = [1, 2, 3, 4, 5];

  [...all_but_last_two] = array, [this.penultimate, this.last] = splice.call(all_but_last_two, -2);

}).call(this);

Environment

  • CoffeeScript version: 2.2.2
  • Node.js version: 8.9.4
@zdenko zdenko added the bug label Mar 4, 2018
@zdenko zdenko self-assigned this Mar 4, 2018
@zdenko zdenko changed the title array destructuring is broken in some cases and leads to TypeError Nodes: array destructuring is broken in some cases and leads to TypeError Mar 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants