Skip to content

Commit

Permalink
fix: default parameters with gap (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Mar 22, 2024
1 parent d37d27d commit 98ca4a9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/webcrack/src/transpile/test/default-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ describe('Babel', () => {
function f(x = 1, y) {}
`));

test('default parameter with gap before the last one', () =>
expectJS(`
function f(e) {
var x = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var y = arguments.length > 5 ? arguments[5] : undefined;
}
`).toMatchInlineSnapshot(`
function f(e, x = {}, _param, _param2, _param3, y) {}
`));

test('default parameter (loose)', () =>
expectJS(`
function f(x, y) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,19 @@ export default {
if (!t.isFunction(fn) || path.key !== 0) return;

if (defaultParam.match(path.node)) {
for (let i = fn.params.length; i < index.current!.value; i++) {
fn.params[i] = t.identifier(path.scope.generateUid('param'));
}
fn.params[index.current!.value] = t.assignmentPattern(
varName.current!,
defaultExpression.current!,
);
path.remove();
this.changes++;
} else if (normalParam.match(path.node)) {
for (let i = fn.params.length; i < index.current!.value; i++) {
fn.params[i] = t.identifier(path.scope.generateUid('param'));
}
fn.params[index.current!.value] = varName.current!;
path.remove();
this.changes++;
Expand Down

0 comments on commit 98ca4a9

Please sign in to comment.