babel / babel/babel-polyfills

[Bug]: Array.prototype.at parse wrong with babel/cli

Open
#112 4 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
TypeScript
Stars
357
Forks
62
PR merge metrics
No merged PRs in 30d

Description

### 💻

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

### How are you using Babel?

@babel/cli

### Input code

case 1(wrong result)
```js
function test() {
this.array = [];
var lastIndexOfArray = this.array.at(-1);
}

output:

import "core-js/modules/esnext.string.at.js"; // should be import "core-js/modules/es.array.at.js"

function test() {
this.array = [];
var lastIndexOfArray = this.array.at(-1);
}
```

case 2 (wrong result)
```js
function test() {
var array = [];
var lastIndexOfArray = array.at(-1);
}

output(same as input, no core-js added):
function test() {
var array = [];
var lastIndexOfArray = array.at(-1);
}
```

case 3(parse as expected)
```js
function test() {
var string = '123';
var lastIndexOfString = string.at(-1);
}

output:
import "core-js/modules/esnext.string.at.js";

function test() {
var string = '123';
var lastIndexOfString = string.at(-1);
}
```

case 4(parse as expected)
```js
function test() {
this.string = '123';
var lastIndexOfString = string.at(-1);
}

output:
import "core-js/modules/esnext.string.at.js";

function test() {
this.string = '123';
var lastIndexOfString = string.at(-1);
}
```

### Configuration file name

_No response_

### Configuration

```json
{
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"useBuiltIns": "usage",
"targets": {
"chrome": 43,
"ios": 9,
"edge": 80,
"firefox": 60
},
"corejs": {
"proposals": true,
"version": 3
}
}
],
"@babel/preset-react"
]
}
```

### Current and expected behavior

add corejs es.array.at.js, not esnext.string.at.js

recognize Array.prototype.at

### Environment

System:
OS: macOS 11.6.1
Binaries:
Node: 16.13.0 - /usr/local/bin/node
npm: 8.1.0 - /usr/local/bin/npm
npmPackages:
@babel/cli: ^7.16.8 => 7.16.8
@babel/core: ^7.16.7 => 7.16.7
@babel/eslint-parser: ^7.16.5 => 7.16.5
@babel/plugin-proposal-decorators: ^7.16.7 => 7.16.7
@babel/plugin-transform-runtime: ^7.16.8 => 7.16.8
@babel/preset-env: ^7.16.8 => 7.16.8
@babel/preset-react: ^7.16.7 => 7.16.7
@babel/preset-typescript: ^7.16.7 => 7.16.7
@babel/runtime: ^7.16.7 => 7.16.7
babel-jest: ^27.4.6 => 27.4.6
babel-preset-const-enum: ^1.0.0 => 1.0.0
eslint: ^8.6.0 => 8.6.0
jest: ^27.4.7 => 27.4.7

### Possible solution

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the four examples through @babel/cli with the supplied @babel/preset-env and core-js configuration, comparing Array.prototype.at with String.prototype.at. Trace how usage-based polyfill detection classifies the method. Done means Array.prototype.at adds es.array.at.js, while String.prototype.at keeps its existing import and unrelated output remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.