-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Description
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.