Skip to content

[plugin-transform-typescript@7.1.0] Combination of 'this' parameter and rest parameters goes wrong #8840

@joostlubach

Description

@joostlubach

Bug Report

Current Behavior

I have a function that both has a this parameter, and a spread / rest parameter pattern, such as:

function logEmit(this: Socket, event: string, ...args: any[]) {
  console.log('given arguments:', args)
}

When I call this function, the value of args is missing the first given argument.

Input code

logEmit.call(socket, 'message', 1, 2, 3, 4, 5)

Yields: given arguments: [2, 3, 4, 5]

Expected behavior/code

I expect the above call to yield given arguments: [1, 2, 3, 4, 5]

Babel Configuration (.babelrc, package.json, cli command)

I use React Native's module:metro-react-native-babel-preset, which uses @babel/plugin-transform-typescript@7.1.0 under the hood.

Environment

  • Babel version(s): v6.26.3
  • Node/npm version: v10.8.0
  • OS: OSX 10.13.6
  • How you are using Babel: not sure, through React Native's metro bundler

Possible Solution

Inspecting the generated code of my logEmit function learns why this happens: the (virtual) this parameter is taken into account in the argument extraction algorithm.

function logEmit(event) {
    for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
      args[_key - 2] = arguments[_key];
    }

    //...
}

The solution would be to ignore the this parameter, so that the number 2 would become 1 instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: plugin orderingoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions