Skip to content

Incorrect compilation for empty array #7258

@kevinptt0323

Description

@kevinptt0323

Input Code

const assert = require('assert')

let arr = new Array(3);
/*
 * or
 * let arr = [,,,];
 */
let arr2 = [...arr];

assert(arr2.hasOwnProperty(0));

.babelrc

{
  "presets": ["@babel/preset-env"]
}

Expected Behavior

No error will be thrown

Current Behavior

var assert = require('assert');

var arr = new Array(3);
var arr2 = arr.concat();
assert(arr2.hasOwnProperty(0));
AssertionError [ERR_ASSERTION]: false == true

Possible Solution

Since empty array (e.g., new Array, or [,,,]) has difference (weird?) structure.

Add _toConsumableArray and check properties when compiling rest operator.

Context

The following code will be incorrect.

let tmp = new Array(100);
let series = [...tmp].map((_,i) => i);
// [0, 1, 2, 3, ..., 99]

Your Environment

software version(s)
Babel 7.0.0-beta.38
node 8.8.1
npm -
Operating System -

Metadata

Metadata

Assignees

Labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions