Skip to content

Commit

Permalink
feat: convert default param with value true
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Mar 22, 2024
1 parent a2005a0 commit 33c5761
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
13 changes: 11 additions & 2 deletions packages/webcrack/src/transpile/test/default-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,19 @@ describe('Babel', () => {
function f() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
var y = arguments.length > 1 ? arguments[1] : undefined;
var z = arguments.length > 2 && arguments[2] !== undefined && arguments[2];
}
`).toMatchInlineSnapshot(`
function f(x = 1, y, z = false) {}
function f(x = 1, y) {}
`));

test('default parameter with value true and false', () =>
expectJS(`
function f() {
var x = arguments.length > 0 && arguments[0] !== undefined && arguments[0];
var y = arguments.length <= 1 || arguments[1] === undefined || arguments[1];
}
`).toMatchInlineSnapshot(`
function f(x = false, y = true) {}
`));

test('default parameter with gap before the last one', () =>
Expand Down
48 changes: 42 additions & 6 deletions packages/webcrack/src/transpile/transforms/default-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
);

// Example: arguments.length > 0 && arguments[0] !== undefined
const argumentCheck = m.logicalExpression(
const argumentCheckAnd = m.logicalExpression(
'&&',
m.binaryExpression(
'>',
Expand All @@ -32,12 +32,30 @@ export default {
m.identifier('undefined'),
),
);
// Example: arguments.length > 0 && arguments[0] !== undefined
const argumentCheckOr = m.logicalExpression(
'||',
m.binaryExpression(
'<=',
constMemberExpression('arguments', 'length'),
index,
),
m.binaryExpression(
'===',
m.memberExpression(
m.identifier('arguments'),
m.fromCapture(index),
true,
),
m.identifier('undefined'),
),
);
// Example: arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
const defaultParam = m.variableDeclaration(undefined, [
m.variableDeclarator(
varId,
m.conditionalExpression(
argumentCheck,
argumentCheckAnd,
m.memberExpression(
m.identifier('arguments'),
m.fromCapture(index),
Expand All @@ -53,7 +71,7 @@ export default {
varId,
m.logicalExpression(
'&&',
argumentCheck,
argumentCheckAnd,
m.memberExpression(
m.identifier('arguments'),
m.fromCapture(index),
Expand All @@ -62,6 +80,22 @@ export default {
),
),
]);
// Example: arguments.length <= 0 || arguments[0] === undefined || arguments[0]
const defaultTrueParam = m.variableDeclaration(undefined, [
m.variableDeclarator(
varId,
m.logicalExpression(
'||',
argumentCheckOr,
m.memberExpression(
m.identifier('arguments'),
m.fromCapture(index),
true,
),
),
),
]);

// Example: if (x === undefined) { x = 1; }
const defaultParamLoose = m.ifStatement(
m.binaryExpression('===', varName, m.identifier('undefined')),
Expand Down Expand Up @@ -105,9 +139,11 @@ export default {
? t.assignmentPattern(varId.current!, defaultExpression.current!)
: defaultFalseParam.match(path.node)
? t.assignmentPattern(varId.current!, t.booleanLiteral(false))
: normalParam.match(path.node)
? varId.current!
: null;
: defaultTrueParam.match(path.node)
? t.assignmentPattern(varId.current!, t.booleanLiteral(true))
: normalParam.match(path.node)
? varId.current!
: null;
if (!newParam) return;

for (let i = fn.params.length; i < index.current!.value; i++) {
Expand Down

0 comments on commit 33c5761

Please sign in to comment.