#18253·babel

[Bug]: for-of member assignment targets a shadowing binding from the loop body

Author: zloirockCreated Sep 27, 2026Updated Sep 27, 2026

Combining @babel/plugin-transform-block-scoping and @babel/plugin-transform-for-of changes which binding a member assignment in the loop head refers to.

This reproduces with only these two plugins, without custom plugins or assumptions.

Input

javascript
const obj = {};
for (obj.value of [1]) {
  const obj = 2;
  console.log(obj);
}
console.log(obj.value);

Configuration

json
{
  "plugins": [
    "@babel/plugin-transform-block-scoping",
    "@babel/plugin-transform-for-of"
  ]
}

Expected behavior

The original code prints 2, then 1.

The assignment in the loop head must target the outer obj. The const obj declaration inside the loop body introduces a separate binding.

Actual output

javascript
var obj = {};
for (var _i = 0, _arr = [1]; _i < _arr.length; _i++) {
  _obj.value = _arr[_i];
  var _obj = 2;
  console.log(_obj);
}
console.log(obj.value);

The generated assignment targets _obj, the renamed inner binding, before its initialization. Execution throws:

TypeError: Cannot set properties of undefined (setting 'value')

The assignment should remain obj.value = _arr[_i].

Versions

  • Node.js: 26.10.0
  • @babel/core: 8.0.6
  • @babel/plugin-transform-block-scoping: 8.0.5
  • @babel/plugin-transform-for-of: 8.0.1

Related issue

Possibly related to #16009, which concerned shadowing of the iterable with iterableIsArray. This reproduction concerns the member-assignment target on the left side and does not require that assumption.