babel / babel/babel

[Bug]: Rewrite Iterative Method doesn't Work after Conversion

Open
#13,942 4 comments 0 reactions 0 assignees View on GitHub
i: needs triage
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

@babel/cli

### Input code

```js
// Take the array and the original iterative method
var arr = [1,2,3];
var iteratorMethod = arr[Symbol.iterator];

// monitor method
var monitor = {
["return"](value) {
console.log(" >> RETURN", this && this.name || '');
return {value, done: true};
}
}

// Rewrite iterative method
arr[Symbol.iterator] = function() {
var tor = iteratorMethod.call(this);
return Object.assign(tor, monitor);
}

// test
for (let i of arr) {
if (i === 2) break;
console.log(" >>", i);
}
```
Here is [babel repl](https://babel.docschina.org/repl/#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&spec=false&loose=false&code_lz=PTAEBUEMGsFNQC4At6QE5sgT1JAdgCaIqgD2aAlgOYV6QA2oFCsmCFAbvALazKkEAUB3S4MoALygA2gEYANACZ5AZgC6AbmGjmrSAnIBZPkgGSxaaQGUs3AEal6AOl1tymwYJChupPM3IfEwFtNB8_ALCpAG9BUBkAIjQ-AFc0PAS1AAoRehTYAEpQWPj4gGM_AGdHWCd6UioshNAAPhbQACUAUXAAVQ6AOQT5YgpK0AAyCdHKpzpeUAAfRdAAclWCrVLQZIQ0vGLc_JGCP1gALkQ0fIBfLdAbwUevMA7YAHdKFiYWNk4eYJCdCWGz2RwuX76dzmABmKTwZXYfiyRRKoBEYQMUR-eixxn4BCcZQY9CyyDGmziO1S6VAAHk7AArWCIpyQSqVah4MnkEa-fxYynPbwsSoIQQwwJZeh8JhkGEWVFUigKrIUSQSKSKIp2ZIwe4VPDVGV1BpNVotYZMIVAA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=unambiguous&lineWrap=true&presets=env%2Ces2015%2Cenv&prettier=false&targets=Electron-1.8&version=7.16.2&externalPlugins=)

### Configuration file name

babel.config.js

### Configuration

```js
module.exports = function (api) {
api.cache(true);
const sourceType= "unambiguous";
const presets = [
["@babel/preset-env", { "modules": false}]
];
const plugins = [ ];

return {
sourceType,
presets,
plugins
};
}
```

### Current and expected behavior

current behavior:
when I use node run the code transed by babel, I get
```bash
>> 1
```
the expected result is (it's the origin code result run by node )
```bash
>> 1
>> RETURN
```
In other word, I expect rewrite iterative method take effect.

### Environment

- babel
- - ├── @babel/cli@7.16.0
- - ├── @babel/preset-env@7.16.0
- Node: [v16.11.1]
- npm version [8.0.0]
- os [ubuntu 20.04]

### Possible solution

_No response_

### Additional context

I run babel use cmd as follows
```bash
npx babel poc.js -o res/poc.js
```

Contributor guide

Open the contributing guide

Research direction

Start by running `npx babel poc.js -o res/poc.js` with the shown `babel.config.js`, then compare the transformed program's behavior in Node with the original. Done means the converted code preserves the iterator rewrite and prints `>> RETURN` after the loop breaks.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.