Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix rest parameters indexing with TypeScript 'this parameter' (#9714)
* Fix rest parameters indexing with TypeScript 'this parameter' The TypeScript [this parameter][0] is a fake parameter and should not be taken into account when counting the function parameters. This patch skips it by using the condition taken from the `transform-typescript` plugin. Note: since the `transform-typescript` plugin is removing this kind of parameter, including it before the `transform-parameters` plugin solves the issue. This patch fixes the issue in other cases. [0]: https://www.typescriptlang.org/docs/handbook/functions.html#this-parameters * nit: improve the 'this parameter' detection condition * improve performance by checking the parameter list length before accessing it * simplify the test a bit by using the `isIdentifier` second parameter
- Loading branch information
1 parent
34a9653
commit 85ddc29
Showing
4 changed files
with
34 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...abel-plugin-transform-parameters/test/fixtures/parameters/rest-ts-this-parameter/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
function u(this: Foo, ...items) { | ||
items[0]; | ||
} | ||
|
||
function v(this: Foo, event: string, ...args: any[]) { | ||
args; | ||
} |
11 changes: 11 additions & 0 deletions
11
...bel-plugin-transform-parameters/test/fixtures/parameters/rest-ts-this-parameter/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function u(this: Foo) { | ||
arguments.length <= 0 ? undefined : arguments[0]; | ||
} | ||
|
||
function v(this: Foo, event: string) { | ||
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
|
||
args; | ||
} |