New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
joostlubach opened this Issue Oct 11, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@joostlubach

joostlubach commented Oct 11, 2018

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.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Oct 11, 2018

Collaborator

Hey @joostlubach! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Oct 11, 2018

Hey @joostlubach! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment