Skip to content
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

Fix rest parameters indexing with TypeScript 'this parameter' #9714

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/babel-plugin-transform-parameters/src/rest.js
Expand Up @@ -153,6 +153,17 @@ const memberExpressionOptimisationVisitor = {
}
},
};

function getParamsCount(node) {
let count = node.params.length;
// skip the first parameter if it is a TypeScript 'this parameter'
BenoitZugmeyer marked this conversation as resolved.
Show resolved Hide resolved
const p0 = node.params[0];
if (p0 && t.isIdentifier(p0) && p0.name === "this") {
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
count -= 1;
}
return count;
}

function hasRest(node) {
const length = node.params.length;
return length > 0 && t.isRestElement(node.params[length - 1]);
Expand Down Expand Up @@ -242,10 +253,12 @@ export default function convertFunctionRest(path) {
node.body.body.unshift(declar);
}

const paramsCount = getParamsCount(node);

// check and optimise for extremely common cases
const state = {
references: [],
offset: node.params.length,
offset: paramsCount,

argumentsNode: argsId,
outerBinding: scope.getBindingIdentifier(rest.name),
Expand Down Expand Up @@ -295,12 +308,12 @@ export default function convertFunctionRest(path) {
state.candidates.map(({ path }) => path),
);

const start = t.numericLiteral(node.params.length);
const start = t.numericLiteral(paramsCount);
const key = scope.generateUidIdentifier("key");
const len = scope.generateUidIdentifier("len");

let arrKey, arrLen;
if (node.params.length) {
if (paramsCount) {
// this method has additional params, so we need to subtract
// the index of the current argument position from the
// position in the array that we want to populate
Expand Down
Expand Up @@ -2,6 +2,7 @@
"plugins": [
"proposal-class-properties",
"external-helpers",
"syntax-typescript",
"syntax-flow",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised that this works, flow and typescript shouldn't be compatible.

"transform-parameters",
"transform-block-scoping",
Expand Down
@@ -0,0 +1,7 @@
function u(this: Foo, ...items) {
items[0];
}

function v(this: Foo, event: string, ...args: any[]) {
args;
}
@@ -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;
}